Java Android AppWidget';主启动器强制停止后未收到s按钮单击事件

Java Android AppWidget';主启动器强制停止后未收到s按钮单击事件,java,android,android-widget,Java,Android,Android Widget,我有一个应用程序小部件,可以在我的提供商的onUpdate()中接收单击事件。 但是,当我试图强制关闭主启动器时,单击事件将丢失。 我甚至在所有的onEnabled(),onReceived()…中设置了断点:连接似乎丢失了 因此,如何“重新连接”按钮事件 WidgetProvider扩展了AppWidgetProvider: @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager

我有一个应用程序小部件,可以在我的提供商的
onUpdate()中接收单击事件。
但是,当我试图强制关闭主启动器时,单击事件将丢失。
我甚至在所有的
onEnabled()
onReceived()
…中设置了断点:连接似乎丢失了

因此,如何“重新连接”按钮事件

WidgetProvider扩展了AppWidgetProvider

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(TAG, "onUpdate()");
        Log.d(TAG, "isLoading: " + CONSTANT.isLoading);
        // update each of the widgets with the remote adapter from updateService
        // Get all ids
        ComponentName thisWidget = new ComponentName(context, ScoreWidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        // Build the intent to call the service
        Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        // Update the widgets via the service
        context.startService(intent);

//      super.onUpdate(context, appWidgetManager, appWidgetIds);
    }  
UpdateWidgetService扩展了
服务

@Override
        public void onStart(Intent intent, int startId) {

            Log.i(TAG, "Called");

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());

            int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

            for (int i = 0; i < appWidgetIds.length; ++i) {
                // Here we setup the intent which points to the StackViewService which will
                // provide the views for this collection.
                Intent remoteViewsIntent = new Intent(this.getApplicationContext(), ScoreWidgetRemoteViewsService.class);
                remoteViewsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
                // When intents are compared, the extras are ignored, so we need to embed the extras
                // into the data so that the extras will not be ignored.
                remoteViewsIntent.setData(Uri.parse(remoteViewsIntent.toUri(Intent.URI_INTENT_SCHEME)));
                RemoteViews rv = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout);
                rv.setRemoteAdapter(appWidgetIds[i], R.id.score_list, remoteViewsIntent);

                // Set the empty view to be displayed if the collection is empty.  It must be a sibling
                // view of the collection view.
                rv.setEmptyView(R.id.score_list, R.id.empty_view);

                // Bind the click intent for the refresh button on the widget
                final Intent refreshIntent = new Intent(this.getApplicationContext(), ScoreWidgetProvider.class);
                refreshIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                refreshIntent.setAction(ScoreWidgetProvider.REFRESH_ACTION);
                final PendingIntent refreshPendingIntent = PendingIntent
                        .getBroadcast(this.getApplicationContext(), 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                rv.setOnClickPendingIntent(R.id.btn_refresh, refreshPendingIntent);

                appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
            }

    //        stopSelf();

            super.onStart(intent, startId);
        }
@覆盖
公共无效启动(Intent Intent,int startId){
Log.i(标记“调用”);
AppWidgetManager AppWidgetManager=AppWidgetManager.getInstance(getApplicationContext());
int[]appWidgetIds=intent.getIntArrayExtra(AppWidgetManager.EXTRA\u APPWIDGET\u id);
对于(int i=0;i
确保在
onStart
功能中使用了正确的上下文。在代码的
onStart
部分中查看
getApplicationContext
,传入错误类型的上下文可能会导致错误。以下是有关详细信息的链接:。

在更新时启动服务!您可以在服务上完成工作。我的小部件包含listview,因此它已经有了扩展RemoteViewsService的类。所以我需要在服务上写另一个?根据我编辑的代码,我试图启动一个服务。但是当我强制关闭launcher时,刷新按钮仍然不起作用。我想知道是否需要将要更新的AppWidgetId添加到挂起的意图中以使其工作。(在您的情况下,所有AppWidgetId都是如此,因为您对每个小部件重复使用了相同的PendingEvent)