当 activity 从后台任务 android 中删除时,服务正在被销毁

Service is being destroyed when activity is removed from background task android

我正在从我的 mainActivity 启动服务 class,如下所示:

Intent io = new Intent(MainActivity.this, Window.class);
startService(io);

它在大多数设备上都能完美运行,但在某些 lenovo 设备上,当我从后台任务中删除我的应用程序时,服务也会被破坏 activity.I 已尝试 Sticky service 但它没有 worked.This 类型的问题仅出现在像联想这样的设备上,在所有其他大多数设备上,服务都没有被 activity 破坏并且它正在工作 fine.Can 谁能帮我解决为什么会这样.

我也试过这种方法,但没有成功:

Intent io = new Intent(MainActivity.this, Window.class);
getBaseContext.startService(io);

我的清单:

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".Boot" android:enabled="true" android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

    <service
        android:name=".Capture"
        android:label="@string/service_name"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>

        <meta-data
            android:name="android.access"
            android:resource="@xml/access" />
    </service>

    <activity android:name=".Faq"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        android:launchMode="singleInstance"/>

    <service android:name=".Window"/>

    <activity android:name=".Intro1"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"/>
    <activity android:name=".Intro2"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"/>
    <activity android:name=".Intro3"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"/>
    <activity android:name=".Permit"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"/>


</application>

您可以 运行 在 separate process 中使用您的服务。在 AndroidManifest

中的服务声明中添加以下属性
android:process=":any-name-for-process"

也有这个问题

大部分联想、小米、华硕等手机在固件中都有Security系统应用,可以屏蔽服务。您应该检查它并在需要时解除对应用程序自动启动的阻止。

默认关闭。

还有一些 BroadcastReceiver 功能(如接收短信)即使您给予必要的权限也无法正常工作。

最糟糕的是您无法通过编程方式检查它。

您可能想看看这个:

How to keep a service running in background even after user quits the app?

和developer.android.com:

运行 前台服务 前台服务是一种被认为是用户主动意识到的服务,因此不会在内存不足时被系统杀死。前台服务必须为状态栏提供通知,该通知位于 "Ongoing" 标题下,这意味着除非服务停止或从前台删除,否则无法关闭通知。

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
        System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);

此外,您可以尝试在 onDestroy() 函数中添加一个警报,该警报会在服务被销毁后几秒后重新启动。