1) Active的生命周期
创建--->运行oncreate--->onstart--->onresume
运行--->销毁onpause--->onstop--->ondestroy
运行--->暂停(可见不可操作)onpause --->恢复 onResume
停止--->恢复 onrestart onstart onresume
当active处于暂停或是停止状态的是,更高等级的应用运行会占用内存,原先active可能被停止
2) 数据持久性
内存:断电掉数据,关闭数据掉数据
外存:断电不掉数据 U 盘 硬盘 /mnt/scard
Onstop:里面去保存数据
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;
@Override
protected 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 block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.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 block
e.printStackTrace();
}
}
}
@Override
public 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;
@Override
protected 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("第一���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 block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onStop() {
super.onStop();
Log.e("第2���e�`", 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 block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
if(oos != null)
{oos.close();}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public 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() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
//
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
//IBinder
Log.e("MainActivity", "onServiceConnected");
binder = (MyBinder) service;
}
};
private TextView mTextView;
@Override
protected 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);
}
@Override
public 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;
}
@Override
public 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() {
}
@Override
public IBinder onBind(Intent intent) {
Log.e("MyService", "onBind");
MyBinder myBinder = new MyBinder();
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("MyService", "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
Log.e("MyService", "onCreate");
super.onCreate();
}
public String play(){
return "";
}
public String stop(){
return "";
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("MyService", "onStartCommand"+intent.getStringExtra("key"));
return super.onStartCommand(intent, flags, startId);
}
@Override
public 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();
}
}
}