Android10中使用广播接收器启动应用程序启动失败问题
创始人
2024-08-20 09:00:55
0

在Android 10上,Google对应用程序启动进行了限制,因此如果应用程序需要在设备重启后自动启动,则需要以不同的方式实现。以下是使用JobScheduler和BroadcastReceiver在设备启动后自动启动应用程序的示例代码:

1.创建BroadcastReceiver类:

public class StartServiceOnBootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { startServiceOnBoot(context); } }

private void startServiceOnBoot(Context context) {
    // Start the service or activity you want to start on boot
    Intent intent = new Intent(context, MyService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}

}

2.在AndroidManifest.xml中注册BroadcastReceiver:

3.在应用程序中创建JobScheduler:

private void scheduleJob(Context context) { ComponentName serviceComponent = new ComponentName(context, TestJobService.class); JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent); builder.setPersisted(true); builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); builder.setRequiresCharging(false); builder.setRequiresDeviceIdle(false); builder.setMinimumLatency(1 * 1000); JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); jobScheduler.schedule(builder.build()); }

4.在应用程序中创建JobService:

public class TestJobService extends JobService { @Override public boolean onStartJob(JobParameters params) { // Start the service or activity you want to start on boot Intent intent = new Intent(this, MyService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { this.startForegroundService(intent); } else { this.startService(intent); } return false; }

@Override
public boolean onStopJob(JobParameters params) {
    return false;
}

}

通过这两种方式,您可以在Android 10上成功启动应用程序,即使设备被重启。

相关内容

热门资讯

黑科技好牌(WPK透视挂)外挂... 黑科技好牌(WPK透视挂)外挂透明挂辅助助手(软件透明挂)原来真的有挂(有挂方针)-哔哩哔哩1、下载...
黑科技存在(Wepoke后台)... 黑科技存在(Wepoke后台)外挂透明挂辅助ai代打(透明挂黑科技)竟然真的有挂(有挂详情)-哔哩哔...
黑科技总结(wepoke最新)... 黑科技总结(wepoke最新)外挂透明挂辅助助手(透视)竟然真的有挂(有挂秘诀)-哔哩哔哩1、完成w...
黑科技中牌率(WPK漏洞)外挂... 黑科技中牌率(WPK漏洞)外挂透明挂辅助器(透明挂黑科技)一直真的是有挂(有挂秘籍)-哔哩哔哩1、进...
黑科技有挂(Wepoke辅助a... 黑科技有挂(Wepoke辅助ai)外挂透明挂辅助ai代打(辅助挂)切实有挂(有挂秘籍)-哔哩哔哩1、...
黑科技讲解(Wepoke大厅房... 黑科技讲解(Wepoke大厅房)外挂透明挂辅助代打(智能ai)一直真的有挂(有挂细节)-哔哩哔哩1、...
黑科技黑科技(红龙扑克)外挂透... 黑科技黑科技(红龙扑克)外挂透明挂辅助设备(辅助挂)切实真的是有挂(有挂方法)-哔哩哔哩1)红龙扑克...
黑科技规律(wepoke挂)外... 黑科技规律(wepoke挂)外挂透明挂辅助器(ai辅助)本来存在有挂(有挂方针)-哔哩哔哩;1、we...
黑科技玄学(微扑克软件)外挂透... 黑科技玄学(微扑克软件)外挂透明挂辅助挂(透明黑科技)竟然真的有挂(证实有挂)-哔哩哔哩1)微扑克软...
黑科技软件(aapoker开发... 黑科技软件(aapoker开发)外挂透明挂辅助黑科技(ai代打)好像存在有挂(有挂透明挂)-哔哩哔哩...