Android設置默認Launcher

當系統存在多個launcher時,若沒有設置默認launcher,開機啓動後會彈出提示框,羅列所有launcher,用戶選擇並設置了默認launcher後,按home鍵以及以後重啓都會進入默認的launcher。

現在,我希望系統能直接就進入我設定的launcher而不是彈出框後選擇然後設置


網上大部分的做法就是修改

packages/apps/Provision/src/com/android/DefaultActivity.java

frameworks/base/services/java/com/android/server/pm/PackageManagerService.java


兩個文件,給個相對比較好看一點的鏈接http://blog.csdn.net/z_guijin/article/details/8964890

我按照這個做不能達到預期的效果,


/////////////////////////////////////////添加內容////////////////////////////////////////

後期修改包名,發現開機自啓動出現問題,然後弄了兩天,終於差不多弄清楚了。果然,欠下的債遲早得還!

其實修改DefaultActivity.java就能夠完成開機自啓動,只是當時我的程序有問題,所以沒有達到預期效果


當然下面修改ActivityManagerService.java也能完成開機自啓動


這兩者的區別在於
DefaultActivity.java只是在第一次啓動時執行,如果修改了默認launcher後不可恢復

ActivityManagerService.java在每次啓動時執行,每次都默認啓動設定的launcher,當然,如果設定的launcher存在的話,設置其他的launcher爲默認會無效,因爲重新啓動時setDefaultLauncher()會將當前默認launcher清除。只有在卸載了設定默認啓動的launcher後才能設置其他launcher爲默認啓動.

//////////////////////////////////////////////////////////////////////////////////////////////////////////


修改多次搜索關鍵字,得到另一篇文章,大致看了下,感覺不會有效,但是已經絕望了就試了試,竟然解決了問題http://blog.csdn.net/jia4525036/article/details/18036765

這篇文章有借鑑之處,直接使用時會發現有些字段是上下文中沒有的,故寫下此文記錄一下。


修改文件

frameworks\base\services\java\com\android\server\am\ActivityManagerService.java

添加一個方法:

	private void setDefaultLauncher() {  
        // get default component   
        String packageName = "com.coship.factorytest";//默認launcher包名
        String className = "com.coship.factorytest.MainActivity";////默認launcher入口
         
		IPackageManager pm = ActivityThread.getPackageManager();
		
		//判斷指定的launcher是否存在
		if(hasApkInstalled(packageName)) {
		
			Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className); 
					  
			//清除當前默認launcher 
			ArrayList<IntentFilter> intentList = new ArrayList<IntentFilter>();  
			ArrayList<ComponentName> cnList = new ArrayList<ComponentName>();  
			mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);  
			IntentFilter dhIF = null;  
			for(int i = 0; i < cnList.size(); i++) {  
				dhIF = intentList.get(i);  
				if(dhIF.hasAction(Intent.ACTION_MAIN) && dhIF.hasCategory(Intent.CATEGORY_HOME)) {  
					mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());  
				}  
			}  
					  
			//獲取所有launcher activity 
			Intent intent = new Intent(Intent.ACTION_MAIN);  
			intent.addCategory(Intent.CATEGORY_HOME);  
			List<ResolveInfo> list = new ArrayList<ResolveInfo>();  
			try {  
				list = pm.queryIntentActivities(intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), PackageManager.MATCH_DEFAULT_ONLY);  
			}catch (RemoteException e) {  
				throw new RuntimeException("Package manager has died", e);  
			}   
			// get all components and the best match  
			IntentFilter filter = new IntentFilter();  
			filter.addAction(Intent.ACTION_MAIN);  
			filter.addCategory(Intent.CATEGORY_HOME);  
			filter.addCategory(Intent.CATEGORY_DEFAULT);  
			final int N = list.size();  
			Slog.d(TAG, "N:::::hyhyhyhy:::: = " + N);  
					
			//設置默認launcher 
			ComponentName launcher = new ComponentName(packageName, className);  

			ComponentName[] set = new ComponentName[N];  
			int defaultMatch = 0;  
			for (int i = 0; i < N; i++) {  
				ResolveInfo r = list.get(i);  
				set[i] = new ComponentName(r.activityInfo.packageName, r.activityInfo.name);  
				Slog.d(TAG, "r.activityInfo.packageName:::::hyhyhyhy:::: = " + r.activityInfo.packageName);
				Slog.d(TAG, "r.activityInfo.name:::::hyhyhyhy:::: = " + r.activityInfo.name);
				if(launcher.getClassName().equals(r.activityInfo.name)) {
					defaultMatch = r.match;
				}
			}  
					
			try {  
				pm.addPreferredActivity(filter, defaultMatch, set, launcher);
			} catch (RemoteException e) {  
				throw new RuntimeException("com.coship.factorytest.MainActivity : Package manager has died", e);  
			}   
			
			
		}//end if
		
    }  

	private static boolean hasApkInstalled(String pkgname) {

	    try {
	        mSelf.mContext.getPackageManager().getPackageInfo(pkgname,0);
	    } catch(Exception e) {
			Slog.d(TAG, "PackageManager.NameNotFoundException: = " + e.getMessage());
	        return false;
	    }
	    return true;
	}

然後在ActivityManagerService類中的

boolean startHomeActivityLocked()

方法第一行調用上面添加的

setDefaultLauncher()
  boolean startHomeActivityLocked() {
	
        if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
                && mTopAction == null) {
            // We are running in factory test mode, but unable to find
            // the factory test app, so just sit around displaying the
            // error message and don't try to start anything.
            return false;
        }

///////////////////////////////////////////
		setDefaultLauncher();
///////////////////////////////////////////
		
        Intent intent = new Intent(
            mTopAction,
            mTopData != null ? Uri.parse(mTopData) : null);
        intent.setComponent(mTopComponent);
        if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
            intent.addCategory(Intent.CATEGORY_HOME);
        }
        ActivityInfo aInfo =
            intent.resolveActivityInfo(mContext.getPackageManager(),
                    STOCK_PM_FLAGS);
        if (aInfo != null) {
            intent.setComponent(new ComponentName(
                    aInfo.applicationInfo.packageName, aInfo.name));
            // Don't do this if the home app is currently being
            // instrumented.
            Proce***ecord app = getProce***ecordLocked(aInfo.processName,
                    aInfo.applicationInfo.uid);
            if (app == null || app.instrumentationClass == null) {
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo,
                        null, null, 0, 0, 0, false, false, null);
            }
        }
        
        
        return true;
    }

添加後的方法全部內容如上,重新編譯android,燒錄,開機就能夠自動進入自定義的launcher

可以通過系統設置取消該launcher的默認設置,取消之後按home鍵會彈出launcher選擇提示框

frameworks\base\core\java\com\android\internal\app\ResolverActivity.java

ResolverActivity類就是選擇打開方式的彈出框

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章