简体   繁体   English

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

[英]Service is being destroyed when activity is removed from background task android

I am starting a service class from my mainActivity like this:我正在从我的mainActivity开始一个服务类,如下所示:

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

It works perfectly on most of all devices but in some lenovo devices when I remove my app from background task the service is also destroyed with activity.I have tried Sticky service but it didn't worked.This type of issue is appearing on lenovo like devices only, on all most of other devices the service is not destroyed with activity and it is working fine.Can anyone help me to solve why is this happening.它在大多数设备上都能完美运行,但在某些 lenovo 设备中,当我从后台任务中删除我的应用程序时,该服务也会因活动而被破坏。我尝试过Sticky service但没有奏效。这种类型的问题出现在 lenovo 上,例如仅限设备,在所有其他大多数设备上,服务不会因活动而被破坏,并且工作正常。谁能帮我解决为什么会发生这种情况。

I have also tried in this way but didn't worked out:我也尝试过这种方式,但没有解决:

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

My Manifest:我的清单:

<?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>

Had this problem too.也有这个问题。

Most of Lenovo, Xiaomi, Asus, etc phones has Security system application in firmware which can block services.大多数联想、小米、华硕等手机在固件中都有Security系统应用程序,可以阻止服务。 You should check it and unlock blocking of your app`s autostart if needed.如果需要,您应该检查它并解锁应用程序自动启动的阻止。

By default its turned off.默认情况下它是关闭的。

Also some BroadcastReceiver functions (like receiving of SMS) cannot work because of it even if you give required permission.即使您给予所需的许可,一些BroadcastReceiver功能(如接收短信)也无法工作。

Worst thing is that you can`t check it programatically.最糟糕的是你不能以编程方式检查它。

You can run your service in a separate process .您可以在separate process运行您的服务。 Add the following attribute to the service declaration in the AndroidManifestAndroidManifest的服务声明中添加以下属性

android:process=":any-name-for-process"

You might want to take a look at this :你可能想看看这个:

How to keep a service running in background even after user quits the app? 即使用户退出应用程序,如何保持服务在后台运行?

and developer.android.com :和 developer.android.com :

Running a Service in the Foreground A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory.在前台运行服务 前台服务被认为是用户主动意识到的服务,因此不会在内存不足时被系统杀死。 A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.前台服务必须为状态栏提供通知,该通知位于“正在进行”标题下,这意味着除非服务停止或从前台删除,否则无法解除通知。

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);

Also, you can try to add to your onDestroy() function, a alarm that will restart the service after a few seconds since when it was destroyed.此外,您可以尝试在 onDestroy() 函数中添加一个警报,该警报将在服务被销毁后几秒钟后重新启动。

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

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