?
第一種方法
?
- Intent?intent?=? new ?Intent(Intent.ACTION_MAIN);??
- intent.addCategory(Intent.CATEGORY_HOME);??
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);??
- startActivity(intent);??
?
?
第二種方法
?
此方法其實不是主要是屏蔽Keycode_Back,讓它不結(jié)束(finish())Activity,直接顯示HOME界面。
?
- PackageManager?pm?=?getPackageManager();??
- ??????????????????????????????ResolveInfo?homeInfo?=?pm.resolveActivity( new ?Intent(Intent.ACTION_MAIN)??
- .addCategory(Intent.CATEGORY_HOME),? 0 );??
?
?
- 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);??
- }??
?
?
- 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);??
- ????}??
- }??
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
