service 及button监听的重复利用
来源:互联网 发布:数据库存储过程代码 编辑:程序博客网 时间:2024/05/10 06:29
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 plaincopypublic 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); } } }
0 0
- service 及button监听的重复利用
- 利用service 监听电话
- 关于Button的监听
- Android的Button监听
- Button的监听方法
- button按钮的四种监听及实例
- 利用runtime解决button重复响应事件
- button的多种监听方法
- android Button 的监听类
- iOS利用runtime,解决多次点击相同button,导致重复跳转的问题
- 利用runtime,解决多次点击相同button,导致重复跳转的问题
- iOS- 利用runtime,解决多次点击相同button,导致重复跳转的问题
- iOS开发--利用 runtime,解决多次点击相同 button,导致重复跳转的问题
- 在android 中利用button 按钮设置监听事件
- include重复id的监听
- Android 使用Service 及 BroadcastReceiver 监听Volume
- 监听button窗口鼠标mouse_over的消息
- android Button 监听的几种方式
- 对交通灯管理系统的剖析
- 关于Microsoft.xmlhttp send 永无响应的奇怪问题
- 1004 Counting Leaves (30)
- 1005 Spell It Right (20)
- Java学习第22天:GUI
- service 及button监听的重复利用
- python调用php上传图片
- vim之对taglist插件的小修改
- 学习笔记 (Shell的标准输入/标准输出/标准错误) [第四课]
- 打印从1到100
- java相关配置
- Xcode 之nib文件
- hdu 成绩转换
- java如何探测数组当中相同的连续值?