开关机压力测试APK学习

        楼主身在一个手机方案公司,最近在查看一个开关机压力测试的APK,记录一下,学习一下别人的代码。功能单一,代码简单,谢谢!


布局界面较简单,一个ListView里面含有三个Textiew,一个设置总的重启次数,一个重启开关,一个用于显示标记已经重启的次数。这里就不写出了。


package com.zh.reboot;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.Context;

import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Toast;

public class mainActivity extends Activity {
	/** Called when the activity is first created. */

	// get all Adapter
	SimpleAdapter adapter;
	ArrayList list;
	HashMap mp;
	ListView lv;
	EditText et_num;
	String[] itemslab = { "", "", "" };
	String[] labmsg = { "", "", "" };
	String num;
	String[] str = { "", "" };

	private final Timer timer = new Timer();
	private TimerTask task;
	Handler handler;
	private boolean rebootflag = true;
	public final static int NEED_REBOOT = 1;
	public final static int NOT_NEED_REBOOT = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		itemslab[0] = getString(R.string.set_total_num);//总共重启次数
		itemslab[1] = getString(R.string.start_auto_reboot);//重启开关
		itemslab[2] = getString(R.string.reboot_record);//已经重启次数记录

		SharedPreferences sharedPreferences = getSharedPreferences("datafile",
				Context.MODE_PRIVATE);
		/*
		 * Editor editor = sharedPreferences.edit();//get editor
		 * editor.putInt("totalnum", 100); editor.putInt("count", 0);
		 * editor.commit();
		 */// submit modify

		int totalnum = sharedPreferences.getInt("totalnum", 1);
		int count = sharedPreferences.getInt("count", 1);

		labmsg[0] = String.valueOf(totalnum);
		labmsg[2] = String.valueOf(count);

		lv = (ListView) findViewById(R.id.lv_operation);
		list = new ArrayList();

		mp = new HashMap();
		mp.put("label", itemslab[0]);
		mp.put("labmsg", labmsg[0]);
		list.add(mp);
		mp = new HashMap();
		mp.put("label", itemslab[1]);
		mp.put("labmsg", labmsg[1]);
		list.add(mp);
		mp = new HashMap();
		mp.put("label", itemslab[2]);
		mp.put("labmsg", labmsg[2] + "/" + labmsg[0]);
		list.add(mp);

		adapter = new SimpleAdapter(this, list, R.layout.item, new String[] {
				"label", "labmsg" }, new int[] { R.id.label, R.id.labmsg });

		lv.setAdapter(adapter);

		if (count == totalnum) {//若相等,则表示开关机测试结束

		} else {
			AlertDialog.Builder builder = new AlertDialog.Builder(
					mainActivity.this);
			builder.setMessage("(" + labmsg[2] + "/" + labmsg[0] + ")" + "\n"
					+ getString(R.string.auto_dialog_msg));

			builder.setNegativeButton(getString(R.string.no),
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int arg1) {
							// TODO Auto-generated method stub
							rebootflag = false;
							SharedPreferences sharedPreferences = getSharedPreferences(
									"datafile", Context.MODE_PRIVATE);
							Editor editor = sharedPreferences.edit();// get
																		// editor
							editor.putInt("need_reboot", NOT_NEED_REBOOT);
							editor.commit();// submit data
							dialog.dismiss();

						}

					});

			builder.setPositiveButton(getString(R.string.yes),
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// excute reboot
							// modify count
							SharedPreferences sharedPreferences = getSharedPreferences(
									"datafile", Context.MODE_PRIVATE);
							int count = sharedPreferences.getInt("count", 1);
							int totalnum = sharedPreferences.getInt("totalnum",
									1);
							if (count < totalnum) {
								count++;
								Editor editor = sharedPreferences.edit();// get
																			// editor
								editor.putInt("count", count);
								editor.putInt("need_reboot", NEED_REBOOT);
								editor.commit();// submit data

								Intent intent = new Intent(Intent.ACTION_REBOOT);
								intent.putExtra("nowait", 1);
								intent.putExtra("interval", 1);
								intent.putExtra("window", 0);
								sendBroadcast(intent);
							}
						}
					});
			builder.show();//弹出提示,是否选择继续重启,或者是取消
		}
		// excute auto_reboot after 5s
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub

				super.handleMessage(msg);

				SharedPreferences sharedPreferences = getSharedPreferences(
						"datafile", Context.MODE_PRIVATE);
				int count = sharedPreferences.getInt("count", 1);
				int totalnum = sharedPreferences.getInt("totalnum", 1);
				if (count < totalnum && rebootflag == true) {
					count++;
					Editor editor = sharedPreferences.edit();// get editor
					editor.putInt("count", count);
					editor.commit();// submit data
					// excute reboot
					Intent intent = new Intent(Intent.ACTION_REBOOT);
					intent.putExtra("nowait", 1);
					intent.putExtra("interval", 1);
					intent.putExtra("window", 0);
					sendBroadcast(intent);
				}

			}

		};

		task = new TimerTask() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message message = new Message();
				message.what = 1;
				handler.sendMessage(message);
			}
		};

		timer.schedule(task, 5000);//若在前面builder.show();中没有选择,则5S后自动重启

		lv.setOnItemClickListener(new OnItemClickListener() {

			/**
			 * response to click item
			 */
			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
				HashMap item = (HashMap) arg0.getItemAtPosition(arg2);
				String label = String.valueOf(item.get("label"));

				if (label.equals(getString(R.string.set_total_num))) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							mainActivity.this);
					builder.setTitle(getString(R.string.set_total_num));
					et_num = new EditText(mainActivity.this);
					et_num.setHint(getString(R.string.hint_words));
					et_num.addTextChangedListener(new TextWatcher() {

						@Override
						public void afterTextChanged(Editable s) {

							if (s.length() > 0) {
								int pos = s.length() - 1;
								char c = s.charAt(pos);
								if (c < 48 || c > 57) {
									//
									s.delete(pos, pos + 1);
									Toast.makeText(
											mainActivity.this,
											getString(R.string.err_letter_info),
											Toast.LENGTH_SHORT).show();
								}

							}
							// TODO Auto-generated method stub

						}

						@Override
						public void beforeTextChanged(CharSequence arg0,
								int arg1, int arg2, int arg3) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onTextChanged(CharSequence arg0, int arg1,
								int arg2, int arg3) {
							// TODO Auto-generated method stub

						}

					});
					builder.setView(et_num);
					builder.setPositiveButton(getString(R.string.confirm),
							new OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// dialog.dismiss();

									String scount = et_num.getText().toString();
									if (scount.equals("")) {
										Toast.makeText(
												mainActivity.this,
												getString(R.string.err_info_null),
												Toast.LENGTH_SHORT).show();
									} else {
										int totalnum = Integer.parseInt(scount);
										if (totalnum > 0 && totalnum < 3000) {

											dialog.dismiss();

											labmsg[0] = scount;

											SharedPreferences sharedPreferences = getSharedPreferences(
													"datafile",
													Context.MODE_PRIVATE);

											Editor editor = sharedPreferences
													.edit();// get editor
											editor.putInt("totalnum", totalnum);
											editor.putInt("count", 0);
											editor.commit();// submit data
											// add data to layout
											labmsg[0] = String
													.valueOf(totalnum);
											labmsg[2] = "0";
											list = new ArrayList();

											mp = new HashMap();
											mp.put("label", itemslab[0]);
											mp.put("labmsg", labmsg[0]);
											list.add(mp);
											mp = new HashMap();
											mp.put("label", itemslab[1]);
											mp.put("labmsg", labmsg[1]);
											list.add(mp);
											mp = new HashMap();
											mp.put("label", itemslab[2]);
											mp.put("labmsg", labmsg[2] + "/"
													+ labmsg[0]);
											list.add(mp);

											// add data to adapter
											adapter = new SimpleAdapter(
													mainActivity.this, list,
													R.layout.item,
													new String[] { "label",
															"labmsg" },
													new int[] { R.id.label,
															R.id.labmsg });

											lv.setAdapter(adapter);// add
																	// adapter
																	// to
																	// listview
																	// and show
																	// it to
																	// user
										} else {
											Toast.makeText(
													mainActivity.this,
													getString(R.string.err_info),
													Toast.LENGTH_SHORT).show();
											dialog.dismiss();
										}
									}
								}

							});
					builder.setNegativeButton(getString(R.string.cancel), null);
					builder.show();

				} else if (label.equals(getString(R.string.start_auto_reboot))) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							mainActivity.this);
					builder.setMessage(getString(R.string.auto_reboot_msg));

					builder.setPositiveButton(getString(R.string.confirm),
							new OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									// modify count
									SharedPreferences sharedPreferences = getSharedPreferences(
											"datafile", Context.MODE_PRIVATE);
									int count = sharedPreferences.getInt(
											"count", 1);
									int totalnum = sharedPreferences.getInt(
											"totalnum", 1);
									if (count < totalnum) {
										count++;
										Editor editor = sharedPreferences
												.edit();// get editor
										editor.putInt("count", count);
										editor.putInt("need_reboot",
												NEED_REBOOT);
										editor.commit();// submit data

										Intent intent = new Intent(
												Intent.ACTION_REBOOT);
										intent.putExtra("nowait", 1);
										intent.putExtra("interval", 1);
										intent.putExtra("window", 0);
										sendBroadcast(intent);
									}
								}
							});

					builder.setNegativeButton(getString(R.string.cancel),
							new OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									SharedPreferences sharedPreferences = getSharedPreferences(
											"datafile", Context.MODE_PRIVATE);
									Editor editor = sharedPreferences.edit();// get
																				// editor
									editor.putInt("need_reboot",
											NOT_NEED_REBOOT);
									editor.commit();// submit data
									dialog.dismiss();
								}
							});

					builder.create().show();

				} else if (label.equals(getString(R.string.reboot_record))) {

				}

			}
		});

	}

}



程序代码比较简单。此外还有一个java文件。

package com.zh.reboot;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class BootBroadcastReceiver extends BroadcastReceiver {
	/** Called when the activity is first created. */
	static final String ACTION = "android.intent.action.BOOT_COMPLETED";//开机就启动,接收这个ACTION,检测是否需要继续开关机测试
	private String TAG = "Reboot";

	@Override
	public void onReceive(Context content, Intent intent) {
		// TODO Auto-generated method stub
		if (intent.getAction().equals(ACTION)) {
			SharedPreferences sp = content.getSharedPreferences("datafile",
					Context.MODE_PRIVATE);
			int count = sp.getInt("count", 0);
			int needreboot = sp.getInt("need_reboot",
					mainActivity.NOT_NEED_REBOOT);
			int totalnum = sp.getInt("totalnum", -1);
			Log.d(TAG, "count:" + count + " need_reboot:" + needreboot
					+ " totalnum:" + totalnum);
			if (count != 0 && needreboot == mainActivity.NEED_REBOOT
					&& count != totalnum) {
				Intent AutoRebootIntent = new Intent(content,
						mainActivity.class);
				AutoRebootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				content.startActivity(AutoRebootIntent);
			}
		}
	}
}

相关部分已经说明,很好理解。欢迎留言。留做标记,以后好找。

你可能感兴趣的:(Android)