我一直在尝试实现应用程序级别的暂停和恢复,类似于活动的onPause和onResume。我知道没有API具有此功能。
我尝试遵循这篇文章:http://curioustechizen.blogspot.com/2012/12/android-application-level-pause-and.html,但迄今为止我没有成功。
有人能够实现这一点吗?你使用了什么范式?
如果需要,让我知道是否需要将一些代码粘贴到此问题中。感谢帮助!
我一直在尝试实现应用程序级别的暂停和恢复,类似于活动的onPause和onResume。我知道没有API具有此功能。
我尝试遵循这篇文章:http://curioustechizen.blogspot.com/2012/12/android-application-level-pause-and.html,但迄今为止我没有成功。
有人能够实现这一点吗?你使用了什么范式?
如果需要,让我知道是否需要将一些代码粘贴到此问题中。感谢帮助!
解决该问题的另一个方法是跟踪每个活动的onStart()和onStop()调用次数。例如:
首先,创建一个类来保存计数:
public class ActiveActivitiesTracker {
private static int sActiveActivities = 0;
public static void activityStarted()
{
if( sActiveActivities == 0 )
{
// TODO: Here is presumably "application level" resume
}
sActiveActivities++;
}
public static void activityStopped()
{
sActiveActivities--;
if( sActiveActivities == 0 )
{
// TODO: Here is presumably "application level" pause
}
}
}
然后在每个活动中,只需调用activityStarted()和activityStopped()方法:
@Override
public void onStart() {
super.onStart();
ActiveActivitiesTracker.activityStarted();
}
@Override
public void onStop() {
super.onStop();
ActiveActivitiesTracker.activityStopped();
}
public class ApplicationLock {
private static final String TAG = ApplicationLock.class.getSimpleName();
private static final int LOCK_TIME = 1000; //lock after a second
private static boolean lock = true; //default is locked
private static Handler handler = new Handler();
private static Runnable runnable = new Runnable() {
@Override
public void run() {
lock = true;
Log.i("ActivityTracker", "App locked");
}
};
public static boolean activityStarted()
{
handler.removeCallbacks(runnable);
if(lock)
{
Log.i(TAG, "App resumed - LOCKED");
return true;
}else{
Log.i(TAG, "App resumed - NOT LOCKED");
return false;
}
}
public static void activityStopped()
{
handler.postDelayed(runnable, LOCK_TIME);
Log.i(TAG, "App paused - Starting countdown");
}
只需在您的活动onPause()中调用activityStopped(),并在onResume()中调用activityStarted()。检查activityStarted()的结果。如果返回true,请锁定您的应用程序。如果应用程序的方向发生变化,则会在onPause之后非常快地调用onResume,因此应用程序不会被锁定。
这种解决方案可能不适合每种情况,但在我的情况下,它是最好的解决方案。此外,您可以更改倒计时以增加用户体验(用户按错按钮并在几秒钟内返回应用程序,无需锁定应用程序)。希望这对其他人有用。
private boolean mAppActiveServiceBound = false;
private AppActiveService mAppActiveService = null;
private ServiceConnection mAppActiveConnection = new ServiceConnection() {
public void onServiceConnected( ComponentName className, IBinder service ) {
mAppActiveService = ( (AppActiveService.AppActiveBinder) service ).getService();
}
public void onServiceDisconnected( ComponentName className ) {
mAppActiveService = null;
}
};
然后在每个活动的onStart()和onStop()方法中:
@Override
public void onStart() {
super.onStart();
mAppActiveServiceBound = bindService( new Intent( this, AppActiveService.class ), mAppActiveConnection, Context.BIND_AUTO_CREATE );
}
@Override
public void onStop() {
super.onStop();
if( mAppActiveServiceBound ) {
unbindService( mAppActiveConnection );
mAppActiveServiceBound = false;
}
}
public class AppActiveService extends Service {
// Receives interactions from clients:
private final IBinder mBinder = new AppActiveBinder();
/**
* Provides a handle to the bound service.
*/
public class AppActiveBinder extends Binder {
AppActiveService getService() {
return AppActiveService.this;
}
}
@Override
public void onCreate(){
// TODO: Here is presumably "application level" resume
}
@Override
public void onDestroy(){
// TODO: Here is presumably "application level" pause
}
}
onPause
和类似的方法有什么问题吗? - hichris123onPause
并且拥有相同的代码吗? - hichris123