首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

监测另一个程序起步或更换整个theme的方法

2012-09-25 
监测另一个程序启动或更换整个theme的方法这两个方法主要都用到了ActivityManager am (ActivityManager)

监测另一个程序启动或更换整个theme的方法
这两个方法主要都用到了ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);这个的使用
对于更换整个手机的主题 下面的代码没有调试过,只是从别处摘过来主要是学习里面api的用法很有借鉴意义:
PackageManager manager = getPackageManager();
Resources myResources;
Resources.Theme myTheme = null;
List<ApplicationInfo> apps = manager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo app : apps) {  
   app.theme=android.R.style.Theme_Light_NoTitleBar_Fullscreen;
   myTheme = myResources.newTheme();  
   myTheme.applyStyle(app.theme, true);
   }

而对与监测一个程序的启动
protected void onPause() {  
    if (isApplicationBroughtToBackground()) {      
     ...   
     }
     }
public static boolean isApplicationBroughtToBackground(Context context) {
               ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                      List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                             if (!taskInfo.isEmpty()) {
                                         ComponentName topActivity = taskInfo.get(0).topActivity;
                                                     if (!topActivity.getPackageName().equals(context.getPackageName())) {
                                                                     return true;
                                                                                }
                                                         }
                                                                     
                                                 return false;
                                       }
         }
通过上面的方法就可以监测到 你当前的应用程序是不是被新发起的程序取代
android.permission.GET_TASKS添加 permission 在你的主manifest.xml中

热点排行