【问题标题】:In android version 8.0 I used work manager to fire a broadcast on periodic time, But when I clear an app from recent tasks It is not working在android 8.0版中,我使用工作管理器定期触发广播,但是当我从最近的任务中清除应用程序时它不起作用
【发布时间】:2019-02-15 07:24:34
【问题描述】:

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

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

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

这是我的工人阶级:

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 的最近列表中关闭或清除应用程序,我也想执行工作管理器。 谢谢。

【问题讨论】:

  • 您的return WorkerResult.SUCCESS 告诉我,我认为您使用的不是最新版本。尝试更新到最新版本(目前我认为是 beta05')。
  • @Ridcully 感谢您的建议。让我更新并测试一下。
  • @Ridcully 以前的问题是通过增加工作库的版本来解决的。 But now it is not working On model Xiaomi MI 5. API level 28 (8.0.0) 当我从最近的列表中清除应用程序时。你能帮我解决一下吗?
  • 我想这是最好的,如果您为 WorkManager 提交错误。根据各个 Beta 版本的发行说明,他们似乎为各个 Android 版本修复了很多类似的错误。

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


【解决方案1】:

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

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

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

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

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()
    }
}

在清单中配置服务

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

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

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

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 将在不强制停止应用程序的情况下完成。 此活动在未被 UserKillingService 触发时调用 splashActivity。

在 SplashActivity onCreate 内部调用 UserKillingService

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

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

【讨论】:

  • 在 Android 8 (Oreo) 上测试过。完美运行:thums-up:。但我有 1 个问题。为什么在启动服务之前要检查“SDK_INT >= VERSION_CODES.O”?
  • @MarkKowalski 因为限制电池优化的设备在牛轧糖之上。您可以查看有关 android workmanager 的视频。他们正试图通过联系制造商来解决这个问题。 youtube.com/watch?v=Qxj2eBmXLHg&t=1354s
  • 在 Android 10 (R) 上测试。效果很好!真是聪明的把戏!谢谢!
【解决方案2】:

如果你通过THE LINK,你会发现:

不幸的是,有些设备会从最近的菜单中终止应用程序作为强制停止。股票 Android 不这样做。当应用程序被强制停止时,它无法执行作业、接收警报或广播等。很遗憾,我们无法解决它 - 问题出在操作系统上,没有解决方法。

这是一个已知问题。很多厂商为了省电,强行关闭应用,取消所有的经期任务、闹钟、广播接收等。主要厂商是一加(可以选择切换)、Redmi、Vivo、Oppo、华为。

这些设备中的每一个都具有 AutoStartManagers/AutoLaunch/StartManager 类型的优化管理器。这会阻止后台活动重新开始。您必须手动要求用户将您的应用程序列入白名单,以便应用程序可以自动启动其后台进程。关注THISTHIS 链接,了解更多信息。

不同厂家添加白名单的方法在this stackoverflow answer中给出。即使添加到白名单后,您的应用也可能因为打盹模式而无法运行,为此您必须ignore battery otimizations

此外,如果您想知道,Gmail/Hangout/WhatsApp/Slack/LinkedIn 等应用程序已经被这些 AutoStart 管理器列入白名单。因此,对其后台进程没有影响。您始终会收到及时的更新和通知。

P.S:复制自我自己的回答HERE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多