下面是程式設計之家 jb51.cc 通過網絡收集整理的代碼片段。
程式設計之家小編現在分享給大家,也給大家做個參考。
package com.example.test;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
public class ApkController {
public static boolean install(String apkPath,Context context){
// 先判斷手機是否有root權限
if(hasRootPeRSSion()){
// 有root權限,利用靜默安裝實作
return clientInstall(apkPath);
}else{
// 沒有root權限,利用意圖進行安裝
File file = new File(apkPath);
if(!file.exists())
return false;
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");
context.startActivity(intent);
return true;
}
}
public static boolean uninstall(String packageName,Context context){
if(hasRootPeRSSion()){
// 有root權限,利用靜默解除安裝實作
return clientUninstall(packageName);
}else{
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);
return true;
}
}
private static boolean hasRootPeRSSion(){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
private static boolean clientInstall(String apkPath){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("chmod 777 "+apkPath);
PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
PrintWriter.println("pm install -r "+apkPath);
// PrintWriter.println("exit");
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
private static boolean clientUninstall(String packageName){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
PrintWriter.println("pm uninstall "+packageName);
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
public static boolean startApp(String packageName,String activityName){
boolean isSuccess = false;
String cmd = "am start -n " + packageName + "/" + activityName + " \n";
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(process!=null){
process.destroy();
}
}
return isSuccess;
}
private static boolean returnResult(int value){
// 代表成功
if (value == 0) {
return true;
} else if (value == 1) { // 失敗
return false;
} else { // 未知情況
return false;
}
}
}
package com.example.test;
import java.io.File;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View view){
new Thread(){
public void run() {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/jniTest.apk";
if (ApkController.install(path,getApplicationContext())){
toast("安裝成功");
}else{
toast("安裝失敗");
}
};
}.start();
}
public void click2(View view){
new Thread(){
public void run() {
if (ApkController.uninstall("com.example.jnitest",getApplicationContext())){
toast("卸載成功");
}else{
toast("卸載失敗");
}
};
}.start();
}
public void click3(View view){
if (ApkController.startApp("com.example.jnitest","com.example.jnitest.MainActivity")) {
toast("啟動成功");
}
}
public void toast(final String text){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();;
}
});
}
}
要用其他的方式實作靜默方式,可以通過僞裝成系統應用,這就要給app打上系統應用的簽名,但是這些簽名在小米等手機上是沒用的,是以這裡不做介紹。還有就是通過把應用放在system/app的目錄下也可以實作。
以上是程式設計之家(jb51.cc)為你收集整理的全部代碼内容,希望文章能夠幫你解決所遇到的程式開發問題。
如果覺得程式設計之家網站内容還不錯,歡迎将程式設計之家網站推薦給程式員好友。
總結
如果覺得程式設計之家網站内容還不錯,歡迎将程式設計之家網站推薦給程式員好友。
本圖文内容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。
小編個人微信号 jb51ccc
喜歡與人分享程式設計技術與工作經驗,歡迎加入程式設計之家官方交流群!