본문 바로가기

Android Dev

[안드로이드] 서브 액티비티에서 어플 종료.[퍼옴]

딸바보님 블로그에서 발취하였습니다.

출처 : http://shstarkr.tistory.com/135 


메인 액티비티가 아닌 서브 액티비티일 경우 메인 액티비티를 호출해 메인에서 종료해주는 방법입니다.

1. AnroidManifest.xml 에 아래 코드를 삽입
  1. <uses-permission android:name="android.permission.RESTART_PACKAGES"/>  
2. 서브액티비티에 아래 함수를 선언하고 종료시킬 시점에서 close() 를 호출해줍니다.
  1. private void close()  
  2. {  
  3.      finish();  
  4.      Intent intent = new Intent(현재액티비티명.this, 메인액티비티명.class);  
  5.      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  6.      intent.putExtra("KILL_ACT"true);  
  7.      startActivity(intent);  
  8. }  
3. 이번엔 메인액티비티에 아래 소스를 추가해줍니다.
  1. protected void onNewIntent(Intent intent)  
  2. {  
  3.      super.onNewIntent(intent);  
  4.      boolean isKill = intent.getBooleanExtra("KILL_ACT"false);  
  5.      if(isKill)  
  6.      close();  
  7. }  
  8.       
  9. private void close()  
  10. {  
  11.      finish();  
  12.      int nSDKVersion = Integer.parseInt(Build.VERSION.SDK);  
  13.      if(nSDKVersion < 8)    //2.1이하  
  14.      {  
  15.            ActivityManager actMng = (ActivityManager)getSystemService(ACTIVITY_SERVICE);  
  16.            actMng.restartPackage(getPackageName());  
  17.      }  
  18.      else  
  19.      {  
  20.             new Thread(new Runnable() {  
  21.                  public void run() {  
  22.                       ActivityManager actMng = (ActivityManager)getSystemService(ACTIVITY_SERVICE);  
  23.                       String strProcessName = getApplicationInfo().processName;  
  24.                       while(true)  
  25.                       {  
  26.                            List<RunningAppProcessInfo> list = actMng.getRunningAppProcesses();  
  27.                            for(RunningAppProcessInfo rap : list)  
  28.                            {  
  29.                                 if(rap.processName.equals(strProcessName))  
  30.                               {  
  31.                                    if(rap.importance >= RunningAppProcessInfo.IMPORTANCE_BACKGROUND)  
  32.                                         actMng.restartPackage(getPackageName());  
  33.                                    Thread.yield();  
  34.                                    break;  
  35.                               }  
  36.                          }  
  37.                     }  
  38.                }  
  39.           }, "Process Killer").start();  
  40.      }  
  41. }  

출처 : http://shstarkr.tistory.com/135