简体   繁体   English

Android——我怎么知道应用是否自动启动

[英]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: 之后,您可以实现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() . 要获取针对特定意图注册的接收者列表,可以使用Use PackageManagerqueryBroadcastReceivers()

To get a list for BOOT_COMPLETE , construct an intent with BOOT_COMPLETE action 要获取BOOT_COMPLETE的列表, BOOT_COMPLETE使用BOOT_COMPLETE操作构造一个意图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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