android的安装和卸载
16lz
2022-07-25
安装:
Java代码- Stringstr="/CanavaCancel.apk";
- StringfileName=Environment.getExternalStorageDirectory()+str;
- Intentintent=newIntent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(newFile(fileName)),"application/vnd.android.package-archive");
- startActivity(intent);
卸载:
Java代码- UripackageURI=Uri.parse("package:com.demo.CanavaCancel");
- IntentuninstallIntent=newIntent(Intent.ACTION_DELETE,packageURI);
- startActivity(uninstallIntent);
package:com.demo.CanavaCancel 这个形式是 package:程序完整的路径 (包名+程序名). 下载apk程序代码: Java代码
- protectedFiledownLoadFile(StringhttpUrl){
- //TODOAuto-generatedmethodstub
- finalStringfileName="updata.apk";
- FiletmpFile=newFile("/sdcard/update");
- if(!tmpFile.exists()){
- tmpFile.mkdir();
- }
- finalFilefile=newFile("/sdcard/update/"+fileName);
- try{
- URLurl=newURL(httpUrl);
- try{
- HttpURLConnectionconn=(HttpURLConnection)url
- .openConnection();
- InputStreamis=conn.getInputStream();
- FileOutputStreamfos=newFileOutputStream(file);
- byte[]buf=newbyte[256];
- conn.connect();
- doublecount=0;
- if(conn.getResponseCode()>=400){
- Toast.makeText(Main.this,"连接超时",Toast.LENGTH_SHORT)
- .show();
- }else{
- while(count<=100){
- if(is!=null){
- intnumRead=is.read(buf);
- if(numRead<=0){
- break;
- }else{
- fos.write(buf,0,numRead);
- }
- }else{
- break;
- }
- }
- }
- conn.disconnect();
- fos.close();
- is.close();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }catch(MalformedURLExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- returnfile;
- }
- privatevoidopenFile(Filefile){
- //TODOAuto-generatedmethodstub
- Log.e("OpenFile",file.getName());
- Intentintent=newIntent();
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setAction(android.content.Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(file),
- "application/vnd.android.package-archive");
- startActivity(intent);
- }
更多相关文章
- android EditText设置不可写
- android 使用html5作布局文件: webview跟javascript交互
- android studio调试c/c++代码
- IM-A820L限制GSM,WCDMA上网的原理(其他泛泰机型可参考)7.13
- 锁屏界面
- android(NDK+JNI)---Eclipse+CDT+gdb调试android ndk程序
- Android(安卓)version and Linux Kernel version
- Android(安卓)闹钟管理类的使用
- Android学习篇之Menu的使用