2013-05-31 345 views
9

如何杀死onne单击完整的应用程序..完成()不工作?它重定向到以前的活动......请指导我。Android杀死进程

  public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      WallpaperManager wp = WallpaperManager 
        .getInstance(getApplicationContext()); 

      try { 
       Display d = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) 
         .getDefaultDisplay(); 
       int width = d.getWidth(); 
       int height = d.getHeight(); 
       wp.setBitmap(b1); 
      } catch (IOException e) { 
       Log.e("error", "Cannot set image as wallpaper", e); 
      } 

         finish(); 

     } 
+0

使用system.exit(0); – Riser

+0

虽然人们回答你的问题,你应该看看Gunaseelan的帖子并阅读“退出申请 - 是否皱眉?” http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon。 – Ryan

+0

对不起,我试过了......但不工作 – Coder

回答

1
  • 使用startActivityForResult启动任何活动。
  • result == RESULT_CANCELLED正在开始另一个活动的活动中写finish()onActivityResult
  • 最后在退出点使用finish()
1

我想你想这

Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 
5

您可以使用此功能来关闭应用程序

Process.killProcess(Process.myPid()); 
+1

int pid = android.os.Process.myPid(); android.os.Process.killProcess(pid);我写了这个命令..但它并没有为我工作...它只是杀死当前的活动 – Coder

25

System.exit(),如果你有在堆栈上一个以上活动不杀你的应用程序

这样使用android.os.Process.killProcess(android.os.Process.myPid());

样品

public void onBackPressed() { 
    android.os.Process.killProcess(android.os.Process.myPid()); 
    super.onBackPressed(); 
} 

更多细节见Is quitting an application frowned upon?How to force stop my android application programmatically?

我希望这会帮助你。

+0

我有两个activity.activity一个显示iamges在gridview和第二个活动显示选定的图像fullview.when我点击设置壁纸这两个活动应该关闭呐......但它没有;吨工作通过使用ABY完成,系统,退出(0),进程杀... – Coder

+0

退出应用程序 – Coder

+0

可能是错的..但我写完后,在两个活动之后工作..现在它的工作好:) – Coder

1

Finish()

在API级别1 时拨打电话的活动做和应该关闭这个。活动结果将传播给通过onActivityResult()启动的任何人。

您必须使用System.exit()来终止您的应用程序,直接关闭该活动。

为什么完成()不适合你?

举一个例子,其中活动A叫做活动B,当你在活动B上调用完成()时,如果完成活动,但活动A给他打电话,并且它是以前的活动,则它返回到活动A屏幕

你可以做什么?

  1. 您可以拨打System.exit() - 它会直接杀死无论从没关系它被称为应用程序。
  2. 使用Process.killProcess(您的进程ID);

  3. 通过使用面漆()杀死你当前活动启动家庭活动,然后调用

    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent);