SharedPreferences保存数据

Java代码 复制代码 收藏代码
  1. package com.test;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.content.ComponentName;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.os.Message;
  13. import android.os.PowerManager;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.view.View.OnClickListener;
  17. import android.widget.Button;
  18. import android.widget.CheckBox;
  19. import android.widget.CompoundButton;
  20. import android.widget.TextView;
  21. public class MainActivity extends Activity implements OnClickListener {
  22. Button stopBut;
  23. Button clearBut;
  24. TextView textView;
  25. CheckBox mCheckBox1, mCheckBox2;
  26. int bootNumber;
  27. boolean isCBChecked;
  28. boolean isReboot = false;
  29. Context mContext;
  30. private static final String BOOT_NUMBER = "mBootNumber";
  31. private static final int SHUT_DOWN = 0;
  32. private static final int PLAY_VIDEO = 1;
  33. private static final String TAG = "MainActivity";
  34. @Override
  35. public void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.main);
  38. mContext = this;
  39. stopBut = (Button) findViewById(R.id.stopBut);
  40. clearBut = (Button) findViewById(R.id.clearBut);
  41. textView = (TextView) findViewById(R.id.mTextView);
  42. mCheckBox1 = (CheckBox) findViewById(R.id.CheckBox1);
  43. mCheckBox2 = (CheckBox) findViewById(R.id.CheckBox2);
  44. stopBut.setOnClickListener(this);
  45. clearBut.setOnClickListener(this);
  46. onReboot();
  47. // 启动保存log日志apk
  48. Intent intent = new Intent();
  49. intent.setComponent(new ComponentName("com.logcat", "com.logcat.MainActivity"));
  50. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  51. startActivity(intent);
  52. //Intent mIntent = new Intent();
  53. //mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
  54. //startActivity(mIntent);
  55. SharedPreferences pres = getSharedPreferences(BOOT_NUMBER,Context.MODE_WORLD_READABLE);
  56. if (pres != null) {
  57. bootNumber = pres.getInt("boot_Number", 0);
  58. isCBChecked = pres.getBoolean("CheckBox_", true);
  59. }
  60. Log.i(TAG, "---------onCreate number:" + bootNumber);
  61. textView.setText("开机次数:" + bootNumber);
  62. bootNumber++;
  63. if (isCBChecked) {
  64. mCheckBox1.setChecked(true);
  65. mCheckBox2.setChecked(false);
  66. mHandler.sendEmptyMessageDelayed(SHUT_DOWN, 8000);
  67. Log.i(TAG, "---------isCBChecked:mCheckBox1" );
  68. } else {
  69. mCheckBox1.setChecked(false);
  70. mCheckBox2.setChecked(true);
  71. mHandler.sendEmptyMessageDelayed(PLAY_VIDEO, 4000);
  72. Log.i(TAG, "---------isCBChecked:mCheckBox2" );
  73. }
  74. mCheckBox1
  75. .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  76. @Override
  77. public void onCheckedChanged(CompoundButton buttonView,
  78. boolean isChecked) {
  79. SharedPreferences pres = getSharedPreferences(
  80. BOOT_NUMBER, Context.MODE_WORLD_READABLE);
  81. if (isChecked) {
  82. isCBChecked = true;
  83. mCheckBox1.setChecked(true);
  84. mCheckBox2.setChecked(false);
  85. if (pres != null) {
  86. SharedPreferences.Editor ed = pres.edit();
  87. ed.putBoolean("CheckBox_", true);
  88. ed.commit();
  89. }
  90. } else {
  91. isCBChecked = false;
  92. SharedPreferences.Editor ed = pres.edit();
  93. ed.putBoolean("CheckBox_", false);
  94. ed.commit();
  95. }
  96. }
  97. });
  98. mCheckBox2
  99. .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  100. @Override
  101. public void onCheckedChanged(CompoundButton buttonView,
  102. boolean isChecked) {
  103. SharedPreferences pres = getSharedPreferences(
  104. BOOT_NUMBER, Context.MODE_WORLD_READABLE);
  105. if (isChecked) {
  106. isCBChecked = false;
  107. mCheckBox2.setChecked(true);
  108. mCheckBox1.setChecked(false);
  109. if (pres != null) {
  110. SharedPreferences.Editor ed = pres.edit();
  111. ed.putBoolean("CheckBox_", false);
  112. ed.commit();
  113. }
  114. } else {
  115. isCBChecked = true;
  116. SharedPreferences.Editor ed = pres.edit();
  117. ed.putBoolean("CheckBox_", true);
  118. ed.commit();
  119. }
  120. }
  121. });
  122. }
  123. private Handler mHandler = new Handler() {
  124. @Override
  125. public void handleMessage(Message msg) {
  126. switch (msg.what) {
  127. case SHUT_DOWN:
  128. onShutdown();
  129. break;
  130. case PLAY_VIDEO:
  131. onPlayVideo();
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. };
  138. @Override
  139. public void onClick(View view) {
  140. if (view == stopBut) {
  141. Log.i(TAG, " stop");
  142. new AlertDialog.Builder(this).setMessage("停止重启设备").setPositiveButton("OK", null).show();
  143. stop();
  144. mHandler.removeMessages(SHUT_DOWN);
  145. } else if (view == clearBut) {
  146. Log.i(TAG, " clear");
  147. SharedPreferences pres = getSharedPreferences(BOOT_NUMBER,Context.MODE_WORLD_READABLE);
  148. if (pres != null) {
  149. SharedPreferences.Editor ed = pres.edit();
  150. ed.putInt("boot_Number", 0);
  151. ed.commit();
  152. }
  153. bootNumber = 0;
  154. textView.setText("开机次数:" + 0);
  155. }
  156. }
  157. public void onShutdown() {
  158. SharedPreferences pres = getSharedPreferences(BOOT_NUMBER,Context.MODE_WORLD_READABLE);
  159. if (pres != null) {
  160. SharedPreferences.Editor ed = pres.edit();
  161. ed.putInt("boot_Number", bootNumber);
  162. ed.commit();
  163. }
  164. //关机
  165. PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
  166. pm.reboot("recovery");
  167. }
  168. private void onPlayVideo(){
  169. mHandler.sendEmptyMessageDelayed(SHUT_DOWN, 90000);
  170. Intent playVideo = new Intent();
  171. playVideo.setClass(MainActivity.this, VideoActicity.class);
  172. startActivity(playVideo);
  173. }
  174. private void stop() {
  175. try {
  176. FileWriter fw = new FileWriter("/sys/misc/poweron_status");
  177. fw.write("0x5a");
  178. fw.close();
  179. Log.i(TAG, "---------stop");
  180. } catch (IOException e) {
  181. e.printStackTrace();
  182. new AlertDialog.Builder(this).setMessage(e.getMessage())
  183. .setPositiveButton("OK", null).show();
  184. }
  185. }
  186. //修改文件
  187. private void onReboot() {
  188. try {
  189. FileWriter fw = new FileWriter("/sys/misc/poweron_status");
  190. fw.write("0x87");
  191. fw.close();
  192. Log.i(TAG, "---------onReboot");
  193. } catch (IOException e) {
  194. e.printStackTrace();
  195. new AlertDialog.Builder(this).setMessage(e.getMessage())
  196. .setPositiveButton("OK", null).show();
  197. }
  198. }
  199. @Override
  200. protected void onDestroy() {
  201. super.onDestroy();
  202. mHandler.removeMessages(SHUT_DOWN);
  203. mHandler.removeMessages(PLAY_VIDEO);
  204. }
  205. }  
posted on 2013-02-07 15:15  蜜雪薇琪  阅读(290)  评论(0编辑  收藏  举报