Android 实现Activity后台运行
2013-11-20 17:23
441 查看
第一种方法
Java代码
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
第二种方法
此方法其实不是主要是屏蔽Keycode_Back,让它不结束(finish())Activity,直接显示HOME界面。
Java代码
PackageManager pm = getPackageManager();
ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME), 0);
Java代码
public boolean
onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
ActivityInfo ai = homeInfo.activityInfo;
Intent startIntent = new Intent(Intent.ACTION_MAIN);
startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startIntent.setComponent(new ComponentName(ai.packageName,
ai.name));
startActivitySafely(startIntent);
return true;
} else
return super.onKeyDown(keyCode, event);
}
[b]Java代码
[/b]
[b]void
startActivitySafely(Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.unabletoopensoftware,
Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
Toast.makeText(this, R.string.unabletoopensoftware,
Toast.LENGTH_SHORT).show();
Log
.e(
TAG,
"Launcher does not have the permission to launch "
+ intent
+ ". Make sure to create a MAIN intent-filter for the corresponding activity "
+ "or use the exported attribute for this activity.",
e);
}
}
[/b]
Java代码
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
第二种方法
此方法其实不是主要是屏蔽Keycode_Back,让它不结束(finish())Activity,直接显示HOME界面。
Java代码
PackageManager pm = getPackageManager();
ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME), 0);
Java代码
public boolean
onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
ActivityInfo ai = homeInfo.activityInfo;
Intent startIntent = new Intent(Intent.ACTION_MAIN);
startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startIntent.setComponent(new ComponentName(ai.packageName,
ai.name));
startActivitySafely(startIntent);
return true;
} else
return super.onKeyDown(keyCode, event);
}
[b]Java代码
[/b]
[b]void
startActivitySafely(Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.unabletoopensoftware,
Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
Toast.makeText(this, R.string.unabletoopensoftware,
Toast.LENGTH_SHORT).show();
Log
.e(
TAG,
"Launcher does not have the permission to launch "
+ intent
+ ". Make sure to create a MAIN intent-filter for the corresponding activity "
+ "or use the exported attribute for this activity.",
e);
}
}
[/b]
相关文章推荐
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- Android 实现Activity后台运行
- Android 实现Activity后台运行
- Android 实现Activity后台运行
- Android 实现Activity后台运行
- Android 实现Activity后台运行
- Android 实现后台运行Activity的方法
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- Android 实现Activity后台运行
- Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”
- Android 实现Activity后台运行
- Android按返回键程序仍在后台运行,并未完全退出和销毁,类似微信QQ等退出方式的实现-- moveTaskToBack
- android上app后台运行的相关知识(一)————android的四大组件:Activity、Service、BroadCast Recevicer、Content provider
- android返回主页时,Activity在后台继续运行的方法
- Android 实现应用程序最小化到后台运行
- android实现程序的后台运行
- android 实现应用程序后台运行的说明