22
Foreground 検知

Foreground検知

Embed Size (px)

Citation preview

Page 1: Foreground検知

Foreground検知

Page 2: Foreground検知

About me

@kiharekato

Androidエンジニア

Tokyo Otaku Mode Inc.

Page 3: Foreground検知

Activity の Lifecycle にあるだろう

Page 4: Foreground検知

無い。

Page 5: Foreground検知

Applicationにあるんでしょ?

Page 6: Foreground検知

やっぱり無い。

Page 7: Foreground検知

まさか、Task系 !?

Page 8: Foreground検知

/**

* Return a list of the tasks that are currently running, with

** 中略 **

* <p><b>Note: this method is only intended for debugging and presenting

* task management user interfaces</b>. This should never be used for

* core logic in an application, such as deciding between different

* behaviors based on the information found here. Such uses are

* <em>not</em> supported, and will likely break in the future. For

* example, if multiple applications can be actively running at the

* same time, assumptions made about the meaning of the data here for

* purposes of control flow will be incorrect.</p>

*

* @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method

* is no longer available to third party

* applications: the introduction of document-centric recents means

* it can leak person information to the caller. For backwards compatibility,

* it will still retu rn a small subset of its data: at least the caller's

* own tasks, and possibly some other tasks

* such as home that are known to not be sensitive.

*/

@Deprecated

public List<RunningTaskInfo> getRunningTasks(int maxNum) throws SecurityException {

// 中略

}

ActivityManager.getRunningTasks

Page 9: Foreground検知

/**

* Return a list of the tasks that are currently running, with

** 中略 **

* <p><b>Note: this method is only intended for debugging and presenting

* task management user interfaces</b>. This should never be used for

* core logic in an application, such as deciding between different

* behaviors based on the information found here. Such uses are

* <em>not</em> supported, and will likely break in the future. For

* example, if multiple applications can be actively running at the

* same time, assumptions made about the meaning of the data here for

* purposes of control flow will be incorrect.</p>

*

* @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method

* is no longer available to third party

* applications: the introduction of document-centric recents means

* it can leak person information to the caller. For backwards compatibility,

* it will still retu rn a small subset of its data: at least the caller's

* own tasks, and possibly some other tasks

* such as home that are known to not be sensitive.

*/

@Deprecated

public List<RunningTaskInfo> getRunningTasks(int maxNum) throws SecurityException {

// 中略

}

ActivityManager.getRunningTasks

!?

Page 10: Foreground検知

/**

* Return a list of the tasks that are currently running, with

** 中略 **

* <p><b>Note: this method is only intended for debugging and presenting

* task management user interfaces</b>. This should never be used for

* core logic in an application, such as deciding between different

* behaviors based on the information found here. Such uses are

* <em>not</em> supported, and will likely break in the future. For

* example, if multiple applications can be actively running at the

* same time, assumptions made about the meaning of the data here for

* purposes of control flow will be incorrect.</p>

*

* @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method

* is no longer available to third party

* applications: the introduction of document-centric recents means

* it can leak person information to the caller. For backwards compatibility,

* it will still retu rn a small subset of its data: at least the caller's

* own tasks, and possibly some other tasks

* such as home that are known to not be sensitive.

*/

@Deprecated

public List<RunningTaskInfo> getRunningTasks(int maxNum) throws SecurityException {

// 中略

}

ActivityManager.getRunningTasks

!?

!?

Page 11: Foreground検知

Androidでは、

SDK側で提供されて無いじゃね?

Page 12: Foreground検知
Page 13: Foreground検知

public class MyApplication extends Application {

private Timer mActivityTransitionTimer;

private TimerTask mActivityTransitionTimerTask;

public boolean wasInBackground;

private final long MAX_ACTIVITY_TRANSITION_TIME_MS = 2000;

// これをonResumeで実行

public void startActivityTransitionTimer() {

this.mActivityTransitionTimer = new Timer();

this.mActivityTransitionTimerTask = new TimerTask() {

public void run() {

MyApplication.this.wasInBackground = true;

}

};

this.mActivityTransitionTimer.schedule(mActivityTransitionTimerTask, MAX_ACTIVITY_TRANSITION_TIME_MS);

}

  // これをonPauseで実行

public void stopActivityTransitionTimer() {

if (this.mActivityTransitionTimerTask != null) this.mActivityTransitionTimerTask.cancel();

if (this.mActivityTransitionTimer != null) this.mActivityTransitionTimer.cancel();

this.wasInBackground = false;

}

}

http://stackoverflow.com/a/15573121

Page 14: Foreground検知

うーん。微妙じゃね?

Page 15: Foreground検知
Page 16: Foreground検知

Application.ActivityLifecycleCallbacks

Page 17: Foreground検知

Application.ActivityLifecycleCallbacks

Page 18: Foreground検知

UsageStatsManager.queryEvents

Page 19: Foreground検知

UsageStatsManager.queryEvents

Page 20: Foreground検知

簡単に行えないのか?

Page 21: Foreground検知

 画面の構成をFragmentに移すことによって、

ActivityをApplicationと同等の完全単一にすることに

よって、Activity.onResumeでForegroundを取得

できるという計画である。

Activity補完計画

Page 22: Foreground検知

他に良い方法をご存知でしたら、教えてください。

ありがとうございました。