码迷,mamicode.com
首页 > 移动开发 > 详细

关于安卓root过手机静默安装与卸载

时间:2015-05-09 16:41:14      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:静默安装   静默卸载   

    用到一个工具类AndroidCommon  详细了解地址:http://www.open-open.com/lib/view/open1385174381198.html   ,其中为我们提供了root后静默安装的工具类PackageUtils,其中包含安装与卸载。

   走下思路,首先静默安装要用到adb命令,所以手机必须root(至于360手机助手静默安装,无需root也可以静默安装,具体我也不清楚,哪位大牛清楚希望可以探讨下),在adb命令中执行pm install -r即可,具体可以参考AndroidCommon提供的类。工具类PackageUtil.sinstallSilent(context, path)静默安装与PackageUtil.uninstallSilent(this, path)静默卸载,都有返回值,返回值为1说明是成功的,具体返回值PackageUtil中有就不多说了 如果返回值不是1的话,可以执行普通的安装方法,接下来看代码

demo中只将AndroidCommon的工具类打包成Lib,可直接看源码

 demo下载地址  http://download.csdn.net/detail/u012303938/8679791

   MainActivity.class  代码中的apk下载地址可能会过期,如果不能下载,换个apk下载地址即可

package com.example.update;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import cn.trinea.android.common.util.PackageUtils;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{
	private int percent;
	private Button button1,button2;
	private TextView textView1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1=(Button) findViewById(R.id.button1);
		button2=(Button) findViewById(R.id.button2);
		textView1=(TextView) findViewById(R.id.textView1);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				loadApks();
			}
		});
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				int  l=PackageUtils.uninstallSilent(MainActivity.this, "com.example.callphone");
				 if(l!=1){
					 Uri packageURI = Uri.parse("package:" + "com.example.callphone");  
			            Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI);  
			            uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
			            startActivity(uninstallIntent); 
					}
			}
		});
	}
	private void loadApks() {
		// TODO Auto-generated method stub
		new Thread(){
			public void run() {
				HttpClient client=new DefaultHttpClient();
				String url="http://121.42.15.80:800/callphone.apk";
				HttpGet get=new HttpGet(url);;
				
				HttpResponse response=null;
				try {
					response=client.execute(get);
					HttpEntity entity=response.getEntity();
					long length=entity.getContentLength();
					int count=0;
					InputStream is=entity.getContent();
					FileOutputStream outputStream=null;
					if(is!=null){
						File file=new File(Environment.getExternalStorageDirectory(),"ybds.apk");
						outputStream=new FileOutputStream(file);
						byte[]  bt=new byte[1024];
						int len=-1;

						while((len=is.read(bt))!=-1){
							outputStream.write(bt, 0, len);
							count+=len;
							if((int)count*100/length>percent){
								percent=(int)(100*count/length);
								((Activity) MainActivity.this).runOnUiThread(new Runnable() {
																		
																		@Override
																		public void run() {
																			// TODO Auto-generated method stub
																			//roundprogressbar.setProgress(percent);
																			textView1.setText(String.valueOf(percent));
																		}
																	});
																
							}
						}
						outputStream.flush();
						if(outputStream!=null){
							outputStream.close();
						}
					}
					inStall();
					//jimodown();
					//installDown();
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
	}

	protected void inStall() {
		// TODO Auto-generated method stub


		//	File files=new File(Environment.getExternalStorageDirectory(),"TestDemo.apk");
		String path =Environment.getExternalStorageDirectory()
				.getPath() +"/ybds.apk" ;
		
		int i=PackageUtils.installSilent(this, path);
		if(i!=1){
			File file = new File(path);  
			if(!file.exists()){  
				return ;   
			}
			Intent intent = new Intent();  
			intent.setAction("android.intent.action.VIEW");  
			intent.addCategory("android.intent.category.DEFAULT");  
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
			intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");  
			startActivity(intent);
		}
			
	}
}


关于安卓root过手机静默安装与卸载

标签:静默安装   静默卸载   

原文地址:http://blog.csdn.net/u012303938/article/details/45601363

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!