重启压力测试APK
完整实现代码如下:
MainActivity.java
package mqqzt.reboot;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
int t = 10;
TextView time;
SharedPreferences sharedPreferences;
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
t--;
handler.sendEmptyMessage(0x123);
}
};
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(t > 0){
time.setText("倒计时:" + t);
}else {
timer.cancel();
PowerManager pManager=(PowerManager)getSystemService(Context.POWER_SERVICE);
pManager.reboot("重启");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("rebootNum", Context.MODE_PRIVATE);
TextView rebootNum = (TextView) findViewById(R.id.num);
rebootNum.setText("重启次数:" + sharedPreferences.getInt("rebootNum", 0));
time = (TextView) findViewById(R.id.time);
Button stopReboot = (Button) findViewById(R.id.stopReboot);
stopReboot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
timer.schedule(task, 1000, 1000);
}
@Override
protected void onDestroy() {
sharedPreferences.edit().putInt("rebootNum", 0).commit();
super.onDestroy();
}
}
BootCompleteReceiver.java
package mqqzt.reboot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
public class BootCompleteReceiver extends BroadcastReceiver {
public BootCompleteReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences = context.getSharedPreferences("rebootNum", Context.MODE_PRIVATE);
int rebootNum = sharedPreferences.getInt("rebootNum", 0);
sharedPreferences.edit().putInt("rebootNum", ++rebootNum).commit();
Intent intent1 = new Intent(context, MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mqqzt.reboot"
android:sharedUserId="android.uid.system" >
<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="倒计时:10"
android:textSize="20sp"
android:textColor="#ff0000"/>
<TextView
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重启次数:0"
android:textSize="20sp"
android:layout_marginLeft="50dp"
android:textColor="#000000"/>
</LinearLayout>
<Button
android:id="@+id/stopReboot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止重启"/>
</LinearLayout>
实现重启的apk需要system权限,权限提升需要以下步骤:
- 在AndroidManifest.xml文件中的manifest节点添加android:sharedUserId=”android.uid.system”
- 使用android自带的签名工具signapk.jar以及源码中的platform.pk8和platform.x509.pem,对apk进行重新签名,执行命令:java -jar signapk.jar platform.x509.pem platform.pk8 old.apk new.apk,执行后的new.apk即为签名后的文件
注:platform.pk8和platform.x509.pem可以在源码中build/target/product/security中找到,signapk.jar可以通过编译源码得到