gpt4 book ai didi

android - 如何验证我的 Android 应用程序的自动启动选项是启用还是禁用

转载 作者:太空宇宙 更新时间:2023-11-03 10:36:09 24 4
gpt4 key购买 nike

我想在启动时或安装应用程序后自动启动我的 Android 应用程序。我应用了以下代码:(AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javacodegeeks.androidserviceonbootexample"
android:installLocation="internalOnly"
>

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

<receiver
android:name="com.javacodegeeks.androidserviceonbootexample.BroadcastReceiverOnBootComplete"
android:enabled="true"
android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>

<service android:name="com.javacodegeeks.androidserviceonbootexample.AndroidServiceStartOnBoot"></service>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

(主要 Activity )

       import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import com.javacodegeeks.androidserviceonbootexample.R;

public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String manufacturer = "xiaomi";
if(manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
//this will open auto start screen where user can enable permission for your app
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);

}
}
}

(OnBootCompleteBroadcastReciever.java)

    import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class OnBootCompleteBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Toast.makeText(context,"BroadCast",Toast.LENGTH_LONG).show();
Intent serviceIntent = new Intent(context, ServiceStartOnBoot.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(serviceIntent);
}

}


}

(ServiceStartOnBoot.java)

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class ServiceStartOnBoot extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
}
}

但是上面的代码不起作用。如何检查自动启动权限是否已启用。此代码始终打开自动启动权限屏幕。我只想在应用未启用自动启动时显示此屏幕。

最佳答案

某些应用程序,如 Facebook、Gmail、Whatsapp 默认情况下已列入制造商的白名单,这意味着这些应用程序将自动启用自动启动权限。

目前无法验证我们的应用程序是否启用了自动启动选项,但您只能要求用户手动启用一次(假设用户安装应用程序时)。

您可以使用以下代码将用户重定向到自动启动权限的设置页面以启用您的应用(经过测试并适用于 Oppo、Vivo、Xiomi、Letv、Huawei 和 Honor):

try {
Intent intent = new Intent();
String manufacturer = android.os.Build.MANUFACTURER;
if ("xiaomi".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
} else if ("oppo".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
} else if ("vivo".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
} else if ("Letv".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
} else if ("Honor".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
}

List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0) {
startActivity(intent);
}
} catch (Exception e) {
Log.e("exc" , String.valueOf(e));
}

关于android - 如何验证我的 Android 应用程序的自动启动选项是启用还是禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44457852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com