-情绪零碎- 2022-09-11 01:58 采纳率: 100%
浏览 79
已结题

Android12真机,Service中启动Activity不响应

问题遇到的现象和发生背景

Android12,真机红米K50G,在成功启动ForegroundService后,启动activity无效。
并且在Android10的虚拟机上能够正常启动activity

问题相关代码,请勿粘贴截图
public class ForegroundService extends Service {
    private static final String TAG = "May";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NotificationChannel channel = new NotificationChannel(Constants.WIDGET_NOTIFY_ID,
                Constants.WIDGET_NOTIFY_NAME,
                NotificationManager.IMPORTANCE_DEFAULT);
        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                .createNotificationChannel(channel);
        Notification notification = new NotificationCompat.Builder(this, Constants.WIDGET_NOTIFY_ID)
                .build();
        startForeground(Constants.CONSTANTS_1, notification);
        Log.e(TAG, "onStartCommand: ");
        Intent gotoCreate = new Intent();
        gotoCreate.setClass(this, MainActivity.class);
        gotoCreate.putExtra(Constants.GOTO_CREATE, true);
        gotoCreate.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(gotoCreate);
        stopForeground(Service.STOP_FOREGROUND_REMOVE);
        onDestroy();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate: " );

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
        stopForeground(Service.STOP_FOREGROUND_REMOVE);
    }
}
运行结果及报错内容

结果,服务正常启动后,能运行到startActivity();但是,手机没有响应,即没有启动这个activity。
虚拟机上能够正常跳转到MainActivity。
两个手机测试时都没有响应报错。

我的解答思路和尝试过的方法

尝试过给与悬浮窗权限,更改不同的intent flag。但是真机中跳转都无效

我想要达到的结果

真机能和虚拟机一样跳转到相应Activity

  • 写回答

2条回答 默认 最新

  • 立青_ 2022-09-13 09:38
    关注

    在 Android 10 或更高版本上运行的应用只有在满足以下一项或多项条件时,才能启动 Activity:

    应用具有可见窗口,例如前台 Activity。
    应用在前台任务的返回栈中拥有 Activity。
    应用在 Recents 屏幕上现有任务的返回栈中拥有 Activity。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月22日
  • 已采纳回答 9月14日
  • 创建了问题 9月11日

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)