在Android App Force上调用函数退出

时间:2015-09-08 05:14:00

标签: java android

我想在用户退出应用程序时注销该帐户,方法是按住主页按钮并轻扫应用程序以关闭该应用程序。是否有一个函数在发生时运行?

3 个答案:

答案 0 :(得分:2)

我建议你在应用程序中覆盖OnTrimMemory

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        if (level == TRIM_MEMORY_UI_HIDDEN) {
            doSomething();
        }
    }

答案 1 :(得分:1)

覆盖onDestroy()或onStop()

答案 2 :(得分:1)

  public class YourApplication extends Application {
  private Thread.UncaughtExceptionHandler androidDefaultUEH;

 @Override
 public void onCreate() {
     super.onCreate();
     androidDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
     Thread.setDefaultUncaughtExceptionHandler(handler);
 }


  private Thread.UncaughtExceptionHandler handler = new  Thread.UncaughtExceptionHandler() {

      public void uncaughtException(Thread thread, Throwable ex) {

          Log.e("YourApplication", "Uncaught exception is: ", ex);

          doSomething(); //  Here your code have to come ..

          androidDefaultUEH.uncaughtException(thread, ex);

      }

  };

在MANIFEST中

<application
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:name=".YourApplication"
   >