質問編集履歴
1
サイトを参考にしたのですがうまくいかないです
test
CHANGED
File without changes
|
test
CHANGED
@@ -1 +1,179 @@
|
|
1
1
|
android studioでwidgetにMainActivityで表示させているTextviewの情報を取得して表示させたいのですがなかなかうまくいきません。wigetprovider にどのようなプログラムを書けばいいか教えていただきたいです。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
追記、、、サイトに載っていたものを参考に同じように書いたのですがクリックしても何も起こらないです
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```NewAppWidget
|
14
|
+
|
15
|
+
public class NewAppWidget extends AppWidgetProvider {
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
|
20
|
+
|
21
|
+
int appWidgetId) {
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
CharSequence widgetText = context.getString(R.string.appwidget_text);
|
26
|
+
|
27
|
+
//CharSequence widgetText = context.getString(R.string.appwidget_text);
|
28
|
+
|
29
|
+
// Construct the RemoteViews object
|
30
|
+
|
31
|
+
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
|
32
|
+
|
33
|
+
views.setTextViewText(R.id.appwidget_text, widgetText);
|
34
|
+
|
35
|
+
views.setOnClickPendingIntent(R.id.appwidget_text, clickAction(context));
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
// Instruct the widget manager to update the widget
|
40
|
+
|
41
|
+
appWidgetManager.updateAppWidget(appWidgetId, views);
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
@Override
|
48
|
+
|
49
|
+
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
50
|
+
|
51
|
+
// There may be multiple widgets active, so update all of them
|
52
|
+
|
53
|
+
for (int appWidgetId : appWidgetIds) {
|
54
|
+
|
55
|
+
updateAppWidget(context, appWidgetManager, appWidgetId);
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
@Override
|
64
|
+
|
65
|
+
public void onEnabled(Context context) {
|
66
|
+
|
67
|
+
// Enter relevant functionality for when the first widget is created
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
@Override
|
74
|
+
|
75
|
+
public void onDisabled(Context context) {
|
76
|
+
|
77
|
+
// Enter relevant functionality for when the last widget is disabled
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
@Override
|
84
|
+
|
85
|
+
public void onReceive(Context context, Intent intent) {
|
86
|
+
|
87
|
+
super.onReceive(context, intent);
|
88
|
+
|
89
|
+
if (intent.getAction().equals("click_action")) {
|
90
|
+
|
91
|
+
Toast.makeText(context, "click_action", Toast.LENGTH_SHORT).show();
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
private static PendingIntent clickAction(Context context) {
|
100
|
+
|
101
|
+
Intent intent = new Intent("click_action");
|
102
|
+
|
103
|
+
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
AndroidManifest.xml
|
118
|
+
|
119
|
+
```AndroidManifest.xml
|
120
|
+
|
121
|
+
<application
|
122
|
+
|
123
|
+
android:allowBackup="true"
|
124
|
+
|
125
|
+
android:icon="@mipmap/ic_launcher"
|
126
|
+
|
127
|
+
android:label="時間割"
|
128
|
+
|
129
|
+
android:roundIcon="@mipmap/ic_launcher_round"
|
130
|
+
|
131
|
+
android:supportsRtl="true"
|
132
|
+
|
133
|
+
android:theme="@style/AppTheme">
|
134
|
+
|
135
|
+
<receiver android:name=".NewAppWidget">
|
136
|
+
|
137
|
+
<intent-filter>
|
138
|
+
|
139
|
+
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
<action android:name="click_action"/>
|
144
|
+
|
145
|
+
</intent-filter>
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
<meta-data
|
150
|
+
|
151
|
+
android:name="android.appwidget.provider"
|
152
|
+
|
153
|
+
android:resource="@xml/new_app_widget_info" />
|
154
|
+
|
155
|
+
</receiver>
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
<activity android:name=".MainActivity">
|
160
|
+
|
161
|
+
<intent-filter>
|
162
|
+
|
163
|
+
<action android:name="android.intent.action.MAIN" />
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
168
|
+
|
169
|
+
</intent-filter>
|
170
|
+
|
171
|
+
</activity>
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
</application>
|
178
|
+
|
179
|
+
```
|