Android程序中安装其它软件包
Android 程序中安装其它软件包
#移动开发 #Android
1.介绍
应用程序中有时需要调用其它软件,但有时用户没有安装需要调用的软件,为方便用户使用,以下例程实现了半自动化的引导用户安装。
- 调用的几种可能性
被调用的软件包已经存在
用户设备存在应用市场程序(如 google market, 掌上应用汇等)
软件安装是否成功(在市场中能否找到软件包,调用市场软件时发生异常)
2.例程
功能
判断文件管理器是否存在,如果不存在,则引导用户从应用市场安装关键字
应用中,安装,其它软件,apk可从此处下载可独立运行的代码
[http://download.csdn.net/detail/xieyan0811/4117459
](http://download.csdn.net/detail/xieyan0811/4117459)
- 核心代码及说明
package com.demo.apk;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.net.Uri;
import android.util.Log;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
import android.content.DialogInterface;
import java.util.List;
import java.util.ArrayList;
public class MyApkActivity extends Activity {
private String TAG ="demo";
private static finalString APK_NAME = "lysesoft.andexplorer";
private static finalint REQUEST_INSTALL = 1;
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button= new Button(this);
button.setText("test" + APK_NAME);
button.setOnClickListener(newView.OnClickListener() {
publicvoid onClick(View v) {
if(apkExists()) // 软件包中否存在
showExists(); // 提示用户已存在
elseif (!hasMarket()) // 是否有手机市场软件
showWithoutMarket(); // 提示用户无手机市场可用
else
runInstall(); // 安装软件
}
});
setContentView(button);
}
private booleanapkExists() {
PackageManagerpManager = getPackageManager();
List
for (int i =0; i < packageInfoList.size(); i++) {
PackageInfopkg = packageInfoList.get(i);
Log.d(TAG,"pkg name " + pkg.packageName);
if(pkg.packageName.equals(APK_NAME))
returntrue;
}
return false;
}
public booleanhasMarket() {
List
Uri uri =Uri.parse("market://search?q=pname:" + APK_NAME);
Intent intent= new Intent(Intent.ACTION_VIEW, uri);
PackageManagerpManager = getPackageManager();
mApps =pManager.queryIntentActivities(intent,
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
Log.d(TAG,"market count " + mApps.size());
if(mApps.size() > 0)
returntrue;
else
returnfalse;
}
private voidshowWithoutMarket() {
AlertDialog.Builderdialog = new AlertDialog.Builder(this);
dialog.setMessage("withoutmarket on smartphone");
dialog.setNegativeButton(android.R.string.cancel,null);
dialog.show();
}
private voidshowExists() {
AlertDialog.Builderdialog = new AlertDialog.Builder(this);
dialog.setMessage("apkalready exists");
dialog.setNegativeButton(android.R.string.cancel,null);
dialog.show();
}
private voidrunInstall() {
Uri uri = Uri.parse("market://search?q=pname:"+ APK_NAME);
Intent it =new Intent(Intent.ACTION_VIEW, uri);
startActivityForResult(it,REQUEST_INSTALL);
}
protected voidonActivityResult(int requestCode, int resultCode,
Intentintent) { // 提示用户如果在市场上找不到软件的解决方法
if(requestCode == REQUEST_INSTALL) {
Log.d(TAG,"resultCode " + resultCode);
Toast.makeText(MyApkActivity.this,
"ifapk can't found in market, please download by PC",
Toast.LENGTH_LONG).show();
}
}
};
(转载请注明出处)