是否可以在WorkManager的doWork()中创建Main Activity?

问题描述 投票:0回答:1

如果应用程序的活动进入定期工作并被破坏,则定期工作是否有可能启动主要活动?

我将应用程序上下文保存在构造函数中,并尝试通过doWork()方法中的Handler类使用应用程序上下文。

我成功地在doWork()中使用了应用程序上下文。但我不知道如何重新创建主活动。我的意思是我想在工作线程中启动我的应用程序。

//java
public Result doWork() {

    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            String str = "BackGround Service running... " + called;
            Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
            /*
            I want to run my application again in here 
            */                                  
        }
    }, 0);
    return Result.success();
}
android android-workmanager
1个回答
1
投票
//java
public Result doWork() {
  Handler handler = new Handler(Looper.getMainLooper());
  handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        String str = "BackGround Service running... " + called;
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent( mContext ,MainActivity.class); 
        mContext.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));                              
    }
  }, 0);
  return Result.success();
}

有效...这是最好的方法吗?

© www.soinside.com 2019 - 2024. All rights reserved.