android 持续重启测试

今天工厂那边突然要个需求,做个压力测试。要求手机不断重启。这有需求就去做。那如何做呢?如果让手机重启呢?

呵呵,其实很简单 PowerMananger啊?看看里面是否有相应的方法实现。唉。不的了。有。

public void reboot(String reason)就这个东西。那好了,解决了。同时要求不断重启。那简单,直接在开机完成后捕捉完成事件,然后臧春杰reboot命令。

注意加上权限

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

嗯,代码写好了。一运行。我的妈,不得了。一直在重启。硬是没机会停下了。哈哈。后悔啊。得有个检测机制啊。

那就开机完成后,读SD卡里的一个文件,相当于密码,如果匹配,就不重启了。这样工厂就有机会停止程序了。

这基本就是文件的I/O操作。大家应该都清楚的不的了。

public class StartUpReceiver extends BroadcastReceiver {
    private static final String TAG = "StartUpReceiver";
  public static final String REBOOT_ACTION = "com.qisda.factest.action.reboot";
    private static final int PASSWD = 11;
   
    private static final String PSW_FILE_NAME = "/psw.txt";
    private static final String LOG_FILE_NAME = "/rebootlog.txt";
    private static final String internal_sdcard = Environment.getExternalStorageDirectory().getPath();
    private static final String external_sdcard = internal_sdcard+ "/sdcard2";
   
    private static final String PSW_FILE = external_sdcard + PSW_FILE_NAME;
    private static final String LOG_FILE = internal_sdcard + LOG_FILE_NAME;
  
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
            boolean bVerified = verifyPasswd(PSW_FILE);
            if(bVerified){
                debug("password is right!!!");
                return;
            }
            String time = getSystemTime();
            outputLog("now begin to shutdown system :    " + time + "\n");  
   String action = arg1.getAction();
   if(action.equals(Intent.ACTION_BOOT_COMPLETED) || action.equals(REBOOT_ACTION)){
    PowerManager powerManager= (PowerManager)arg0.getSystemService(Context.POWER_SERVICE);
    powerManager.reboot(null);
        }
    }
    private boolean verifyPasswd(String filepath){
        File file = new File(filepath);
        byte[] psword = new byte[1];
        if(! file.exists()){
            return false;
        }
        try {
            FileInputStream inputStream = new FileInputStream(file);
            inputStream.read(psword);
            inputStream.close();
            if(psword[0] == (byte)PASSWD){
                return true;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
    private void outputLog(String time){
        File file = new File(LOG_FILE);
        if(! file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            FileOutputStream outputStream = new FileOutputStream(file,true);
            byte[] log = time.getBytes();
            outputStream.write(log);
            outputStream.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

注意读写SD卡加权限

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

网上有人说无法写SD卡。我这边是2.3的,测试是没问题的。可能1.5不行吧。不知道。

一直不明白为什么system用户没有权限写SD卡呢?诡异啊。

posted on 2012-10-12 17:39  nanjing  阅读(1554)  评论(0编辑  收藏  举报

导航