Android中,退出应用程序的方式:
1.通过pid
int pid = android.os.Process.myPid() //获取当前应用程烂逗序的PID
android.os.Process.killProcess(pid) //杀死当前进程
这种方法退出应用,是会保留某些后进程,例如:Service,Notifications等。
2.通并历橘过ActivityManager
ActivityManager manager = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE)//获取应用程序管理器
manager.killBackgroundProcesses(getPackageName())//强制结束当前应用程序
这种方式退出应用,会结束本应用程序的一切活动,因为本方法会根据应用程序的包名杀死所有进绝团程包括Activity,Service,Notifications等。
据我所知,答案是No,没有这样的broadcast,你枣配没法用receiver去监听。唯答毕一的办法就是后台查询。整个后台线程清岩芹,随时去遍历
// Get currently running application processes
List<ActivityManager.RunningAppProcessInfo>list = servMng.getRunningAppProcesses()
if(list != null){
for(int i=0i<list.size()++i){
if("com.android.email".matches(list.get(i).processName)){
int pid = android.os.Process.getUidForName("com.android.email")
android.os.Process.killProcess(pid)
}else{
mTextVIew.append(list.get(i).processName + "\n")
}
}
}
基本思路是让app中所有的activity继承与一个公共的activity(例如:BaseActivity),然后在BaseActivity的onStop()中判断当前程序是否处于后台[mw_shl_code=java,true]/**
package com.wadata.mobilefollowup.view.base
import java.util.List
import android.app.Activity
import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo
import android.content.Context
public class BaseActivity extends Activity {
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop()
if (!isAppOnForeground()) {
/氏余侍/app 进入后台
//全局变量isActive = false 记录当前已经进入后台
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume()
//if (!isActive) {
//app 从后台唤醒,进入前台
//isActive = true
//}
}
/**
* 程序毁清是否在前台运行
*
* @return
*/
public boolean isAppOnForeground() {
// Returns a list of application processes that are running on the
//歼吵 device
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)
String packageName = getApplicationContext().getPackageName()
List<RunningAppProcessInfo>appProcesses = activityManager
.getRunningAppProcesses()
if (appProcesses == null)
return false
for (RunningAppProcessInfo appProcess : appProcesses) {
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName)
&&appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true
}
}
return false
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)