读书人

Android 兑现Activity后台运行(转)

发布时间: 2012-08-14 10:39:57 作者: rapoo

Android 实现Activity后台运行(转)

?

Android 实现Activity后台运行博客分类:?
  • androidAndroid

    第一种方法

    ?

    Java代码??Android  兑现Activity后台运行(转)
    1. Intent?intent?=?new?Intent(Intent.ACTION_MAIN);??
    2. intent.addCategory(Intent.CATEGORY_HOME);??
    3. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);??
    4. startActivity(intent);??

    ?

    ?

    第二种方法

    ?

    此方法其实不是主要是屏蔽Keycode_Back,让它不结束(finish())Activity,直接显示HOME界面。

    ?

    Java代码??Android  兑现Activity后台运行(转)
    1. PackageManager?pm?=?getPackageManager();??
    2. ??????????????????????????????ResolveInfo?homeInfo?=?pm.resolveActivity(new?Intent(Intent.ACTION_MAIN)??
    3. .addCategory(Intent.CATEGORY_HOME),?0);??

    ?

    ?

    Java代码??Android  兑现Activity后台运行(转)
    1. public?boolean?onKeyDown(int?keyCode,?KeyEvent?event)?{??
    2. ????if?(keyCode?==?KeyEvent.KEYCODE_BACK)?{??
    3. ????????ActivityInfo?ai?=?homeInfo.activityInfo;??
    4. ????????Intent?startIntent?=?new?Intent(Intent.ACTION_MAIN);??
    5. ????????startIntent.addCategory(Intent.CATEGORY_LAUNCHER);??
    6. ????????startIntent.setComponent(new?ComponentName(ai.packageName,??
    7. ????????????????ai.name));??
    8. ????????startActivitySafely(startIntent);??
    9. ????????return?true;??
    10. ????}?else??
    11. ????????return?super.onKeyDown(keyCode,?event);??
    12. }??

    ?

    ?

    Java代码??Android  兑现Activity后台运行(转)
    1. void?startActivitySafely(Intent?intent)?{??
    2. ????intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);??
    3. ????try?{??
    4. ????????startActivity(intent);??
    5. ????}?catch?(ActivityNotFoundException?e)?{??
    6. ????????Toast.makeText(this,?R.string.unabletoopensoftware,??
    7. ????????????????Toast.LENGTH_SHORT).show();??
    8. ????}?catch?(SecurityException?e)?{??
    9. ????????Toast.makeText(this,?R.string.unabletoopensoftware,??
    10. ????????????????Toast.LENGTH_SHORT).show();??
    11. ????????Log??
    12. ????????????????.e(??
    13. ????????????????????????TAG,??
    14. ????????????????????????"Launcher?does?not?have?the?permission?to?launch?"??
    15. ????????????????????????????????+?intent??
    16. ????????????????????????????????+?".?Make?sure?to?create?a?MAIN?intent-filter?for?the?corresponding?activity?"??
    17. ????????????????????????????????+?"or?use?the?exported?attribute?for?this?activity.",??
    18. ????????????????????????e);??
    19. ????}??
    20. }??

    ?

  • 读书人网 >Android

    热点推荐