您的位置:首页 > 其它

service 及button监听的重复利用

2014-02-16 13:19 344 查看
public class MyService extends Service {
private static final String TAG = "MyService";
private MyBinder mBinder=new MyBinder();
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.e(TAG, "start IBinder~~~");
return mBinder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.e(TAG, "start onCreate~~~");
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.e(TAG, "start onDestroy~~~");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.e(TAG, "start onStartCommand~~~");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.e(TAG, "start onUnbind~~~");
return super.onUnbind(intent);
}
public String getSystemTime(){
/*Time t=new Time();
t.setToNow();*/
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date());
}
public class MyBinder extends Binder{
public MyService getService(){
return MyService.this;
}
}

}


分别实现了他的相应的生命周期方法,然后修改主activity为:

[java] view plaincopy
public class ServiceDemoActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private MyService mMyService;
private TextView mTextView;
private Context mContext;
private Button startServiceButton;
private Button stopServiceButton;
private Button bindServiceButton;
private Button unbindServiceButton;

//这里需要用到ServiceConnection在Context.bindService和context.unBindService()里用到
private ServiceConnection mServiceConnection = new ServiceConnection() {
//当我bindService时,让TextView显示MyService里getSystemTime()方法的返回值

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mMyService = ((MyService.MyBinder)service).getService();
mTextView.setText("I am frome Service :" + mMyService.getSystemTime());

}

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews(){
mContext=this;
mTextView=(TextView) this.findViewById(R.id.text);

startServiceButton = (Button)findViewById(R.id.startservice);
stopServiceButton = (Button)findViewById(R.id.stopservice);
bindServiceButton = (Button)findViewById(R.id.bindservice);
unbindServiceButton = (Button)findViewById(R.id.unbindservice);

startServiceButton.setOnClickListener(this);
stopServiceButton.setOnClickListener(this);
bindServiceButton.setOnClickListener(this);
unbindServiceButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == startServiceButton){
Intent i  = new Intent();
i.setClass(ServiceDemoActivity.this, MyService.class);
mContext.startService(i);
}else if(v == stopServiceButton){
Intent i  = new Intent();
i.setClass(ServiceDemoActivity.this, MyService.class);
mContext.stopService(i);
}else if(v == bindServiceButton){
Intent i  = new Intent();
i.setClass(ServiceDemoActivity.this, MyService.class);
mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE);
}else{
mContext.unbindService(mServiceConnection);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: