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

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日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法