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

Chi*_*ati 1 android android-service android-lifecycle android-jetpack android-workmanager

我已安排工作经理定期在特定时间执行任务。但它正在与其他Android版本一起使用,例如25或更低版本。当我在API 28上运行它时,如果我从最近的列表中清除应用程序,它将停止触发广播。

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

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

这是我的工人班:

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.)
    }
}
Run Code Online (Sandbox Code Playgroud)

即使关闭或清除所有版本的android上的最新应用程序,我也要执行工作管理器。谢谢。

san*_*san 6

根据ankuranurag2的回答,确实有一些Android品牌在从最新版本中被杀死后被迫停止应用程序。在工作管理器Google问题跟踪器https://issuetracker.google.com/issues/110745313中检查此问题以 获取标签#2。

甚至我最近都在从最近的设备(设备-HUAWEI BKL-L09)中杀死了应用程序后尝试在后台运行该作业,但无法执行该操作。

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

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

1st创建一个服务,该服务标识终止我引用此URL的应用程序进程 /sf/answers/2999976801/

UserKillingService

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

在清单中配置服务

<service android:name=".UserKillingService"
            android:stopWithTask="false"/>
Run Code Online (Sandbox Code Playgroud)

该服务在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>
Run Code Online (Sandbox Code Playgroud)

或者您可以使用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)
        }
Run Code Online (Sandbox Code Playgroud)

该Startupactivity将完成,而不会强制停止应用程序。如果未从UserKillingService触发该活动,则该活动称为splashActivity。

在SplashActivity内部onCreate调用UserKillingService

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
        startService(Intent(baseContext,UserKillingService::class.java))
Run Code Online (Sandbox Code Playgroud)

请告诉我您是否有更好的解决方案。对不起我的英语不好