提问人:Wylliam Borba Bassos 提问时间:10/27/2022 更新时间:10/27/2022 访问量:469
使用 onPause 回调最小化一段时间后重新打开 Android 应用程序
Reopen Android app after some time when minimized using onPause callback
问:
我正在开发一个 Android 应用程序,由于某种原因,它尚未在随机时间被识别出来,它会最小化自己。我想使用最小化应用程序时调用的 onPause 回调函数以编程方式重新打开它。
如前所述,能够制定规则以在一段时间后调用应用程序重新打开,以便它不会立即使用 Timer 和 TimerTask 以及布尔键重新打开:
private Timer mActivityTransitionTimer;
private TimerTask mActivityTransitionTimerTask;
public boolean wasInBackground;
private final long MAX_ACTIVITY_TRANSITION_TIME_MS = 3000;
@Override
protected void onPause() {
super.onPause();
System.out.println("debug Pause/Resume: --- P A U S E ----");
startActivityTransitionTimer();
}
@Override
protected void onResume() {
super.onResume();
System.out.println("debug Pause/Resume: ---- R E S U M E ---");
stopActivityTransitionTimer();
}
public void stopActivityTransitionTimer() {
if (this.mActivityTransitionTimerTask != null) {
this.mActivityTransitionTimerTask.cancel();
}
if (this.mActivityTransitionTimer != null) {
this.mActivityTransitionTimer.cancel();
}
this.wasInBackground = false;
}
public void startActivityTransitionTimer() {
this.mActivityTransitionTimer = new Timer();
this.mActivityTransitionTimerTask = new TimerTask() {
public void run() {
wasInBackground = true;
System.out.println("debug Pause/Resume: --- NEED REOPENE --");
}
};
this.mActivityTransitionTimer.schedule(
mActivityTransitionTimerTask,
MAX_ACTIVITY_TRANSITION_TIME_MS
);
}
我需要将 NEED REOPEN 日志替换为使应用程序重新打开的内容。 我已经试过了:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("package");
if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
}
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("package","package.Activity"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
没有任何效果,我在一项活动中
答: 暂无答案
评论
SERVICE
SERVICE
Intent
startActivity()
Context
Activity