android studioでwidgetにMainActivityで表示させているTextviewの情報を取得して表示させたいのですがなかなかうまくいきません。wigetprovider にどのようなプログラムを書けばいいか教えていただきたいです。
追記、、、サイトに載っていたものを参考に同じように書いたのですがクリックしても何も起こらないです
NewAppWidget
1public class NewAppWidget extends AppWidgetProvider { 2 3 static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, 4 int appWidgetId) { 5 6 CharSequence widgetText = context.getString(R.string.appwidget_text); 7 //CharSequence widgetText = context.getString(R.string.appwidget_text); 8 // Construct the RemoteViews object 9 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); 10 views.setTextViewText(R.id.appwidget_text, widgetText); 11 views.setOnClickPendingIntent(R.id.appwidget_text, clickAction(context)); 12 13 // Instruct the widget manager to update the widget 14 appWidgetManager.updateAppWidget(appWidgetId, views); 15 } 16 17 @Override 18 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 19 // There may be multiple widgets active, so update all of them 20 for (int appWidgetId : appWidgetIds) { 21 updateAppWidget(context, appWidgetManager, appWidgetId); 22 } 23 } 24 25 @Override 26 public void onEnabled(Context context) { 27 // Enter relevant functionality for when the first widget is created 28 } 29 30 @Override 31 public void onDisabled(Context context) { 32 // Enter relevant functionality for when the last widget is disabled 33 } 34 35 @Override 36 public void onReceive(Context context, Intent intent) { 37 super.onReceive(context, intent); 38 if (intent.getAction().equals("click_action")) { 39 Toast.makeText(context, "click_action", Toast.LENGTH_SHORT).show(); 40 } 41 } 42 43 private static PendingIntent clickAction(Context context) { 44 Intent intent = new Intent("click_action"); 45 return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 46 } 47 48 49 50}
AndroidManifest.xml
AndroidManifest.xml
1<application 2 android:allowBackup="true" 3 android:icon="@mipmap/ic_launcher" 4 android:label="時間割" 5 android:roundIcon="@mipmap/ic_launcher_round" 6 android:supportsRtl="true" 7 android:theme="@style/AppTheme"> 8 <receiver android:name=".NewAppWidget"> 9 <intent-filter> 10 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 11 12 <action android:name="click_action"/> 13 </intent-filter> 14 15 <meta-data 16 android:name="android.appwidget.provider" 17 android:resource="@xml/new_app_widget_info" /> 18 </receiver> 19 20 <activity android:name=".MainActivity"> 21 <intent-filter> 22 <action android:name="android.intent.action.MAIN" /> 23 24 <category android:name="android.intent.category.LAUNCHER" /> 25 </intent-filter> 26 </activity> 27 28 29 </application>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/11 02:33
退会済みユーザー
2019/07/12 06:52