如何将特定的挂起意图绑定到多小部件上特定小部件上的特定按钮..appwidget?

发布于 2024-09-24 03:03:46 字数 1622 浏览 3 评论 0原文

我正在开发一个小部件,它允许用户在屏幕上拥有多个小部件实例。每个 Widget id 都维护自己的配置文件。然而,由于某些奇怪的原因,我负责为每个小部件 id 单独设置按钮的代码不起作用,只有第一个小部件 id 链接到每个单独的小部件。下面是负责的代码。谁能看出问题出在哪里吗?

private void TieClicks(Context context){
  RemoteViews rViews;
  PendingIntent editPendingIntent= null;

//  Intent updateintent = new Intent(context,SyncNoteWidget.class);
//  updateintent.setAction(SyncNote_Action_Widget_Update);
//  PendingIntent pendingupdateintent = PendingIntent.getBroadcast(context, 0, updateintent, 0);
//  rViews.setOnClickPendingIntent(R.id.widgettextview , pendingupdateintent);
//  
  AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(context, SyncNoteWidget.class));
  for (int i =0;i< ids.length;i=i+1){
   int wId = ids[i];
   rViews = new RemoteViews(context.getPackageName(),R.layout.widget);

   editPendingIntent = makeControlPendingIntentActivity(context, wId);
   Log.v("syncnote", "tieing " + String.valueOf(wId));
   rViews.setOnClickPendingIntent(R.id.widgeteditbutton , editPendingIntent);
   appWidgetManager.updateAppWidget(wId, rViews);

   editPendingIntent= null;

  }
 }
 private PendingIntent makeControlPendingIntentActivity(Context context,int appWidgetId) {

     Intent active = new Intent(context, EditNote.class);
     active.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
     active.setAction(com.ntu.way2fungames.syncnote.SyncNoteWidget.SyncNote_Action_Edit); 
     active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     return(PendingIntent.getActivity(context, 0, active, 0 ));
    }

I have a Widget i am working on, that allows the user to have more then one instance of the widget on his screen. Each Widget id maintains its own configuration file. However for some odd reason my code that is responsible for setting up the buttons individually for each widget id is not working, only the first widget id is linked to each individual widget. below is the code that is responsible. Can anyone see where the problem is?

private void TieClicks(Context context){
  RemoteViews rViews;
  PendingIntent editPendingIntent= null;

//  Intent updateintent = new Intent(context,SyncNoteWidget.class);
//  updateintent.setAction(SyncNote_Action_Widget_Update);
//  PendingIntent pendingupdateintent = PendingIntent.getBroadcast(context, 0, updateintent, 0);
//  rViews.setOnClickPendingIntent(R.id.widgettextview , pendingupdateintent);
//  
  AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(context, SyncNoteWidget.class));
  for (int i =0;i< ids.length;i=i+1){
   int wId = ids[i];
   rViews = new RemoteViews(context.getPackageName(),R.layout.widget);

   editPendingIntent = makeControlPendingIntentActivity(context, wId);
   Log.v("syncnote", "tieing " + String.valueOf(wId));
   rViews.setOnClickPendingIntent(R.id.widgeteditbutton , editPendingIntent);
   appWidgetManager.updateAppWidget(wId, rViews);

   editPendingIntent= null;

  }
 }
 private PendingIntent makeControlPendingIntentActivity(Context context,int appWidgetId) {

     Intent active = new Intent(context, EditNote.class);
     active.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
     active.setAction(com.ntu.way2fungames.syncnote.SyncNoteWidget.SyncNote_Action_Edit); 
     active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     return(PendingIntent.getActivity(context, 0, active, 0 ));
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2024-10-01 03:03:46

问题是由于某种原因“PendingIntent.getActivity”正在重复使用第一个 PendingIntent ..上帝知道为什么。修复如下,只需在调用中添加一些随机数据即可。

private PendingIntent makeControlPendingIntentActivity(Context context,int appWidgetId) {
        Intent active = null;
        active = new Intent(context, EditNote.class);
        active.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        active.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK );
        active.setAction(com.ntu.way2fungames.syncnote.SyncNoteWidget.SyncNote_Action_Edit); 
        active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        PendingIntent pi =PendingIntent.getActivity(context,(int)(Math.random()*10000), active, 0 );
        Log.v("syncnote", "PI: "+pi.toString());
        return(pi);
    }

The problem was that for some reason "PendingIntent.getActivity" was re-using the first PendingIntent.. God knows why. The fix is below, simply add some random data to the call.

private PendingIntent makeControlPendingIntentActivity(Context context,int appWidgetId) {
        Intent active = null;
        active = new Intent(context, EditNote.class);
        active.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        active.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK );
        active.setAction(com.ntu.way2fungames.syncnote.SyncNoteWidget.SyncNote_Action_Edit); 
        active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        PendingIntent pi =PendingIntent.getActivity(context,(int)(Math.random()*10000), active, 0 );
        Log.v("syncnote", "PI: "+pi.toString());
        return(pi);
    }
淡忘如思 2024-10-01 03:03:46

如果您包含正在创建的日志条目,这将会有所帮助:

  Log.v("syncnote", "tieing " + String.valueOf(wId));

这可以回答重要的问题:
int[] ids 中有多少个值?

R.layout.widget 中有什么?

您的小部件的图片也会有所帮助。一张图胜过一千个字……

It would help if you included the log entries you're creating with:

  Log.v("syncnote", "tieing " + String.valueOf(wId));

That could answer the important question:
How many values are in: int[] ids ?

What is in R.layout.widget ?

A picture of your widgets would help too. A picture is worth a thousand words...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文