teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

サイトを参考にしたのですがうまくいかないです

2019/07/11 02:33

投稿

Takuminn
Takuminn

スコア14

title CHANGED
File without changes
body CHANGED
@@ -1,1 +1,90 @@
1
- android studioでwidgetにMainActivityで表示させているTextviewの情報を取得して表示させたいのですがなかなかうまくいきません。wigetprovider にどのようなプログラムを書けばいいか教えていただきたいです。
1
+ android studioでwidgetにMainActivityで表示させているTextviewの情報を取得して表示させたいのですがなかなかうまくいきません。wigetprovider にどのようなプログラムを書けばいいか教えていただきたいです。
2
+
3
+
4
+ 追記、、、サイトに載っていたものを参考に同じように書いたのですがクリックしても何も起こらないです
5
+
6
+
7
+ ```NewAppWidget
8
+ public class NewAppWidget extends AppWidgetProvider {
9
+
10
+ static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
11
+ int appWidgetId) {
12
+
13
+ CharSequence widgetText = context.getString(R.string.appwidget_text);
14
+ //CharSequence widgetText = context.getString(R.string.appwidget_text);
15
+ // Construct the RemoteViews object
16
+ RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
17
+ views.setTextViewText(R.id.appwidget_text, widgetText);
18
+ views.setOnClickPendingIntent(R.id.appwidget_text, clickAction(context));
19
+
20
+ // Instruct the widget manager to update the widget
21
+ appWidgetManager.updateAppWidget(appWidgetId, views);
22
+ }
23
+
24
+ @Override
25
+ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
26
+ // There may be multiple widgets active, so update all of them
27
+ for (int appWidgetId : appWidgetIds) {
28
+ updateAppWidget(context, appWidgetManager, appWidgetId);
29
+ }
30
+ }
31
+
32
+ @Override
33
+ public void onEnabled(Context context) {
34
+ // Enter relevant functionality for when the first widget is created
35
+ }
36
+
37
+ @Override
38
+ public void onDisabled(Context context) {
39
+ // Enter relevant functionality for when the last widget is disabled
40
+ }
41
+
42
+ @Override
43
+ public void onReceive(Context context, Intent intent) {
44
+ super.onReceive(context, intent);
45
+ if (intent.getAction().equals("click_action")) {
46
+ Toast.makeText(context, "click_action", Toast.LENGTH_SHORT).show();
47
+ }
48
+ }
49
+
50
+ private static PendingIntent clickAction(Context context) {
51
+ Intent intent = new Intent("click_action");
52
+ return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
53
+ }
54
+
55
+
56
+
57
+ }
58
+ ```
59
+ AndroidManifest.xml
60
+ ```AndroidManifest.xml
61
+ <application
62
+ android:allowBackup="true"
63
+ android:icon="@mipmap/ic_launcher"
64
+ android:label="時間割"
65
+ android:roundIcon="@mipmap/ic_launcher_round"
66
+ android:supportsRtl="true"
67
+ android:theme="@style/AppTheme">
68
+ <receiver android:name=".NewAppWidget">
69
+ <intent-filter>
70
+ <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
71
+
72
+ <action android:name="click_action"/>
73
+ </intent-filter>
74
+
75
+ <meta-data
76
+ android:name="android.appwidget.provider"
77
+ android:resource="@xml/new_app_widget_info" />
78
+ </receiver>
79
+
80
+ <activity android:name=".MainActivity">
81
+ <intent-filter>
82
+ <action android:name="android.intent.action.MAIN" />
83
+
84
+ <category android:name="android.intent.category.LAUNCHER" />
85
+ </intent-filter>
86
+ </activity>
87
+
88
+
89
+ </application>
90
+ ```