设为首页 加入收藏

TOP

Android Service生命周期及用法(二)
2015-07-16 12:56:23 来源: 作者: 【 】 浏览:57
Tags:Android Service 生命 周期 用法
droid.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ServiceDemo extends Activity implements OnClickListener{
?
?private MyService? mMyService;
?private TextView mTextView;
?private Button startServiceButton;
?private Button stopServiceButton;
?private Button bindServiceButton;
?private Button unbindServiceButton;
?private Context mContext;
?
?//这里需要用到ServiceConnection在Context.bindService和context.unBindService()里用到
?private ServiceConnection mServiceConnection = new ServiceConnection() {
? //当我bindService时,让TextView显示MyService里getSystemTime()方法的返回值?
? 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());
? }
?
? public void onServiceDisconnected(ComponentName name) {
? ?// TODO Auto-generated method stub
? ?
? }
?};
? ? public void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.main);
? ? ? ? setupViews();
? ? }
? ?
? ? public void setupViews(){
? ?
? ? ?mContext = ServiceDemo.this;
? ? ?mTextView = (TextView)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);
? ? }
?
?public void onClick(View v) {
? // TODO Auto-generated method stub
? if(v == startServiceButton){
? ?Intent i? = new Intent();
? ?i.setClass(ServiceDemo.this, MyService.class);
? ?mContext.startService(i);
? }else if(v == stopServiceButton){
? ?Intent i? = new Intent();
? ?i.setClass(ServiceDemo.this, MyService.class);
? ?mContext.stopService(i);
? }else if(v == bindServiceButton){
? ?Intent i? = new Intent();
? ?i.setClass(ServiceDemo.this, MyService.class);
? ?mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE);
? }else{
? ?mContext.unbindService(mServiceConnection);
? }
?}
?
?
?
}


第五步:修改AndroidManifest.xml代码(将我们新建的MyService注册进去如下代码第14行:)



? ? ? package="com.tutor.servicedemo"
? ? ? android:versionCode="1"
? ? ? android:versionName="1.0">
? ?
? ? ? ? ? ? ? ? ? ? ? ? ? android:label="@string/app_name">
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?

? ? ? ?

?
? ?

? ?


第六步:执行上述工程,效果图如下:



点击startServie按钮时先后执行了Service中onCreate()->onStart()这两个方法,打开Logcat视窗效果如下图:



我们这时可以按HOME键进入Settings(设置)->Applications(应用)->Running Services(正在运行的服务)看一下我们新启动了一个服务,效果如下:



点击stopService按钮时,Service则执行了onDestroy()方法,效果图如下所示:



这时候我们再次点击startService按钮,然后点击bindService按钮(通常bindService都是bind已经启动的Service),我们看一下Service执行了IBinder()方法,以及TextView的值也有所变化了,如下两张图所示:




最后点击unbindService按钮,则Service执行了onUnbind()方法,如下图所示:



Ok,今天就先讲到这里了,谢谢大家关注~


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android Studio 单刷《第一行代码.. 下一篇Android基础知识总结篇

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: