简体   繁体   中英

Android——How can i know whether the app is autostart or not

I want to know whether the user allows autostart permissions of the app. I have already tried this:

ComponentName mComponentName = new ComponentName(getPackageName(),".AutoStartReceiver");
int a = getPackageManager().getComponentEnabledSetting(mComponentName);

the autostart permission can be granted and denied by security app,but I don't know how to get the status in my application. such as the picture

you are probably using this permission:

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

After that, you can implement a BroadcastReceiver:

public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
}}

And just add the class to your manifest-file:

<receiver android:enabled="true" android:name=".BootUpReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

To check directly, if you have Autostart permissions, use this:

int p = ContextCompat.checkSelfPermission(Activity.this, Manifest.permission.RECEIVE_BOOT_COMPLETED);
if (p == PackageManager.PERMISSION_GRANTED) {
    //Yay, you have the receive boot completed (= Autostart) permission!
}

To get a list of receivers, registered for a specific intent, you can use Use PackageManager and queryBroadcastReceivers() .

To get a list for BOOT_COMPLETE , construct an intent with BOOT_COMPLETE action

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM