1. 程式人生 > >Android 常用判斷方法--不斷補充

Android 常用判斷方法--不斷補充

/** 
     * 判斷當前介面是否是桌面 
     */  
    private boolean isHome() {  
        ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  
        List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);  
        return getHomes().contains(rti.get(0).topActivity.getPackageName());  
    }  
/** 
     * 獲得屬於桌面的應用的應用包名稱 
     *  
     * @return 返回包含所有包名的字串列表 
     */  
    private List<String> getHomes() {  
        List<String> names = new ArrayList<String>();  
        PackageManager packageManager = this.getPackageManager();  
        Intent intent = new Intent(Intent.ACTION_MAIN);  
        intent.addCategory(Intent.CATEGORY_HOME);  
        List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,  
                PackageManager.MATCH_DEFAULT_ONLY);  
        for
(ResolveInfo ri : resolveInfo) { names.add(ri.activityInfo.packageName); } return names; }