Java 当应用程序从堆栈中终止时,后台服务停止
当应用程序从堆栈中终止时,后台服务停止Java 当应用程序从堆栈中终止时,后台服务停止,java,android,Java,Android,当应用程序从堆栈中终止时,后台服务停止 public int onStartCommand(Intent intent, int flags, int startId) { googleApiClient = new GoogleApiClient.Builder(getApplicationContext()) .addConnectionCallbacks(this) .addOnConnectionF
public int onStartCommand(Intent intent, int flags, int startId) {
googleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
return START_STICKY;
}
没有直接的解决方案,但下面的链接中提到了解决方法 以上链接适用于库存ROM和定制ROM设备,如OnePlus、OPPO、维梧等
我希望这将帮助您走出困境转到应用程序信息并为您的应用程序授予自动启动权限。 如果你想在程序上获得这个许可
public class Utils {
public static void startPowerSaverIntent(Context context) {
SharedPreferences settings = context.getSharedPreferences("ProtectedApps", Context.MODE_PRIVATE);
boolean skipMessage = settings.getBoolean("skipProtectedAppCheck", false);
if (!skipMessage) {
final SharedPreferences.Editor editor = settings.edit();
boolean foundCorrectIntent = false;
for (Intent intent : Constants.POWERMANAGER_INTENTS) {
if (isCallable(context, intent)) {
foundCorrectIntent = true;
final AppCompatCheckBox dontShowAgain = new AppCompatCheckBox(context);
dontShowAgain.setText("Do not show again");
dontShowAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean("skipProtectedAppCheck", isChecked);
editor.apply();
}
});
new AlertDialog.Builder(context)
.setTitle(Build.MANUFACTURER + " Protected Apps")
.setMessage(String.format("%s requires to be enabled in 'Protected Apps' to function properly.%n", context.getString(R.string.app_name)))
.setView(dontShowAgain)
.setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
context.startActivity(intent);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
break;
}
}
if (!foundCorrectIntent) {
editor.putBoolean("skipProtectedAppCheck", true);
editor.apply();
}
}
}
private static boolean isCallable(Context context, Intent intent) {
try {
if (intent == null || context == null) {
return false;
} else {
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
} catch (Exception ignored) {
return false;
}
}
}
公共类Utils{
公共静态void startpowersavirecontent(上下文){
SharedReferences设置=context.getSharedReferences(“ProtectedApps”,context.MODE\u PRIVATE);
boolean skipMessage=settings.getBoolean(“skipProtectedAppCheck”,false);
如果(!skipMessage){
final sharedReferences.Editor=settings.edit();
布尔foundCorrectIntent=false;
for(意图:常量。POWERMANAGER\u意图){
if(可调用(上下文、意图)){
foundCorrectIntent=true;
final-AppCompatCheckBox dontshowleach=新的AppCompatCheckBox(上下文);
setText(“不再显示”);
setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener()){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
编辑器.putBoolean(“skipProtectedAppCheck”,已选中);
editor.apply();
}
});
新建AlertDialog.Builder(上下文)
.setTitle(Build.MANUFACTURER+“受保护的应用程序”)
.setMessage(String.format(“%s需要在“受保护的应用程序”中启用才能正常工作。%n”,context.getString(R.String.app_name)))
.setView(不再显示)
.setPositiveButton(“转到设置”,新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
背景。开始触觉(意图);
}
})
.setNegativeButton(android.R.string.cancel,null)
.show();
打破
}
}
如果(!foundCorrectIntent){
编辑器.putBoolean(“skipProtectedAppCheck”,true);
editor.apply();
}
}
}
私有静态布尔值可调用(上下文、意图){
试一试{
if(intent==null | | context==null){
返回false;
}否则{
List List=context.getPackageManager().QueryInputActivities(intent,
PackageManager.MATCH_(仅限默认值);
返回列表.size()>0;
}
}捕获(忽略异常){
返回false;
}
}
}
为不同的设备添加此选项
public class Constants {
//updated the POWERMANAGER_INTENTS 26/06/2019
static final List<Intent> POWERMANAGER_INTENTS = Arrays.asList(
new Intent().setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")),
new Intent().setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")),
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")),
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", Build.VERSION.SDK_INT >= Build.VERSION_CODES.P? "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity": "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
new Intent().setComponent(new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerUsageModelActivity")),
new Intent().setComponent(new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerSaverModeActivity")),
new Intent().setComponent(new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerConsumptionActivity")),
new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")),
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.startupapp.StartupAppListActivity")).setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).setData(Uri.parse("package:"+ MyApplication.getContext().getPackageName())) : null,
new Intent().setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity")),
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")),
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager")),
new Intent().setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")),
new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.entry.FunctionActivity")),
new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.autostart.AutoStartActivity")),
new Intent().setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"))
.setData(android.net.Uri.parse("mobilemanager://function/entry/AutoStart")),
new Intent().setComponent(new ComponentName("com.meizu.safe", "com.meizu.safe.security.SHOW_APPSEC")).addCategory(Intent.CATEGORY_DEFAULT).putExtra("packageName", BuildConfig.APPLICATION_ID)
);
}
公共类常量{
//2019年6月26日更新了POWERMANAGER计划
静态最终列表POWERMANAGER\u INTENTS=Arrays.asList(
new Intent().setComponent(新组件名称(“com.miui.securitycenter”、“com.miui.permcenter.autostart.AutoStartManagementActivity”),
new Intent().setComponent(新组件名称(“com.letv.android.letvsafe”,“com.letv.android.letvsafe.AutobootManageActivity”),
new Intent().setComponent(新组件名称(“com.huawei.systemmanager”、“com.huawei.systemmanager.optimize.process.ProtectActivity”),
new Intent().setComponent(新组件名称(“com.huawei.systemmanager”,Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.P?”com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity:“com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity”),
new Intent().setComponent(新组件名称(“com.coloros.oppoguardelf”、“com.coloros.powermanager.fuelgaue.PowerUsageModelActivity”),
new Intent().setComponent(新组件名称(“com.coloros.oppoguardelf”、“com.coloros.powermanager.fuelgaue.PowerSaverModeActivity”),
new Intent().setComponent(新组件名称(“com.coloros.oppoguardelf”、“com.coloros.powermanager.fuelgaue.PowerConsumptionActivity”),
new Intent().setComponent(新组件名称(“com.coloros.safecenter”、“com.coloros.safecenter.permission.startup.StartupAppListActivity”),
Build.VERSION.SDK_INT>=Build.VERSION_code.N?new Intent().setComponent(新组件名称(“com.coloros.safecenter”、“com.coloros.safecenter.startupapp.startupapplicistactivity”)).setAction(设置.操作(请求)忽略(电池)优化).setData(Uri.parse(“包:“+MyApplication.getContext().getPackageName()):null,
new Intent().setComponent(新组件名称(“com.oppo.safe”、“com.oppo.safe.permission.startup.StartupAppListActivity”),
new Intent().setComponent(新组件名称(“com.iqoo.secure”、“com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity”),
new Intent().setComponent(新组件名称(“com.iqoo.secure”、“com.iqoo.secure.ui.phoneoptimize.bgstartmanager”),
new Intent().setComponent(新组件名称(“com.vivo.permissionmanager”、“com.vivo.permissionmanager.activity.BgStartUpManagerActivity”),
new Intent().setComponent(新组件名称(“com.asus.mobilemanager”、“com.asus.mobilemanager.entry.FunctionActivity”),
new Intent().setComponent(新组件名称(“com.asus.mobilemanager”、“com.asus.mobilemanager.autostart.AutoStartActivity”),
new Intent().setComponent(新组件名(“com.letv.android.letvsafe”、“com.letv.android.letvsafe.AutobootManageActivity”))
.setData(android.net.Uri.parse(“mobilemanager://function/entry/AutoStart")),
new Intent().setComponent(新组件名(“com.meizu.safe”、“com.meizu.safe.security.SHOW_APPSEC”)).addCategory(Intent.CATEGORY_DEFAULT).putExtra(“packageName”,BuildConfig.APPLICATION_ID)
);
}
许可
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="oppo.permission.OPPO_COMPONENT_SAFE"/>
<uses-permission android:name="com.huawei.permission.external_app_settings.USE_COMPONENT"/>