SharedPreferences保存数据
package com.test; import java.io.FileWriter; import java.io.IOException; import android.app.Activity; import android.app.AlertDialog; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.PowerManager; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { Button stopBut; Button clearBut; TextView textView; CheckBox mCheckBox1, mCheckBox2; int bootNumber; boolean isCBChecked; boolean isReboot = false; Context mContext; private static final String BOOT_NUMBER = "mBootNumber"; private static final int SHUT_DOWN = 0; private static final int PLAY_VIDEO = 1; private static final String TAG = "MainActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mContext = this; stopBut = (Button) findViewById(R.id.stopBut); clearBut = (Button) findViewById(R.id.clearBut); textView = (TextView) findViewById(R.id.mTextView); mCheckBox1 = (CheckBox) findViewById(R.id.CheckBox1); mCheckBox2 = (CheckBox) findViewById(R.id.CheckBox2); stopBut.setOnClickListener(this); clearBut.setOnClickListener(this); onReboot(); // 启动保存log日志apk Intent intent = new Intent(); intent.setComponent(new ComponentName("com.logcat", "com.logcat.MainActivity")); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); //Intent mIntent = new Intent(); //mIntent = getPackageManager().getLaunchIntentForPackage(packageName); //startActivity(mIntent); SharedPreferences pres = getSharedPreferences(BOOT_NUMBER,Context.MODE_WORLD_READABLE); if (pres != null) { bootNumber = pres.getInt("boot_Number", 0); isCBChecked = pres.getBoolean("CheckBox_", true); } Log.i(TAG, "---------onCreate number:" + bootNumber); textView.setText("开机次数:" + bootNumber); bootNumber++; if (isCBChecked) { mCheckBox1.setChecked(true); mCheckBox2.setChecked(false); mHandler.sendEmptyMessageDelayed(SHUT_DOWN, 8000); Log.i(TAG, "---------isCBChecked:mCheckBox1" ); } else { mCheckBox1.setChecked(false); mCheckBox2.setChecked(true); mHandler.sendEmptyMessageDelayed(PLAY_VIDEO, 4000); Log.i(TAG, "---------isCBChecked:mCheckBox2" ); } mCheckBox1 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences pres = getSharedPreferences( BOOT_NUMBER, Context.MODE_WORLD_READABLE); if (isChecked) { isCBChecked = true; mCheckBox1.setChecked(true); mCheckBox2.setChecked(false); if (pres != null) { SharedPreferences.Editor ed = pres.edit(); ed.putBoolean("CheckBox_", true); ed.commit(); } } else { isCBChecked = false; SharedPreferences.Editor ed = pres.edit(); ed.putBoolean("CheckBox_", false); ed.commit(); } } }); mCheckBox2 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences pres = getSharedPreferences( BOOT_NUMBER, Context.MODE_WORLD_READABLE); if (isChecked) { isCBChecked = false; mCheckBox2.setChecked(true); mCheckBox1.setChecked(false); if (pres != null) { SharedPreferences.Editor ed = pres.edit(); ed.putBoolean("CheckBox_", false); ed.commit(); } } else { isCBChecked = true; SharedPreferences.Editor ed = pres.edit(); ed.putBoolean("CheckBox_", true); ed.commit(); } } }); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SHUT_DOWN: onShutdown(); break; case PLAY_VIDEO: onPlayVideo(); break; default: break; } } }; @Override public void onClick(View view) { if (view == stopBut) { Log.i(TAG, " stop"); new AlertDialog.Builder(this).setMessage("停止重启设备").setPositiveButton("OK", null).show(); stop(); mHandler.removeMessages(SHUT_DOWN); } else if (view == clearBut) { Log.i(TAG, " clear"); SharedPreferences pres = getSharedPreferences(BOOT_NUMBER,Context.MODE_WORLD_READABLE); if (pres != null) { SharedPreferences.Editor ed = pres.edit(); ed.putInt("boot_Number", 0); ed.commit(); } bootNumber = 0; textView.setText("开机次数:" + 0); } } public void onShutdown() { SharedPreferences pres = getSharedPreferences(BOOT_NUMBER,Context.MODE_WORLD_READABLE); if (pres != null) { SharedPreferences.Editor ed = pres.edit(); ed.putInt("boot_Number", bootNumber); ed.commit(); } //关机 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); pm.reboot("recovery"); } private void onPlayVideo(){ mHandler.sendEmptyMessageDelayed(SHUT_DOWN, 90000); Intent playVideo = new Intent(); playVideo.setClass(MainActivity.this, VideoActicity.class); startActivity(playVideo); } private void stop() { try { FileWriter fw = new FileWriter("/sys/misc/poweron_status"); fw.write("0x5a"); fw.close(); Log.i(TAG, "---------stop"); } catch (IOException e) { e.printStackTrace(); new AlertDialog.Builder(this).setMessage(e.getMessage()) .setPositiveButton("OK", null).show(); } } //修改文件 private void onReboot() { try { FileWriter fw = new FileWriter("/sys/misc/poweron_status"); fw.write("0x87"); fw.close(); Log.i(TAG, "---------onReboot"); } catch (IOException e) { e.printStackTrace(); new AlertDialog.Builder(this).setMessage(e.getMessage()) .setPositiveButton("OK", null).show(); } } @Override protected void onDestroy() { super.onDestroy(); mHandler.removeMessages(SHUT_DOWN); mHandler.removeMessages(PLAY_VIDEO); } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28