首先判断是否有root权限,如果有利用静默方式,否则利用意图实现app安装和卸载操作。
亲测可用
/**
* 描述: app安装操作
* @author 吴传龙
* Email:andywuchuanlong@sina.cn
* QQ: 3026862225
* @version 创建时间: 2015年3月6日 下午3:51:14
* @version 最后修改时间:2015年3月6日 下午3:51:14 修改人:吴传龙
*/
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 | package import
import
import import
import
import public ApkController { public boolean install(String apkPath,Context context){ // 先判断手机是否有root权限 if (hasRootPerssion()){ // 有root权限,利用静默安装实现 return
} else { // 没有root权限,利用意图进行安装 File file = new
if (!file.exists()) return
; Intent intent = new
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
; } } public boolean uninstall(String packageName,Context context){ if (hasRootPerssion()){ // 有root权限,利用静默卸载实现 return
} else { Uri packageURI = Uri.parse( "package:"
Intent uninstallIntent = new
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(uninstallIntent); return
; } } /** * 判断手机是否有root权限 */ private boolean hasRootPerssion(){ PrintWriter PrintWriter = null ; Process process = null ; try
process = Runtime.getRuntime().exec( "su" ); PrintWriter = new
PrintWriter.flush(); PrintWriter.close(); int
return
} catch
e.printStackTrace(); } finally { if (process!= null ){ process.destroy(); } } return
; } /** * 静默安装 */ private boolean clientInstall(String apkPath){ PrintWriter PrintWriter = null ; Process process = null ; try
process = Runtime.getRuntime().exec( "su" ); PrintWriter = new
PrintWriter.println( "chmod 777 " +apkPath); PrintWriter.println( "export LD_LIBRARY_PATH=/vendor/lib:/system/lib" ); PrintWriter.println( "pm install -r " +apkPath); // PrintWriter.flush(); PrintWriter.close(); int
return
} catch
e.printStackTrace(); } finally { if (process!= null ){ process.destroy(); } } return
; } /** * 静默卸载 */ private boolean clientUninstall(String packageName){ PrintWriter PrintWriter = null ; Process process = null ; try
process = Runtime.getRuntime().exec( "su" ); PrintWriter = new
PrintWriter.println( "LD_LIBRARY_PATH=/vendor/lib:/system/lib " ); PrintWriter.println( "pm uninstall " +packageName); PrintWriter.flush(); PrintWriter.close(); int
return
} catch
e.printStackTrace(); } finally { if (process!= null ){ process.destroy(); } } return
; } /** * 启动app * com.exmaple.client/.MainActivity * com.exmaple.client/com.exmaple.client.MainActivity */ public boolean startApp(String packageName,String activityName){ boolean
false ; String cmd = "am start -n " + packageName + "/"
" \n" ; Process process = null ; try
process = Runtime.getRuntime().exec(cmd); int
return
} catch
e.printStackTrace(); } finally { if (process!= null ){ process.destroy(); } } return
} private boolean returnResult( int
// 代表成功 if
0 ) { return
; } else (value == 1 ) { // 失败 return
; } else
// 未知情况 return
; } } } |
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 | package import import
import
import
import
import
import
import
import
import
import
import
public MainActivity extends @Override protected onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public click1(View view){ new
public run() { String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/jniTest.apk" ; if
toast( "安裝成功" ); } else { toast( "安裝失败" ); } }; }.start(); } public click2(View view){ new
public run() { if
"com.example.jnitest" , getApplicationContext())){ toast( "卸載成功" ); } else { toast( "卸載失败" ); } }; }.start(); } /** * 描述: 启动 * @param * 修改人: 吴传龙 * 最后修改时间:2015年3月9日 上午8:19:30 */ public click3(View view){ if
"com.example.jnitest" , "com.example.jnitest.MainActivity" )) { toast( "啟動成功" ); } } public toast( final
runOnUiThread( new
@Override public run() { Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();; } }); } |