android应用实例之默认浏览器检测

本文主要使用PackageManager相关API来检索系统默认的浏览器是哪一个,请参考。
package com.example.testdefaultbrowser;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    
    Context mContext;
    private final String VIEW_LOG_TAG = "LINHUI_DEBUG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = getBaseContext();
        testGetDefaultActivity();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void testGetDefaultActivity() {
        PackageManager pm = mContext.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.google.com"));
        ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        Log.i(VIEW_LOG_TAG, "getDefaultActivity info = " + info + ";pkgName = " + info.activityInfo.packageName);
        TextView tv = (TextView) findViewById(R.id.result);
        tv.setTextColor(Color.rgb(255, 0, 0));
        tv.setText("Default Browser Name:"+info.activityInfo.packageName);

        if (info.activityInfo.packageName != "android") {
            // Use the destination package name
            
            String pkgName = "package:"+info.activityInfo.packageName;
            Intent intent_to = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                    Uri.parse(pkgName));
            startActivity(intent_to);
        }
    }
}
posted @ 2018-07-09 16:07  mail181  阅读(17)  评论(0编辑  收藏  举报