最近公司需要為終端設(shè)備定制一款A(yù)PP,除了展示的功能到涂,還需要開機(jī)自動啟動脊框、靜默安裝后自動啟動的功能。終端設(shè)備在出場之前就已經(jīng)Root了践啄,網(wǎng)上也還是有很多Root的代碼浇雹,今天就不說Root了。話不多說屿讽,直接為小伙伴們一一揭開面紗吧昭灵。
1.開機(jī)自啟(較為簡單)
首先寫一個廣播類
public class BootBroadcastReceiver extends BroadcastReceiver {
static final String action_boot="android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(action_boot)){
Intent ootStartIntent=new Intent(context,MainActivity.class);
ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//這句話一定要加上
context.startActivity(ootStartIntent);
}
}
}
在manifest注冊好后吠裆,就OK了
//開機(jī)自啟動廣播
<receiver android:name=".widget.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
2.靜默安裝后自動啟動,這里使用命令安裝(需要Root)
/**
* 靜默安裝apk
*/
private boolean installUseRoot(String filePath) {
if (TextUtils.isEmpty(filePath))
throw new IllegalArgumentException("Please check apk file path!");
boolean result = false;
Process process = null;
OutputStream outputStream = null;
BufferedReader errorStream = null;
try {
process = Runtime.getRuntime().exec("su");
outputStream = process.getOutputStream();
String command = "pm install -r " + filePath + "\n";
outputStream.write(command.getBytes());
outputStream.flush();
outputStream.write("exit\n".getBytes());
outputStream.flush();
process.waitFor();
errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder msg = new StringBuilder();
String line;
while ((line = errorStream.readLine()) != null) {
msg.append(line);
}
if (!msg.toString().contains("Failure")) {
result = true;
}
} catch (Exception e) {
Log.e("------>", e.getMessage(), e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (errorStream != null) {
errorStream.close();
}
} catch (IOException e) {
outputStream = null;
errorStream = null;
process.destroy();
}
}
return result;
}
安裝完成后使用AlarmManager發(fā)送廣播,達(dá)到自動啟動的目的
AlarmManager的詳細(xì)介紹 http://blog.csdn.net/wangxingwu_314/article/details/8060312
Intent intent= new Intent(mContext, MyBroadcastReceiver.class);
intent.setAction("INSTALL_AND_START");
PendingIntent sender = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager ALARM = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);
ALARM.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10*1000, sender);
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("INSTALL_AND_START")) {
start(context);
}
}
private void start(Context context){
if (!isRunningForeground(context)){
startAPP("com.example.newtownterminal", context);//字符串中輸入自己app的包名
}
}
/**
* 啟動一個app
*/
public void startAPP(String appPackageName, Context context){
try{
Intent intent = context.getPackageManager().getLaunchIntentForPackage(appPackageName);
context.startActivity(intent);
}catch(Exception e){
Toast.makeText(context, "尚未安裝", Toast.LENGTH_LONG).show();
}
}
/**
* 判斷app是否在前臺運(yùn)行
* @param context
* @return
*/
private boolean isRunningForeground (Context context) {
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String currentPackageName = cn.getPackageName();
if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) {
return true ;
}
return false ;
}
}
<receiver
android:name=".receiver.MyBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="INSTALL_AND_START"/>
</intent-filter>
</receiver>