此方法其实不是主要是屏蔽Keycode_Back,让它不结束(finish())Activity,直接显示HOME界面。
Java代码
PackageManager pm = getPackageManager();
ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME), 0);
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);
}
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);
}
Java代码
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);
}
}
Java代码
PackageManager pm = getPackageManager();
ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME), 0);
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);
}
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);
}
Java代码
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);
}
}