[英]How to autostart android app automatically without user permission?
I want to use background service in android . 我想在android中使用后台服务。 For that i have used background service, foreground service and job scheduler as well.
为此,我使用了后台服务,前台服务和作业计划程序。 But my app doesn't work in background without auto start option enabled.
但是,如果未启用自动启动选项,我的应用程序将无法在后台运行。
I have added background , foreground services and job scheduler also. 我还添加了background,前台服务和Job Scheduler。 I have added code to redirect the user to autostart setting enable.
我添加了代码以将用户重定向到自动启动设置启用。 As i have seen in many apps they won't ask user for the permission to enable autostart (ex. whatsapp, flipkart).
正如我在许多应用程序中看到的那样,他们不会要求用户启用自动启动功能(例如whatsapp,flipkart)。
private void scheduleJob() {
ComponentName componentName = new ComponentName(this, MyJobService.class);
JobInfo jobInfo = new JobInfo.Builder(123, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE)
// .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setPersisted(true) // job alive even if we reboot
// .setPeriodic(15 * 60 * 1000) // 15 mins
.setPeriodic(5 * 1000) // 5 secs
.build();
JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = jobScheduler.schedule(jobInfo);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.e(TAG, "Job Scheduled");
} else {
Log.e(TAG, "Job Scheduling failed");
}
My app doesn't work in background without auto start option enabled. 如果未启用自动启动选项,我的应用程序将无法在后台运行。 I want it enabled automatically.
我希望它自动启用。 Autostart enables automatically when you install app
安装应用程序时自动启动自动启动
Okay to implement that create a sticky service and register that in your main activity and on boot completed you should refresh your service whenever you need to restart the application just refresh your service and either create a notification or just refresh your service which this code: 可以实现创建粘性服务并在您的主要活动中进行注册,并在启动完成后进行注册,只要需要重新启动应用程序,就应该刷新服务,只需刷新服务并创建通知或刷新服务即可,此代码如下:
public static Boolean RestartApp1(Activity activity) {
if (!GlobalData.checkAndRequestPermissions(activity)) {
Intent i = activity.getBaseContext().getPackageManager().getLaunchIntentForPackage(activity.getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(i);
return false;
} else {
return true;
}
}
if (GlobalData.RestartApp1(activity)) {
getSharedData();
findViews();
initViews();
setActionBarSDK();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.