Android(安卓)实现Activity后台运行
16lz
2022-06-13
Android 实现Activity后台运行
- 博客分类:
- android
第一种方法
Java代码- Intentintent=newIntent(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_HOME);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent);
第二种方法
此方法其实不是主要是屏蔽Keycode_Back,让它不结束(finish())Activity,直接显示HOME界面。
Java代码- PackageManagerpm=getPackageManager();
- ResolveInfohomeInfo=pm.resolveActivity(newIntent(Intent.ACTION_MAIN)
- .addCategory(Intent.CATEGORY_HOME),0);
- publicbooleanonKeyDown(intkeyCode,KeyEventevent){
- if(keyCode==KeyEvent.KEYCODE_BACK){
- ActivityInfoai=homeInfo.activityInfo;
- IntentstartIntent=newIntent(Intent.ACTION_MAIN);
- startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
- startIntent.setComponent(newComponentName(ai.packageName,
- ai.name));
- startActivitySafely(startIntent);
- returntrue;
- }else
- returnsuper.onKeyDown(keyCode,event);
- }
- voidstartActivitySafely(Intentintent){
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- try{
- startActivity(intent);
- }catch(ActivityNotFoundExceptione){
- Toast.makeText(this,R.string.unabletoopensoftware,
- Toast.LENGTH_SHORT).show();
- }catch(SecurityExceptione){
- Toast.makeText(this,R.string.unabletoopensoftware,
- Toast.LENGTH_SHORT).show();
- Log
- .e(
- TAG,
- "Launcherdoesnothavethepermissiontolaunch"
- +intent
- +".MakesuretocreateaMAINintent-filterforthecorrespondingactivity"
- +"orusetheexportedattributeforthisactivity.",
- e);
- }
- }
更多相关文章
- Android任意时刻、任意代码处全屏
- :app:uploadCrashlyticsMappingFileRelease
- Android(安卓)内核源代码交叉编译纺
- 给动态生成的View添加水波纹效果
- android dialog home键
- android 创建菜单列表及其事件
- Android(安卓)SearchView结合Filter实现列表检索功能使用总结
- Android锁屏控制
- Android下MP3播放器的实现源代码01