继续学习Android,我读了以下内容:
Question: Does the user have a choice to kill the application
unless we put a menu option in to kill it? If no such option exists,
how does the user terminate the application?
Answer: (Romain Guy): The user doesn't, the system handles this automatically. That's what the activity lifecycle (especially onPause/onStop/onDestroy) is for. No matter what you do, do not put a "quit" or "exit" application button. It is useless with Android's application model. This is also contrary to how core applications work.
呵呵,我在Android世界里每走一步都会遇到一些问题=(
显然,你不能在Android中退出应用程序(但Android系统可以随时完全销毁你的应用程序)。这是怎么回事?我开始认为这是不可能写一个应用程序的功能作为“正常的应用程序”-用户可以退出应用程序时,他/她决定这么做。这不应该依靠操作系统来完成。
我正在尝试创建的应用程序不是Android市场的应用程序。它不是一个被大众“广泛使用”的应用程序,它是一个将在非常狭窄的商业领域使用的商业应用程序。
我其实很期待为Android平台开发,因为它解决了Windows Mobile和。net中存在的许多问题。然而,上周对我来说有点令人失望……我希望我不必放弃Android,但它现在看起来不太好=(
有办法让我真的退出应用程序吗?
目前,我在我的应用程序中实现了以下内容。可能这些有助于从应用程序中移动出来,无论你想要的是什么。我从操作栏菜单中调用这个函数。
public static void exitApplication(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
exitApplicationHC(context);
}
else {
exitApplicationPreHC(context);
}
}
private static void exitApplicationPreHC(Context context) {
Intent i = new Intent(context, LoginActivity.class);
i.putExtra(EXTRA_EXIT, true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
if (context instanceof Activity) {
((Activity) context).finish();
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static void exitApplicationHC(Context context) {
Intent i = new Intent(context, LoginActivity.class);
i.putExtra(EXTRA_EXIT, true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(i);
}
我认为关键是没有必要退出应用程序,除非你的软件有问题。Android会在用户不使用该应用且设备需要更多内存时退出该应用。如果你有一个需要在后台运行服务的应用程序,你可能需要一种方法来关闭服务。
For example, Google Listen continues to play podcast when the app is not visible. But there is always the pause button to turn the podcast off when the user is done with it. If I remember correctly, Listen, even puts a shortcut in the notification bar so you can always get to the pause button quickly. Another example is an app like a twitter app for instance which constantly polls a service on the internet. These types of apps should really allow the user to choose how often to poll the server, or whether even to poll in a background thread.
如果你需要在退出时运行的代码,你可以根据需要重写onPause(), onStop()或onDestroy()。
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
如果你有10个,20个…有多个活动正在运行,你想要完成所有活动并退出系统。
在应用程序类或常量类中创建静态数组。
常量
public class Constants {
public static ArrayList<Activity> activities = new ArrayList<Activity>();
}
在此数组中添加当前活动引用
activity = MainActivity.this;
Constants.activities.add(活动);
public class MainActivity extends Activity {
private ImageView imageButton;
private Activity activity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = MainActivity.this;
Constants.activities.add(activity);
imageButton = (ImageView) findViewById(R.id.camera);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// existing app.
if (Constants.activities != null) {
for (int i = 0; i < Constants.activities.size(); i++) {
Activity s = Constants.activities.get(i);
s.finish();
}
}
//super.finish();
finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
}
}
另一个选项可以是Android辅助服务,绿色应用程序正在使用强制关闭应用程序来加速内存。有了你的应用程序辅助服务访问,你可以点击按钮,基本上Greenify应用程序点击强制关闭按钮,在应用程序的设置中发现:
在这里你可以学习无障碍服务:
https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html
下面是可访问性服务以编程方式点击的设置按钮:
所以你可以通过以下步骤实现杀死任何应用程序,包括你的:
1)登记无障碍服务申请
2)根据您的要求,如果您想杀死所有应用程序获取列表的所有包
3)导航到他们的设置屏幕并单击强制关闭按钮
这是它。我可以分享一个示例代码,我还创建了一个应用程序,如greenify作为家庭作业。
谢谢你!
更新:
“用户不需要,系统会自动处理。”
所以基本上,通过这个解决方案,我们间接地使用了系统强制关闭,但基于用户需求。这样双方都能保持快乐:-)