几个小时后,Android Widget click 没有任何反应

标签 android widget

我有一个小部件,它有一个刷新按钮和一个 TextView 。刷新更新内容,当用户点击 textview 时,它会启动一个新 Activity 。

问题是它可以正常工作几个小时,然后点击和刷新按钮没有任何作用。 logcat 中没有捕获任何内容。此外,如果用户删除小部件并放置一个新小部件,它会开始工作几个小时,然后是同样的故事 :(...我做错了什么!

广播接收器。

更新时

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        long interval = getrefresInterval();

        final Intent intent = new Intent(context, UpdateService.class);
        final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
        final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pending);

        alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);


        // Build the intent to call the service
           RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);



        // To react to a click we have to use a pending intent as the
            // onClickListener is excecuted by the homescreen application
            Intent ClickIntent = new Intent(context.getApplicationContext(),widgetHadith.class);
            Intent UpdateIntent = new Intent(context.getApplicationContext(),UpdateService.class);

            PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, ClickIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);


            PendingIntent pendingIntentUpdate = PendingIntent.getService(context.getApplicationContext(), 0, UpdateIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT); //use this to update text on widget. if use this put UpdateService.class to intent

            remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);
            remoteViews.setOnClickPendingIntent(R.id.widget_refresh, pendingIntentUpdate);

            // Finally update all widgets with the information about the click listener
            appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);


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

    }

接收时

public void onReceive(Context context, Intent intent) {

    // v1.5 fix that doesn't call onDelete Action
    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } else {
        super.onReceive(context, intent);
    }
}

删除

public void onDeleted(Context context, int[] appWidgetIds) {
    //  Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
        super.onDeleted(context, appWidgetIds);
    }

我正在更新的服务onstart

    @Override
    public void onStart(Intent intent, int startId) {
        RemoteViews updateViews = new RemoteViews(this.getPackageName(),R.layout.widget);


        processDatabase();
        Spanned text = LoadHadith();
        String hadith = text.toString();
        Log.d("BR", "service---> ");
        // set the text of component TextView with id 'message'
        updateViews.setTextViewText(R.id.widget_textview, text);


        //Push update for this widget to the home screen
        ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);
 }

最佳答案

问题是您不能对小部件进行部分更新,您必须设置所有小部件功能,例如每次推送新的 remoteView 时设置 PendingIntent。 (部分更新仅适用于 API14 及更高版本...)。

你的小部件丢失它们的 pendingIntents 的原因是 android 系统保存了 remoteView,并用它重建你的小部件,以防它重置小部件(内存不足,正在使用的 TaskManager/taskKiller 等......),因此您必须在 updateService 的 remoteView 中设置小部件的所有更新代码。 否则,它不会再次设置 pendingIntents。

因此只需将设置 pendingIntents 的代码添加到服务中,您的问题就会得到解决 =]

关于几个小时后,Android Widget click 没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958126/

相关文章:

android - 搜索查看蜂窝

wordpress - 有一个错误。无法读取未定义的属性(读取 'map' )

Android从远程MySQL服务器(PHP)更新多个本地SQLite表,我该怎么办?

android - 通知 Activity 崩溃

android - 移动图书馆或图片库示例

php - 创建过去一周最受欢迎的帖子列表 - Wordpress

android - 在小部件配置 Activity 中设置 RESULT_CANCELED 是否多余?

android - EditText 中缺少光标和下划线

java - Gradle构建错误:任务 ':app:prepareDebugDependencies'的执行失败

python - 使用 Tkinter Text 索引表达式删除最后一行文本