android - 在 android 8.0 版中,我使用工作管理器定期触发广播,但是当我从最近的任务中清除应用程序时,它不起作用

标签 android android-service android-lifecycle android-jetpack android-workmanager

我已经设置了一个工作管理器来定期在特定时间执行任务。但它正在与其他 android 版本(如 25 或更低版本)一起使用。当我在 API 28 上运行它时,当我从最近的列表中清除应用程序时,它将停止触发广播。

我在 Activity 中安排了这样的工作经理:

 OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(MyWorker.class).setInitialDelay(15, TimeUnit.SECONDS).build();
 WorkManager.getInstance().enqueue(mywork);

这是我的 Worker 类:

public class MyWorker extends Worker {
    @NonNull
    @Override
    public Worker.WorkerResult doWork() {
        // Do the work here
        Mylogger.getInstance().printLog("MyWorr","doWork()");
        //fire broadcast from here
        Intent broadcastIntent = new Intent(getApplicationContext(), TimeMatchingBroadcastReceiver.class);
        getApplicationContext().sendBroadcast(broadcastIntent);
        CommonUtils.scheduleWorkManager();
        // Indicate success or failure with your return value:
        return WorkerResult.SUCCESS;

        // (Returning RETRY tells WorkManager to try this task again
        // later; FAILURE says not to try again.)
    }
}

即使我在所有版本的 android 上关闭或清除最近列表中的应用程序,我也想执行工作管理器。 谢谢。

最佳答案

根据 ankuranurag2 的回答,确实有一些 android 品牌在最近杀死应用程序后强制停止应用程序。 在工作管理器谷歌问题跟踪器中检查这个问题 https://issuetracker.google.com/issues/110745313对于标签 #2 。

即使我也尝试在从最近(设备 - HUAWEI BKL-L09)杀死应用程序后在后台运行该作业,但无法做到这一点。

将其更改为手动后检查自动启动选项,然后它工作正常。 但这不是让用户体验告诉用户更改自动启动选项的好主意,我们无法确定它是打开还是关闭。

搜索后我给出了补丁修复,我认为这不是一个更好的解决方案,但可以检查它是否适合我。

1st 创建一个标识终止应用程序进程的服务 我引用了这个网址 https://stackoverflow.com/a/42856811/8264148

用户杀戮服务

class UserKillingService : Service() {

    override fun onBind(intent: Intent?)= null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int)= Service.START_NOT_STICKY

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onTaskRemoved(rootIntent: Intent?) {
        val i = Intent(this,StartUpActivity::class.java)
        i!!.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        i!!.putExtra("USER_KILLED", "USER_KILLED")
        startActivity(i)
        //stop the service
        stopSelf()
    }
}

在 list 中配置服务

<service android:name=".UserKillingService"
            android:stopWithTask="false"/>

此服务调用 onTaskRemoved 中的 StartUpActivity 并停止根本不需要的服务。

此 Activity 是一个没有 UI 的主要启动器 Activity

StartUpActivity

<activity android:name=".StartUpActivity"
            android:theme="@android:style/Theme.NoDisplay">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

或者你可以使用 android:theme = "@android:style/Theme.Translucent.NoTitleBar"

Inside onCreate of StartUpActivity

if (intent.hasExtra("USER_KILLED")) {
//finish the instance
            finish()
        } else {
          SplashActivity::class.start(this@StartUpActivity,finish = true)
        }

此 Startupactivity 将完成而无需强制停止应用程序。 此 Activity 在未从 UserKillingService 触发时调用 splashActivity。

在 SplashActivity onCreate 中调用 UserKillingService

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
        startService(Intent(baseContext,UserKillingService::class.java))

如果您有更好的解决方案,请告诉我。对不起我的英语

关于android - 在 android 8.0 版中,我使用工作管理器定期触发广播,但是当我从最近的任务中清除应用程序时,它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54704427/

相关文章:

android - 何时是提交子 fragment 的最佳时间?

android - Activity 和 Fragment 中的自动 UI 配置更改处理有时会失败

android - onStart() 到底是做什么的? - 安卓

ios - 相当于 iOS 上的 Android 服务

android - 在调用方法之前绑定(bind)服务

android - 如何在 Android 中自动化 webview

android - 拖动时不调用 onTouch() ACTION_MOVE 案例

android - 为什么解绑服务onDestroy?

android - 将音频文件存储在数组中

android - 在微调器上设置背景时将微调器文本向左对齐