|
在一个android小应用开发过程当中,免不了要调用浏览器,或者指定某个网页必须调用某个浏览器的情况,由于是新手,不知道有没有这种函数已经可以直接调用,在时间的压逼下,只能自己想办法,当然,最原始的方法就是用网上半成熟的代码,完成自己想要的功能,我暂时要的是结果,而不是过程。我要实现的效果是点击小应用里的某个按钮,然后弹出一个浏览器选择框,选中那一个浏览器类型,就指定那一个浏览器对某个网址进行打开.
代码实现的效果如下:
实现的代码如下,由于本人真机环境的原因,部分浏览器还没有经过测试:
引用文件:
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import java.util.Arrays;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.app.AlertDialog;
具体实现代码:
private String ucPath = "", operaPath = "", qqPath = "", dolphinPath = "", skyfirePath = "", steelPath = "", GooglePath = "";
boolean existUC = false, existOpera = false, existQQ = false, existDolphin = false, existSkyfire = false, existSteel = false, existGoogle = false;
private String url = "";
private int mSingleChoiceID = 0;
//要访问的网址
private String visitUrl = "http://www.XXXXXXX.com";
url = visitUrl;
//-------------------------------浏览器选择框完整代码-----------------------------------------------
private void choiceBrowserToVisitUrl() {
PackageManager packageMgr = getPackageManager();
List<ackageInfo> list = packageMgr.getInstalledPackages(0);
//先定义一个浏览器,后面动态创建浏览器数组的个数
String[] A = new String[1];
int iCount = 1;//记录浏览器个数
A = (String[])resizeArray(A,iCount);
A[iCount-1] = "默认浏览器";
for (int i = 0; i < list.size(); i++) {
PackageInfo info = list.get(i);
String temp = info.packageName;
if (temp.equals("com.uc.browser")) {
// 存在UC
ucPath = temp;
iCount = iCount + 1;
A = (String[])resizeArray(A,iCount);
A[iCount-1] = "UC浏览器";
} else if (temp.equals("com.tencent.mtt")) {
// 存在QQ
qqPath = temp;
iCount = iCount + 1;
A = (String[])resizeArray(A,iCount);
A[iCount-1] = "QQ浏览器";
} else if (temp.equals("com.opera.mini.android")) {
// 存在Opera
operaPath = temp;
iCount = iCount + 1;
A = (String[])resizeArray(A,iCount);
A[iCount-1] = "OPERA浏览器";
} else if (temp.equals("mobi.mgeek.TunnyBrowser")) {
dolphinPath = temp;
iCount = iCount + 1;
A = (String[])resizeArray(A,iCount);
A[iCount-1] = "DOLPHIN浏览器";
} else if (temp.equals("com.skyfire.browser")) {
skyfirePath = temp;
iCount = iCount + 1;
A = (String[])resizeArray(A,iCount);
A[iCount-1] = "SKYFIRE浏览器";
} else if (temp.equals("com.kolbysoft.steel")) {
steelPath = temp;
iCount = iCount + 1;
A = (String[])resizeArray(A,iCount);
A[iCount-1] = "STEEL浏览器";
} else if (temp.equals("com.android.browser")) {
// 存在GoogleBroser
googlePath = temp;
iCount = iCount + 1;
A = (String[])resizeArray(A,iCount);
A[iCount-1] = "Google浏览器";
}
}
final String[] B = new String[iCount];
System.arraycopy(A, 0, B, 0, iCount);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("选择浏览器");
builder.setSingleChoiceItems(A, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mSingleChoiceID = whichButton;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
if (B[mSingleChoiceID].equals("UC浏览器"))
{
existUC = true;
doShowBrowse();
}
else if (B[mSingleChoiceID].equals("QQ浏览器"))
{
existQQ = true;
doShowBrowse();
}
else if (B[mSingleChoiceID].equals("OPERA浏览器"))
{
existOpera = true;
doShowBrowse();
}
else if (B[mSingleChoiceID].equals("DOLPHIN浏览器"))
{
existDolphin = true;
doShowBrowse();
}
else if (B[mSingleChoiceID].equals("SKYFIRE浏览器"))
{
existSkyfire = true;
doShowBrowse();
}
else if (B[mSingleChoiceID].equals("TEEL浏览器"))
{
existSteel = true;
doShowBrowse();
}
else if (B[mSingleChoiceID].equals("Google浏览器"))
{
existGoogle = true;
doShowBrowse();
}
}
});
builder.create().show();
}
private void doShowBrowse(){
// 单击事件对象的实例
PackageManager packageMgr1 = getPackageManager();
if (existUC) {
gotoUrl(ucPath, url, packageMgr1);
} else if (existOpera) {
gotoUrl(operaPath, url, packageMgr1);
} else if (existQQ) {
gotoUrl(qqPath, url, packageMgr1);
} else if (existDolphin) {
gotoUrl(dolphinPath, url, packageMgr1);
} else if (existSkyfire) {
gotoUrl(skyfirePath, url, packageMgr1);
} else if (existSteel) {
gotoUrl(steelPath, url, packageMgr1);
} else if (existGoogle) {
gotoUrl(googlePath, url, packageMgr1);
} else {
doDefault();
}
}
/** * Reallocates an array with a new size, and copies the contents
* * of the old array to the new array.
* * @param oldArray the old array, to be reallocated.
* * @param newSize the new array size.
* * @return A new array with the same contents.
* */
private static Object resizeArray (Object oldArray, int newSize) {
int oldSize = java.lang.reflect.Array.getLength(oldArray);
Class elementType = oldArray.getClass().getComponentType();
Object newArray = java.lang.reflect.Array.newInstance(
elementType,newSize);
int preserveLength = Math.min(oldSize,newSize);
if (preserveLength > 0)
System.arraycopy (oldArray,0,newArray,0,preserveLength);
return newArray; }
// Test routine for resizeArray
private void gotoUrl(String packageName, String url,
PackageManager packageMgr) {
try {
Intent intent;
intent = packageMgr.getLaunchIntentForPackage(packageName);
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse(url));
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
private void doDefault() {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(visitUrl));
startActivity(intent);
} |
|