【问题标题】:Android App Widget On Click Not Working When App Is closed关闭应用程序时单击Android应用程序小部件不起作用
【发布时间】:2021-12-05 00:07:01
【问题描述】:

请不要将此标记为重复我查看了 StackOverFlow 并没有找到解决方案

所以基本上我正在尝试构建一个带有 4 个按钮的简单小部件,但是当应用程序未在后台打开时不会调用 onRecieve,我到处查看(甚至是谷歌的第二页)但找不到一个有效的解决方案。

接收方已在清单中注册:

<receiver android:name=".Amount.WidgetAdd" android:exported="true" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_add_info" />
    </receiver>

我的预算是这样设置的:

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

    for (int i = 0; i<appWidgetIds.length; i++) {
        int id = appWidgetIds[i];

        remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_add);
        remoteViews.setOnClickPendingIntent(R.id.image_joints, getPendingSelfIntent(context, JointOnClick));
        remoteViews.setOnClickPendingIntent(R.id.image_bowls, getPendingSelfIntent(context, BowlOnClick));
        remoteViews.setOnClickPendingIntent(R.id.image_pipe, getPendingSelfIntent(context, PipeOnClick));
        remoteViews.setOnClickPendingIntent(R.id.image_more, getPendingSelfIntent(context, MoreOnClick));

        Intent intent = new Intent(context, WidgetAdd.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                420, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        c = context;
        prefs = c.getSharedPreferences("WT", Context.MODE_PRIVATE);


        appWidgetManager.updateAppWidget(id, remoteViews);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

我的 OnReceive 看起来像这样:

@Override
public void onReceive(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }

    if (JointOnClick.equals(intent.getAction())){
        Toast.makeText(context, "Joint", Toast.LENGTH_SHORT).show();
        try {
            AddJoint();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (BowlOnClick.equals(intent.getAction())){
        Toast.makeText(context, "Bowl", Toast.LENGTH_SHORT).show();
        try {
            AddBowl();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //your onClick action is here
    }
    if (PipeOnClick.equals(intent.getAction())){
        //your onClick action is here
        Toast.makeText(context, "Pipe", Toast.LENGTH_SHORT).show();
        try {
            AddPipe();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (MoreOnClick.equals(intent.getAction())){
        //your onClick action is here
        Toast.makeText(context, "More", Toast.LENGTH_SHORT).show();
        Log.w("WidgetAdd", "More");
    }
    super.onReceive(context, intent);
}

有人有什么建议吗?

【问题讨论】:

    标签: android android-studio android-fragments widget android-widget


    【解决方案1】:

    您必须为意图添加标志 FLAG_ACTIVITY_NEW_TASK

    intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
    

    【讨论】:

    • 你在什么文件中设置这个?
    • @Roderik 看到问题,它只有一个意图
    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多