尽管使用了我在 stackoverflow&android developer 上找到的所有解决方案,Android 服务还是被破坏了

问题描述

我知道网上有很多长寿命 Android 服务的解决方案,但它们都不适用于我在 Android 9 上。因此,非常感谢您在此处提供的任何帮助。 我的服务实现了 SensorEventListener 并且我想连续监视手机的 3 个传感器变化,即使主要活动进入后台或被杀死(我知道电池寿命,这不是这里的问题)。我经历过的解决方案是:

1-使用 startForegroundService(按照 google 开发者的建议)

        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            Intent i = new Intent(context,myService.class);
            startForegroundService(i);
        }else {
            Intent i = new Intent(context,myService.class);
            startService(i);
        }

2-在服务结束时调用 startForeground onCreate

    Notification nf=GetNotification();
    startForeground(NOTIFICATION_ID,nf);

3-override onDestroyonTaskRemoved(单独和两者一起)并调用 sendbroadcast 将意图发送到在内部启动服务的广播接收器它的 onReceive 回调

   @Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);

    Intent tnt=new Intent("com.myAppPackage.appName.actionName");
    sendbroadcast(tnt);
}
public void onDestroy() {
    super.onDestroy();

    Intent tnt=new Intent("com.myAppPackage.appName.actionName");
    sendbroadcast(tnt);
}

和广播接收器:

private class clsKeepServiceAlive extends broadcastReceiver{
    @Override
    public void onReceive(Context context,Intent intent) {
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            Intent i = new Intent(context,myService.class);
            startService(i);
        }
    }
}
private clsKeepServiceAlive m_keepServiceAlive=new clsKeepServiceAlive();

并在服务OnCreate中动态和显式地注册此接收器,正如android开发人员文档所建议的:

    IntentFilter iflt4=new IntentFilter();
    iflt4.addAction("com.myAppPackage.appName.actionName");
    iflt4.addAction("android.intent.action.LOCKED_BOOT_COMPLETED");
    iflt4.addAction("android.intent.action.BOOT_COMPLETED");
    registerReceiver(m_keepServiceAlive,iflt4);

以及我尝试的最后一个解决方案,但没有成功

5- 在 Manifest 中使用服务属性

        <service
        android:name=".myService"
        android:enabled="true"
        android:exported="false"
        android:process=":myservicename"
        android:directBootAware="true"
        android:label="@string/app_name"
        android:stopWithTask="false" />

但是所有这些解决方案都不适合我。我在服务的 onDestroy 回调中放置了一条消息,以便在服务被销毁时写入数据库(以确保服务何时被销毁) 并且当我的应用程序进入后台或被用户杀死时,我肯定会收到通知前台服务通知)并且我在应用程序图标(红色的 1 上方图标)上看到它,但几分钟后(例如 20 分钟)当我回到手机我看到图标没有通知标志,通知本身消失并且服务已被破坏(我下次运行应用程序时可以看到它的消息)。 Afetr 将近 1 周尝试了网上建议的任何解决方案,我必须添加有关此问题的另一个问题,并请你们提供一定的帮助。这是应用程序 gradle 的一部分:

android {
compileSdkVersion 30
buildToolsversion "30.0.3"

defaultConfig {
    applicationId "com.myAppPackage.appName"
    minSdkVersion 23
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...