获取浏览器的homepage

主要知识点:
跨进程访问数据

首先修改浏览器源码:
BrowserSettings.java

private static String getSDMCDefaultSharedPreferencesName(Context context) {
        return context.getPackageName() + "_preferences";
    }
    
    private BrowserSettings(Context context) {
        mContext = context.getApplicationContext();

/*sky modify just for test*/ //mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); //因为这个默认的mode是MODE_PRIVATE mPrefs = mContext.getSharedPreferences(getSDMCDefaultSharedPreferencesName(mContext), Context.MODE_WORLD_READABLE);//修改为可读 /*sky modify over*/ mAutofillHandler = new AutofillHandler(mContext); mManagedSettings = new LinkedList<WeakReference<WebSettings>>(); mCustomUserAgents = new WeakHashMap<WebSettings, String>(); mAutofillHandler.asyncLoadFromDb(); BackgroundHandler.execute(mSetup); }

然后就是跨APK访问数据了:

        Context otherAppsContext = null;
        
        try{
            otherAppsContext = context.createPackageContext("com.android.browser",0);
        }catch (Exception e){
            Log.i("BootReceiver-BootBrowser", "NameNotFoundException");
        }
        
        try{
            //com.android.browser_preferences.xml
            SharedPreferences mPrefs = otherAppsContext.getSharedPreferences("com.android.browser_preferences",Context.MODE_PRIVATE);
            String homepage = mPrefs.getString("homepage","hahahaha");
            
            Log.i("BootReceiver-BootBrowser","homepage="+homepage);
        }catch(Exception e){
            Log.i("BootReceiver-BootBrowser", "Exception#####");
        }
        
        //隐式访问浏览器APK
        Intent intent = new Intent();        
        intent.setAction("android.intent.action.VIEW");    
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //Uri content_url = Uri.parse("http://www.baidu.com");   
        Uri content_url = Uri.parse(homepage);  
        intent.setData(content_url);  
        context.startActivity(intent);        
        
        //显式访问浏览器APK
        Intent intent = new Intent();  
        //intent.addCategory(Intent.CATEGORY_LAUNCHER); 
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
        ComponentName cn = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");             
        intent.setComponent(cn);  
        
        context.startActivity(intent); 

参考:  
跨apk访问SharedPreferences   
http://www.cnblogs.com/leaven/archive/2012/07/28/2613291.html 

原文地址:https://www.cnblogs.com/lijunamneg/p/3312592.html