您的位置:首页 > 其它

Active 与 Service 的生命周期、保存数据----Day04 2014.5.29

2014-06-05 15:55 141 查看
1) Active的生命周期创建--->运行oncreate--->onstart--->onresume运行--->销毁onpause--->onstop--->ondestroy运行--->暂停(可见不可操作)onpause --->恢复 onResume停止--->恢复 onrestart onstart onresume当active处于暂停或是停止状态的是,更高等级的应用运行会占用内存,原先active可能被停止2) 数据持久性内存:断电掉数据,关闭数据掉数据外存:断电不掉数据 U 盘 硬盘 /mnt/scardOnstop:里面去保存数据Onstart:去读取数据 sdcard
添加权限:读写sdcard的权限 AndroidManifest.xml-->Permission--->Add-->uses-permission--->在右边选择权限所需的权限--->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 跟
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>



保存数据:使用IO流、ByteArrayBuffer的使用public class MainActivity extends Activity {String path = "/mnt/sdcard/aaa.txt";private EditText text;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text = (EditText) findViewById(R.id.et1);Log.e("", ""+path);try {ByteArrayBuffer BAB = new ByteArrayBuffer(1000);FileInputStream fis = new FileInputStream(path);byte[] b = new byte[1024];int len = 0;while ((len = fis.read(b)) != -1) { BAB.append(b,0,len);}String str = new String(BAB.toByteArray());text.setText(str);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();FileOutputStream fos = null;try {Log.e("onStop1", "FileOutputStream");fos = new FileOutputStream(path);try {fos.write(text.getText().toString().getBytes());} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();} finally {try {if (fos != null)fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}} 对象流Android应用: public class MainActivity extends Activity { EditText et1; EditText et2; EditText et3; EditText et4;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et1 = (EditText)findViewById(R.id.editText1);et2 = (EditText)findViewById(R.id.editText2);et3 = (EditText)findViewById(R.id.editText3);et4 = (EditText)findViewById(R.id.editText4);Log.e("第一個錯誤", et1.getText().toString()+et2.getText().toString());try {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/mnt/sdcard/xiaosan.txt"));XiaoSan z = (XiaoSan)ois.readObject();et1.setText(z.name);et2.setText(z.age);et3.setText(z.Phone);et4.setText(z.Shengcai);} catch (StreamCorruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}} @Override protected void onStop() { super.onStop(); Log.e("第2個錯誤", et1.getText().toString()+et2.getText().toString()); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("/mnt/sdcard/xiaosan.txt"));oos.writeObject(new XiaoSan(et1.getText().toString(),et2.getText().toString(),et3.getText().toString(),et4.getText().toString()));oos.flush();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(oos != null){oos.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}} 3) Service服务 (提供服务)四大组件之一Service创建要素:创建一个类继承Service在清单活动中进行注册开启服务:调用startService()这个方法onCreate onStartCommand注意:第一次启动服务,会调用onCreate onStartCommand 这两个方法服务如果没有结束方法(onDestroy()),多次点击启动服务,只会调用onStartCommand().

Service 绑定服务:bingService(service,conn,Context.BIND_AUTO_CREATE);参数:第一个:Intent意图对象第二个:serviceconnect类型对象第三个:Context.BIND_AUTO_CREATE 绑定服务,如果没有服务会自动创建。 解绑服务:unbingdService(conn);注意:绑定服务跟解绑服务调用了的conn,必须是同一个对象。 MainActivity里面的设置: public class MainActivity extends Activity implements OnClickListener{private boolean isBind;private MyBinder binder;ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub//}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stub//IBinderLog.e("MainActivity", "onServiceConnected");binder = (MyBinder) service;}};private TextView mTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.button1).setOnClickListener(this);findViewById(R.id.button2).setOnClickListener(this);findViewById(R.id.button3).setOnClickListener(this);findViewById(R.id.button4).setOnClickListener(this);findViewById(R.id.button5).setOnClickListener(this);findViewById(R.id.button6).setOnClickListener(this);mTextView = (TextView) findViewById(R.id.textView1);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1://btn_01();break;case R.id.button2://btn_02();break;case R.id.button3://bind_service();break;case R.id.button4://unbind_service();break;case R.id.button5://btn_play();break;case R.id.button6://btn_stop();break;default:break;}}private void btn_stop() {mTextView.setText(binder.stop());}private void btn_play() {mTextView.setText(binder.play());}private void unbind_service() {if(isBind){unbindService(conn);isBind = false;}}private void bind_service() {isBind = true;Intent service = new Intent();service.setClass(this, MyService.class);bindService(service , conn , Context.BIND_AUTO_CREATE);}private void btn_02() {Intent name = new Intent();name.setClass(this, MyService.class);stopService(name );}private void btn_01() {Intent service = new Intent();service.setClass(this, MyService.class);service.putExtra("key", "");startService(service );}}MyService里面的代码:public class MyService extends Service {public MyService() {}@Overridepublic IBinder onBind(Intent intent) {Log.e("MyService", "onBind");MyBinder myBinder = new MyBinder();return myBinder;}@Overridepublic boolean onUnbind(Intent intent) {Log.e("MyService", "onUnbind");return super.onUnbind(intent);}@Overridepublic void onCreate() {Log.e("MyService", "onCreate");super.onCreate();}public String play(){return "";}public String stop(){return "";}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.e("MyService", "onStartCommand"+intent.getStringExtra("key"));return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.e("MyService", "onDestroy");super.onDestroy();}class MyBinder extends Binder{public String play(){return MyService.this.play();}
public String stop(){return MyService.this.stop();}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  active 持久性