由于app避免不了,系统更新。和内部下载。
本人也是应公司需求去写一个,但是由于自己技术不扎实所有写个简书记录下, 我直接上代码。让我讲原理不太可行。所以见谅。
/**
* 下载Apk 安装Apk 打开APK
*
*@authorAdministrator
*/
public classMainActivityextendsActivity {
privateButtonbutton1;
private static finalStringURL_STRING="http://gdown.baidu.com/data/wisegame/b7d7e4efd8199dea/tianyiyuedu_310.apk";
private static intdown=0;
Filefile;
privateHandlerhandler=newHandler() {
@Override
public voidhandleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what) {
case1:
button1.setText("点击安装");
down=1;
break;
case2:
down=2;
button1.setText("打开");
break;
}
}
};
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1= (Button) findViewById(R.id.btn);
button1.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
// 下载apk
if(down==0) {
downloadApk();
button1.setText("正在下载");
// 安装APK
}else if(down==1) {
installApk();
// 打开apk
}else if(down==2) {
openApk(this,URL_STRING);
}
}
});
/**注册下载完成广播**/
registerReceiver(downloadCompleteReceiver, newIntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
/**
* 下载APK
**/
private voiddownloadApk() {
// String apkUrl = "http://192.168.1.1/downloadtest.apk";
Uri uri = Uri.parse(URL_STRING);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request =newDownloadManager.Request(uri);
// 设置允许使用的网络类型,这里是移动网络和wifi都可以
request.setAllowedNetworkTypes(request.NETWORK_MOBILE| request.NETWORK_WIFI);
//设置是否允许漫游
request.setAllowedOverRoaming(false);
//设置文件类型
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(URL_STRING));
request.setMimeType(mimeString);
//在通知栏中显示
request.setNotificationVisibility(request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("download...");
request.setVisibleInDownloadsUi(true);
//sdcard目录下的download文件夹
request.setDestinationInExternalPublicDir("/download","downloadtest.apk");
// 将下载请求放入队列
downloadManager.enqueue(request);
Message message =handler.obtainMessage();
message.what=1;
handler.sendMessage(message);
}
privateBroadcastReceiverdownloadCompleteReceiver=newBroadcastReceiver() {
@Override
public voidonReceive(Context context,Intent intent) {
/**下载完成后安装APK**/
installApk();
}
};
private voidinstallApk() {
Intent i =newIntent(Intent.ACTION_VIEW);
String filePath ="/sdcard/download/downloadtest.apk";
Message message =handler.obtainMessage();
message.what=2;
handler.sendMessage(message);
i.setDataAndType(Uri.parse("file://"+ filePath),"application/vnd.android.package-archive");
startActivity(i);
}
/**
* 打开已经安装好的apk
*/
private voidopenApk(View.OnClickListener context,String url) {
PackageManager manager = getPackageManager();
// 这里的是你下载好的文件路径
PackageInfo info = manager.getPackageArchiveInfo(Environment.getExternalStorageDirectory().getAbsolutePath() + getFilePath(url),PackageManager.GET_ACTIVITIES);
if(info !=null) {
Intent intent = manager.getLaunchIntentForPackage(info.applicationInfo.packageName);
startActivity(intent);
}
}
/**
* 根据传过来url创建文件
*/
privateFilegetFile(String url) {
File files =newFile(Environment.getExternalStorageDirectory().getAbsoluteFile(),getFilePath(url));
returnfiles;
}
/**
* 截取出url后面的apk的文件名
*
*@paramurl
*@return
*/
privateStringgetFilePath(String url) {
returnurl.substring(url.lastIndexOf("/"),url.length());
}
}
直接粘贴过去。 格式化就能用。 布局就是一个button 没必要粘贴上来