Android组件Activity
1.onCreate(), onStart(), onResume(), onPause(), onStop(), onResult(), onDestory()
2.要想启动另一个Activity,必须在AndroidManifest.xml进行配置。在onCreate()函数中,把布局的xml文件用setContentView(R.layout.XXXX)引入,便可以用findViewById(R.id.XXX)来获取控件。之后便可以在获取到的控件上添加监听事件。
3.从MainActivity中向其他的Activity传递信息有两种方式:
btnStartAty1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Aty1.class);
//i.putExtra("txt", "Hello Acy1");
Bundle data = new Bundle();
data.putString("txt", "Hello Aty1");
i.putExtras(data);
//startActivity(i);
startActivityForResult(i, 0);
}
});
第一种方式是,i.putExtra("txt", "Hello Aty1")。第二种方式是,传递一个Bundle。之后便可以在要传递给的Activity的onCreate()中,获取传递来的Hello Aty1
4.如果MainActivity中要获取其他的Activity中传递来的数据,那么在启动Activity时,不可以用startActivity(i),而是要用startActivityForResult(i, 0);
5.在主MainActivity中获取其余Activity传递来的数据时,在其余的Activity中的onCreate()中要,
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.putExtra("txt", "Hello ManActivity"); //要传递的数据
setResult(0, i); //传给MainActivity
finish();
}
});
在主MainActivity中,用如下的方法,接收传递来的数据。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String result = data.getStringExtra("txt");
tvOut.setText(result);
super.onActivityResult(requestCode, resultCode, data);
}
1.启动service与关闭service
启动service时,要先建立一个service,然后在AndroidManifest.xml注册。
public class MainActivity extends Activity implements OnClickListener, ServiceConnection{
private Button btnStartService, btnStopService, btnBindService, btnUnbindService, btnGetCurrentNum;
private Intent serviceIntent;
private EchoService echoService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent = new Intent(this, EchoService.class);
btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService);
btnBindService = (Button) findViewById(R.id.btnBindService);
btnUnbindService = (Button) findViewById(R.id.btnUnbindService);
btnGetCurrentNum = (Button) findViewById(R.id.btnGetCurrentNum);
btnStartService.setOnClickListener(this);
btnStopService.setOnClickListener(this);
btnBindService.setOnClickListener(this);
btnUnbindService.setOnClickListener(this);
btnGetCurrentNum.setOnClickListener(this);
}
@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.btnStartService:
startService(serviceIntent);
break;
case R.id.btnStopService:
stopService(serviceIntent);
break;
case R.id.btnBindService:
bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.btnUnbindService:
unbindService(this);
echoService = null;
break;
case R.id.btnGetCurrentNum:
if (echoService != null) {
System.out.println("now service number is = "+echoService.getCurrentNum());
}
}
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
System.out.println("onServiceConnect");
echoService = ((EchoService.EchoServiceBinder)binder).getService();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
}
public class EchoService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("onBind");
return echoServiceBinder;
}
public final EchoServiceBinder echoServiceBinder = new EchoServiceBinder();
public class EchoServiceBinder extends Binder {
public EchoService getService() {
return EchoService.this;
}
}
public int getCurrentNum() {
return i;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("onCreate");
startTimer();
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("onDestroy");
stopTimer();
}
private int i = 0;
public void startTimer() {
if(timer == null) {
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
i++;
System.out.println(i);
}
};
timer.schedule(task, 1000, 1000);
}
}
public void stopTimer() {
if (timer != null) {
task.cancel();
timer.cancel();
task = null;
timer = null;
}
}
private Timer timer = null;
private TimerTask task = null;
}
btnStartService.setOnClickListener(this)。this是指处理监听事件的主体。函数onClick()中startService(serviceIntent)和stopService(serviceIntent)分别是启动服务和停止服务的代码。
启动服务时调用服务类中onCreate()函数,结束服务时调用onDestroy()函数。
2.绑定服务和解除服务
case R.id.btnBindService:
bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.btnUnbindService:
unbindService(this);
echoService = null;
break;
绑定服务和解除服务如上代码所示。
绑定服务时,在自己定义的Service类里,会自动调用onBind函数。该函数若返回一个Binder的子类,便绑定成功。此时,在MainActivity中会自动调用onServiceConnected()函数。。通过在自己定义的服务类里返回Binder的子类,在定义Binder子类的代码里,返回该服务类的实例。那么在MainActivity里便可以取到该服务的实例,之后便可以调用该服务实例里的方法和属性。