編輯:關於Android編程
本文實例講述了android實現靜默安裝與卸載的方法。分享給大家供大家參考。具體如下:
方法1:【使用調用接口方法,由於安裝卸載應用程序的部分API是隱藏的,所以必須下載Android系統源碼,在源碼下開發並編譯之後使用MM命令編譯生成APK文件】
import java.io.File; import android.app.Activity; import android.os.Bundle; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.IPackageInstallObserver; import android.content.pm.IPackageDeleteObserver; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class InstallActivity extends Activity { /** Called when the activity is first created. */ private String APK_NAME = "QQHD-mini.apk"; private Button install = null; private Button uninstall = null; private final int INSTALL_COMPLETE = 1; private final int UNINSTALL_COMPLETE = 2; final static int SUCCEEDED = 1; final static int FAILED = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.install_uninstall); install = (Button) findViewById(R.id.install); uninstall = (Button) findViewById(R.id.uninstall); uninstall.setOnClickListener(listener1); install.setOnClickListener(listener2); } private OnClickListener listener2=new OnClickListener(){ public void onClick(View v) { String fileName = Environment.getExternalStorageDirectory() + "/" + APK_NAME; Uri uri = Uri.fromFile(new File(fileName)); int installFlags = 0; PackageManager pm = getPackageManager(); try { PackageInfo pi = pm.getPackageInfo("com.tencent.minihd.qq", PackageManager.GET_UNINSTALLED_PACKAGES); if(pi != null) { installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; } } catch (NameNotFoundException e) {} PackageInstallObserver observer = new PackageInstallObserver(); pm.installPackage(uri, observer, installFlags, "com.tencent.minihd.qq"); } }; class PackageInstallObserver extends IPackageInstallObserver.Stub { public void packageInstalled(String packageName, int returnCode) { Message msg = mHandler.obtainMessage(INSTALL_COMPLETE); msg.arg1 = returnCode; mHandler.sendMessage(msg); } }; private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case INSTALL_COMPLETE: int centerTextLabel; Drawable centerTextDrawable = null; if(msg.arg1 == SUCCEEDED) { Toast.makeText(getApplicationContext(), "install apk SUCCEEDED", Toast.LENGTH_SHORT).show(); } else {} if (centerTextDrawable != null) {} break; case UNINSTALL_COMPLETE: int centerTextLabel2; Drawable centerTextDrawable2 = null; if(msg.arg1 == SUCCEEDED) { Toast.makeText(getApplicationContext(), "uninstall apk SUCCEEDED", Toast.LENGTH_SHORT).show(); } else {} if (centerTextDrawable2 != null) {} break; default: break; } } }; private OnClickListener listener1=new OnClickListener(){ public void onClick(View v) { PackageManager pm = getPackageManager(); PackageDeleteObserver observer = new PackageDeleteObserver(); pm.deletePackage("com.tencent.minihd.qq", observer, 0); } }; private class PackageDeleteObserver extends IPackageDeleteObserver.Stub { public void packageDeleted(String packageName, int returnCode) { Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE); msg.arg1 = returnCode; mHandler.sendMessage(msg); } } }
安裝、卸載相關權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.DELETE_PACKAGES" /> <uses-permission android:name="android.permission.CLEAR_APP_CACHE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
方法2:【需要獲取root權限或者和rom擁有一樣的簽名,即讓該apk以System的身份在系統中運行】
private void test() { execCommand("system/bin/pm install -r " + "sdcard/xxx.apk"); // xxx.apk放置在內置儲存的根目錄下(mnt/sdcard/xxx.apk) // execCommand("system/bin/pm uninstall " + PACKAGE_NAME); //PACKAGE_NAME為xxx.apk包名 } public boolean execCommand(String cmd) { Process process = null; try { process = Runtime.getRuntime().exec(cmd); process.waitFor(); } catch (Exception e) { return false; } finally { try { process.destroy(); } catch (Exception e) { } } return true; }
其他方法:一些第三方jar包等方式。
希望本文所述對大家的Android程序設計有所幫助。
最近關於微信應用號的消息越來越多,很多人也對它十分感興趣。傳聞已久的微信應用號總算要推出了,昨天晚上有網友在微博曝出一張微信應用號內測邀請函,引發了眾多的討
Toast的自定義使用原理與其類似。1.Toast源碼分析老規矩,我們先去看Toast的源碼。Toast有兩種顯示布局方式,一種最常見調用Toast.makeText()
Android中實現手勢畫圖一般都兩種方式,一是直接在View上繪制,而是使用SurfaceView。兩者還是有一些差別的。簡介下。View:顯示視圖,內置畫布,提供圖形
AudioPolicyService是策略的制定者,比如什麼時候打開音頻接口設備、某種Stream類型的音頻對應什麼設備等等。而AudioFlinger則是策略的執行者,