Android 怎么退出整个应用程序
2个回答
展开全部
1.后面添加了一个菜单返回功能键,这个方法也只能用system.exit(0)来关闭当前活动的Activity,附代码:
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Exit");
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
System.exit(0);
return true;
}
});
return true;
}
2.这样太不方便,每次退出界面之后应用程序还在运行,
网上提供了几种方法,将下面SysApplication这个类复制到工程里面,然后在每个Acitivity的oncreate方法里面通过SysApplication.getInstance().addActivity(this); 添加当前Acitivity到ancivitylist里面去,最后在想退出的时候调用SysApplication.getInstance().exit();可直接关闭所有的Acitivity并退出应用程序。
附代码:
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;
import android.content.Intent;
public class SysApplication extends Application {
private List mList = new LinkedList();
private static SysApplication instance;
private SysApplication() {
}
public synchronized static SysApplication getInstance() {
if (null == instance) {
instance = new SysApplication();
}
return instance;
}
// add Activity
public void addActivity(Activity activity) {
mList.add(activity);
}
public void exit() {
try {
for (Activity activity : mList) {
if (activity != null)
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
public void onLowMemory() {
super.onLowMemory();
System.gc();
}
}
3.在应用程序里面 的activity的oncreate里面添加SysApplication.getInstance().addActivity(this)
如:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SysApplication.getInstance().addActivity(this);
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Exit");
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
System.exit(0);
return true;
}
});
return true;
}
2.这样太不方便,每次退出界面之后应用程序还在运行,
网上提供了几种方法,将下面SysApplication这个类复制到工程里面,然后在每个Acitivity的oncreate方法里面通过SysApplication.getInstance().addActivity(this); 添加当前Acitivity到ancivitylist里面去,最后在想退出的时候调用SysApplication.getInstance().exit();可直接关闭所有的Acitivity并退出应用程序。
附代码:
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;
import android.content.Intent;
public class SysApplication extends Application {
private List mList = new LinkedList();
private static SysApplication instance;
private SysApplication() {
}
public synchronized static SysApplication getInstance() {
if (null == instance) {
instance = new SysApplication();
}
return instance;
}
// add Activity
public void addActivity(Activity activity) {
mList.add(activity);
}
public void exit() {
try {
for (Activity activity : mList) {
if (activity != null)
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
public void onLowMemory() {
super.onLowMemory();
System.gc();
}
}
3.在应用程序里面 的activity的oncreate里面添加SysApplication.getInstance().addActivity(this)
如:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SysApplication.getInstance().addActivity(this);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询