首页 > 解决方案 > 如何在不以编程方式在android中打开设置的情况下自动启动

问题描述

因为我想每两分钟将位置更新到服务器,所以我在 android 中添加了服务类,当移动设备重新启动时,我正在使用 boot_completed 调用广播接收器。但是即使我的设备重新启动或打开,该接收器也没有调用那个时候的申请我也没有拿到。

因为 AUTO-START 被禁用,这就是它不来的原因。如何在 android 中以编程方式启用,就像启用 gps 弹出窗口一样。

代码:如果我这样给出,它会打开自动启动页面的权限,而不是手动显示如何启用它。请帮助我。每次应用程序打开时此代码都意味着即使它已启用,也会出现。

if(Build.BRAND.equalsIgnoreCase("xiaomi") ){

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
    startActivity(intent);


}else if(Build.BRAND.equalsIgnoreCase("Letv")){

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
    startActivity(intent);

}
else if(Build.BRAND.equalsIgnoreCase("Honor")){

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
    startActivity(intent);

}

public class BootCompletedIntentReceiver extends BroadcastReceiver {
    PendingIntent pintent;
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "boot"+intent.getAction(), Toast.LENGTH_SHORT).show();
        Log.i("QQ","boot"+intent.getAction());

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent pushIntent = new Intent(context, ForegroundLocationService.class);
            context.startService(pushIntent);

/*
            Calendar cal = Calendar.getInstance();
         intent = new Intent(context, ForegroundLocationService.class);
            pintent = PendingIntent
                    .getService(context, 0, intent, 0);
            Log.i("QQ","else--less than oreo"+pintent);

            AlarmManager alarm = (AlarmManager)context. getSystemService(Context.ALARM_SERVICE);
            // Start service every 20 seconds
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                    5* 1000, pintent);*/
        }
    }
}

显现:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

 <receiver android:enabled="true"
        android:directBootAware="true"
        android:exported="true" android:name=".background_services.BootCompletedIntentReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
        </intent-filter>
        <meta-data android:name="enable" android:value="true"/>
        <meta-data android:name="bootType" android:value="restart"/>
        <meta-data android:name="sendToBack" android:value="true"/>
    </receiver>

标签: androidservicebroadcastreceiverbootcompleted

解决方案


据我所知,不可能达到与 gps Popup 相同的行为。也无法知道用户是否为您的 APP 开启了自动启动。所以我认为你唯一能做的就是要求用户为你的应用程序打开自动启动,就像你已经在做的那样。例如,在应用程序首次打开时询问一次,然后在共享首选项中存储一个值,以防止每次应用程序下次启动时弹出该弹出窗口。


推荐阅读