202005021116 2025-04-01 19:22 采纳率: 50%
浏览 10

安卓 12 为什么无法通过广播方式开启另一个应用?

我想实现的功能是注册广播,然后广播中的onReceive收到广播数据后直接开启另一个应用.
我试了一下,在安卓9中可以收到广播后打开另一个应用,但是安卓12上面却无法打开应用,该如何在安卓12或以上也能正常打开呢?

package com.example.launcherdemo;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.example.launcherdemo.bean.ActionBean;
import com.example.launcherdemo.other.DynamicServiceImpl;
import com.google.gson.Gson;
import com.jeremyliao.liveeventbus.LiveEventBus;

/**
 * date:        2025/3/31 星期一
 * author:      Bill
 * describe:
 */
public class AReceiver extends BroadcastReceiver {

    private static final String TAG = "AReceiver";
    private String otherPackageName="com.example.demob";//要开启的应用包名

    //和清单文件注册的广播action必须一致
    private static final String ACTION = "com.xzhy.ademo.ACTION_CUSTOM_BROADCAST_B";
    private static final String EXTRA_MESSAGE = "message";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION.equals(action)) {

//            String packageName = "com.example.demob";
//            String className = "com.example.demob.MainActivity";
//
//            Intent launchIntent = new Intent();
//            launchIntent.setComponent(new ComponentName(packageName, className));
//            launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须添加此 Flag
//
//            try {
//                context.startActivity(launchIntent);
//            } catch (ActivityNotFoundException e) {
//                // 处理目标应用未安装的情况
//                Log.e("MyReceiver", "Target app not found", e);
//            }

            String json = intent.getStringExtra(EXTRA_MESSAGE);
            Toast.makeText(context, "收到广播 1738 message=" + json, Toast.LENGTH_SHORT).show();
//            Log.d("AReceiver", "--- AReceiver onReceive message: "+message);
            //todo: 解析并操作接收到的JSON数据
            Gson gson = new Gson();
            ActionBean bean = gson.fromJson(json, ActionBean.class);
            String autoAction = bean.getData().getAction(); //获取并解析自动操作数据

            Log.d("1602", "动作= " + autoAction);
//            new DynamicServiceImpl(context).processEncodedData(json);
            switch (autoAction) {
                case "OPEN_APP":    //开启应用
                    LogUtils.d("1629 111111111111");
                    launch(context);
                    break;
            }
        }
    }

    private void launch(Context context) {
        //调起微信
        String weChatPackageName = "com.tencent.mm";
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ComponentName cn = new ComponentName(weChatPackageName, "com.tencent.mm.ui.LauncherUI");
        intent.setComponent(cn);
        context.startActivity(intent);

    }

}


  • 写回答

4条回答 默认 最新

  • 阿里嘎多学长 2025-04-01 19:22
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    安卓 12 为什么无法通过广播方式开启另一个应用?

    你遇到的问题是安卓 12 中无法通过广播方式开启另一个应用,这可能是由于安卓 12 中对广播的限制引起的。

    在安卓 12 中,Google 引入了新的广播机制,称为「Foreground Service Broadcasts」,它旨在限制广播的使用,以提高应用的安全性和性能。这个机制会将广播限制在前台服务中,无法在后台服务中接收广播。

    为了在安卓 12 中实现你的需求,你可以尝试以下方法:

    1. 使用 startForegroundService 方法来启动前台服务,然后在服务中注册广播接收器。
    2. 使用 JobScheduler 来 schedule 一个作业,然后在作业中启动另一个应用。
    3. 使用 IntentService 来启动服务,然后在服务中启动另一个应用。

    以下是一个使用 startForegroundService 方法的示例代码:

    public class MyService extends Service {
        @Override
        public void onCreate() {
            super.onCreate();
            // 注册广播接收器
            IntentFilter filter = new IntentFilter();
            filter.addAction("com.example.action");
            registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    // 开启另一个应用
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName("com.example.app", "com.example.app.MainActivity"));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            }, filter);
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
    
    public class MyActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // 启动前台服务
            Intent intent = new Intent(this, MyService.class);
            startForegroundService(intent);
        }
    }
    

    在上面的代码中,我们首先在 MyService 中注册广播接收器,然后在 onReceive 方法中启动另一个应用。在 MyActivity 中,我们启动前台服务 MyService

    请注意,这只是一个示例代码,你需要根据你的实际需求进行修改。

    评论

报告相同问题?

问题事件

  • 创建了问题 4月1日