Android开发之设备稳定性测试(循环N次重启设备)

        项目需求:在app中点击重启测试按钮后开始反复重启设备,且每次重启后会弹出提示框三秒,用户可点击提示框的确认按钮停止测试.

        需求拆分:1.点击按钮重启设备

                     2.监听开机广播

                     3.记录重启次数

                     4.弹出提示框

先说第一点,在代码中重启设备

    本项目中应用的设备已经是root过的(连接电脑,adb shell后直接是管理员权限),所以最简单的办法是通过命令控制设备重启,代码如下:

case R.id.rebootTest:
			// 创建退出对话框
			AlertDialog.Builder isSure = new Builder(this);
			// 设置对话框标题
			isSure.setTitle("警告");
			// 设置对话框消息
			isSure.setMessage("是否确定进行重启测试?");
			// 添加选择按钮并注册监听
			isSure.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					myLog("点击确定,开始重启测试");
					SharedPreferences share = getSharedPreferences("rebootCountConfig", MODE_PRIVATE);
	                Editor editor = share.edit();
	                editor.putInt("rebootCount", 1);
	                editor.putBoolean("StartRebootFlag", true);
	                editor.commit();
					startRebootTest();
					// finish();
				}
			});
			isSure.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();

					// finish();
				}
			});
			// 对话框显示
			isSure.create().show();
			break;

重启这么敏感的操作,肯定要弹出提示框确认再进行的,真正重启设备的代码在这儿:

protected void startRebootTest() {
		// TODO Auto-generated method stub
		myLog("进入startRebootTest方法");
		String cmd = "reboot";//或者"su reboot"
		try {
			// 发送请求
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			Log.d("zdd", "你的手机未root,无法实现该功能!");
		}
	}

再说第二点,监听开机广播

创建一个BroadcastReceiver文件,在Manifest.xml中静态注册广播,代码如下:

<receiver
            android:name="selfTest.MyBootCompleteReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="1000" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

添加权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

在BroadcastReceiver文件代码如下:

public class MyBootCompleteReceiver extends BroadcastReceiver {
	public final static int REBOOT_TOTAL_NUM = 999;
	public MyBootCompleteReceiver() {
	}

	@Override
	public void onReceive(final Context context, Intent intent) {
		// TODO: This method is called when the BroadcastReceiver is receiving
		mylog(intent.getAction());
		
	}
	
	private void mylog(String str) {
		Log.d("zdd", "MyBootComplete=>"+str);
	}
}

一步一步的验证,功能OK了再进行下一步.这里看Logcat中有监听到开机广播的打印信息就说明功能OK.

下一步,记录重启次数

使用SharedPreference存取记录重启次数的变量.

@Override
	public void onReceive(final Context context, Intent intent) {
		// TODO: This method is called when the BroadcastReceiver is receiving
		mylog(intent.getAction());
		SharedPreferences shared = context.getSharedPreferences("rebootCountConfig", Context.MODE_PRIVATE);
        int rebootCount = shared.getInt("rebootCount", 0);
        boolean startRebootTestFlag = shared.getBoolean("StartRebootFlag", false);
        mylog("rebootCount="+String.valueOf(rebootCount));
        if (rebootCount==0) {
			
		}else if (rebootCount<REBOOT_TOTAL_NUM) {
			if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {//进入显示弹框的Activity
				Intent BootBroadcastIntent = new Intent(context, IsRebootTestContinueActivity.class);
				BootBroadcastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(BootBroadcastIntent);
	
			}
		}else {//达到最大重启次数
			Intent BootBroadcastIntent = new Intent(context, MainMenu.class);
			BootBroadcastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(BootBroadcastIntent);
		}
	}

开始我是想直接在BroadcastReceiver中弹出对话框的,通过添加系统权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

在创建对话框时添加一句:

dialog.getWindow()  
        .setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		dialog.show();

也可以做到,但是效果几乎是一闪而逝,太考验手速和反应速度了.因此我跳转到一个Activity中专门用来显示弹出提示对话框,想延时几秒都可以.即弹出提示框 

public class IsRebootTestContinueActivity extends BaseActivity {
	AlertDialog dialog;
	Context mContext;
	TimerTask task;
	Timer  timer;
	public static final int REBOOT_TOTAL_NUM = 999;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_is_reboot_test_continue);
		
		mContext = this;
		AlertDialog.Builder isSure = new Builder(this);
		// 设置对话框标题
		isSure.setTitle("警告");
		// 设置对话框消息
		isSure.setMessage("是否确定停止重启测试?");
		// 添加选择按钮并注册监听
		isSure.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				//dialog.dismiss();
				Intent intent = new Intent(IsRebootTestContinueActivity.this, MainMenu.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
				finish();
			}
		});
		// 对话框显示
		dialog = isSure.create();
		dialog.getWindow()  
        .setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		dialog.show();
		
		task = new TimerTask() {
            @Override
            public void run() {
                dialog.dismiss();
              SharedPreferences shared = getSharedPreferences("rebootCountConfig", Context.MODE_PRIVATE);
    	        int rebootCount = shared.getInt("rebootCount", 0);
    	        boolean startRebootTestFlag = shared.getBoolean("StartRebootFlag", false);
    	        myLog("rebootCount="+String.valueOf(rebootCount));
    	        if (rebootCount==0) {
    				
    			}else if (rebootCount<REBOOT_TOTAL_NUM) {
    				SharedPreferences share = getSharedPreferences("rebootCountConfig", MODE_PRIVATE);
    				Editor editor = share.edit();
    		        editor.putInt("rebootCount", ++rebootCount);
    		        editor.commit();
    				String cmd = "reboot";
    				try {
    					// 发送请求
    					Runtime.getRuntime().exec(cmd);
    				} catch (IOException e) {
    					myLog("你的手机未root,无法实现该功能!");
    				}
    			}else {
    				myLog(String.valueOf(rebootCount)+"次重启测试结束,启动app");
    				Intent BootBroadcastIntent = new Intent(IsRebootTestContinueActivity.this, MainMenu.class);
    				BootBroadcastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    				startActivity(BootBroadcastIntent);
    			}
                finish();
            }
        };
        timer = new Timer();
        timer.schedule(task, 3000);//3秒后执行TimeTask的run方法
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (task!=null) {
			task.cancel();
		}
		if (timer!=null) {
			timer.cancel();
		}
	}

这样就大功告成了.

在实际测试时还遇到个问题,重启N次后接收不到开机广播,因此不再重启,我修改了以下广播的接收优先级,现在在测试效果.

猜你喜欢

转载自blog.csdn.net/qq_37069563/article/details/80678950
今日推荐