關於安卓root過手機靜默安裝與卸載

來源:互聯網
上載者:User

標籤:靜默安裝   靜默卸載   

    用到一個工具類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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubloadApks();}});button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubint  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 stubnew 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() {@Overridepublic 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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過手機靜默安裝與卸載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.