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); } } }
相关文章推荐
- ios设备屏幕尺寸与分辨率
- 关于Microsoft.xmlhttp send 永无响应的奇怪问题
- 人生体会
- 2014互联网公司“组团”去美国上市,他们是......
- Windows平台下为Python添加MongoDB支持PyMongo
- 跟我学习css3之transition
- Java学习第21天:IO流之知识补充
- Set接口与应用_推算年龄
- 1004 Counting Leaves (30)
- 1005 Spell It Right (20)
- 读《超级符号就是超级创意》华与华方法
- Longest Substring Without Repeating Characters
- 九度OJ 1511 从尾到头打印链表
- AndroidManifest.xml详解
- LeetCode----Longest Substring Without Repeating Characters
- 九度OJ 1511 从尾到头打印链表
- c# ref与out的区别
- import paramiko时一个AttributeError错误的解决
- Python中的模块与包
- 无法创建新的堆栈防护页面