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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 两周宝宝不爱吃饭怎么办 小孩不喜欢吃水果蔬菜怎么办 孩子对学习不感兴趣怎么办 幼儿园老师不喜欢我孩子怎么办 孩子数学不主动思考问题怎么办 孩子做事慢磨蹭家长应该怎么办 孩子不写作业怎么办啊 对孩子在校被欺怎么办 二年级的孩子不爱学习怎么办 孩子不爱学习怎么办二年级 二年级孩子不爱写作业怎么办 孩子喜欢的朋友家长不喜欢怎么办? 孩子不喜欢上幼儿园家长怎么办 孩子不喜欢家长学佛怎么办呢? 学生家长讨厌老师老师该怎么办 孩子不爱去幼儿园总是哭怎么办 孩子学习一点都不主动怎么办 孩子怕老师不想上学怎么办 幼儿园孩子怕老师不想上学怎么办 孩子在幼儿园怕老师怎么办 幼儿园老师对孩子有歧视怎么办 儿子在幼儿园受到老师歧视怎么办 我把老师骂了怎么办 孩子讨厌老师骂老师怎么办 幼儿园老师讨厌一个孩子怎么办 幼儿园老师对孩子不好怎么办 高中分班讨厌班主任怎么办 把孩子老师惹了怎么办 高三班主任第一节课应该怎么办 高三孩子与班主任不和怎么办 孩子很害怕一件事怎么办 吼完孩子觉得很害怕怎么办 小朋友在幼儿园被欺负怎么办 老师总找孩子茬怎么办 小孩说幼儿园老师不喜欢她怎么办 小孩在学校老师不喜欢怎么办 好哭的孩子老师怎么办 小孩哭的犟住怎么办 2-3小孩脾气很犟怎么办 生了孩子没奶怎么办 二年级孩子写作业慢怎么办