【问题标题】:In Appwidget RemoteViews' setOnClickPendingIntent is not getting called after user force stops application在 Appwidget RemoteViews 中,用户强制停止应用程序后未调用 setOnClickPendingIntent
【发布时间】:2018-05-19 20:40:42
【问题描述】:

在这里,我制作了一个小部件,并在我正在启动一项服务的地方添加了它的点击监听器。它运行良好。但是一旦用户强制从 appinfo 停止应用程序,我的小部件的点击没有响应。我不明白为什么会发生这种情况。对此的帮助将不胜感激。谢谢您

我的应用小部件类代码:

MyWidget.java:

public class MyWidget extends AppWidgetProvider {

    static Context cont;
    static SharedPreferences preferences;


    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        cont = context;
        Intent intent2 = new Intent();

        intent2.setAction("....");
        PendingIntent pendingIntent = PendingIntent.
                getBroadcast(context, 0,
                        intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
        views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them

        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }
@Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
                context.startService(new Intent(context, MyService.class));

       }
  @Override
    public void onEnabled(Context context) {
..................  }
  @Override
    public void onDisabled(Context context) {
  .................}

【问题讨论】:

  • 切换到使用显式 Intent (new Intent(context, YourReceiver.class)) 而不是隐式 Intent (setAction("....");)。至少,这将消除一些安全问题。它可能在这种情况下有所帮助,因为明确的Intent 是将应用程序移出停止状态的方法。
  • @CommonsWare - 谢谢..帮助。

标签: android broadcastreceiver android-service android-appwidget appwidgetprovider


【解决方案1】:

尽可能始终使用明确的Intent。您的代码在禁止隐式广播的 Android 8.0 及更高版本上无法运行。显式 Intent 可以让您的应用脱离停止状态。

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多