Android - Multiple appWidgets playing different sounds

笑着哭i 提交于 2019-12-04 07:43:55
Rabi

I had the same problem and solved it like this:

  1. In your AppWidgetProviderClass, declare your Intent as follows:

    Intent intent = new Intent(context, YourActivity.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    
  2. ...and PendingIntent

    PendingIntent pi = PendingIntent.getActivity(context, appWidgetId, intent,
                                                 PendingIntent.FLAG_UPDATE_CURRENT);
    
  3. In the Activity class, after getting the appWidgetId you want to update:

    int currentWidgetId = this.getIntent().getIntExtra(
           AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    
  4. ...you can use a function similar to this one:

    private void updateWidgetView() {
        views = new RemoteViews(YourWidget.class.getPackage().getName(),
                                R.layout.main_widget);
        mgr = AppWidgetManager.getInstance(this);
        views.setTextViewText(R.id.some_text_view, someText);
        // Tell the AppWidgetManager to perform an update on the current App Widget
        mgr.updateAppWidget(currentWidgetId, views);
    }
    

I had this Problem right now and with that code the problem is gone, Think that the widgetAppId in the Intent and the flag Create new will make this Intent always unique.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!