Android Service两种启动方式
2015-12-21 20:29
465 查看
两种启动service的方式:
ok,可以看出,service的生命周期为:
onCreate(),onStartCommand(),onBind(),onUnbind(),onDestory()。
下面我们来直接上代码验证。
布局:
Service类:
Activity类:
注册service:
好了,看效果吧。
先看下正常启动service。
点击startService按钮:
点击stopService按钮:
再看下绑定启动service:
点击bindService按钮:
点击unBindService按钮:
如果既点击了startService按钮,又点击了bindService按钮(不论点击顺序),需要点击stopService和unBindService两个按钮才能结束服务。
源码下载
ok,可以看出,service的生命周期为:
onCreate(),onStartCommand(),onBind(),onUnbind(),onDestory()。
下面我们来直接上代码验证。
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/bn_start_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="startService" /> <Button android:id="@+id/bn_stop_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="stopService" /> <Button android:id="@+id/bn_bind_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="bindService" /> <Button android:id="@+id/bn_unbind_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="unBindService" /> </LinearLayout>
Service类:
package com.kevin.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class MyService extends Service { private Binder binder=new Binder(); @Override public IBinder onBind(Intent intent) { Log.d("", "onBind--"); return binder; } @Override public void onCreate() { super.onCreate(); Log.d("", "onCreate--"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("", "onStartCommand--"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d("", "onDestroy--"); } @Override public boolean onUnbind(Intent intent) { Log.d("", "onUnbind--"); return super.onUnbind(intent); } }
Activity类:
package com.kevin.activity; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.kevin.service.MyService; public class MainActivity extends Activity implements OnClickListener { private Button bnStart, bnBind, bnStop, bnUnBind; private Binder binder; private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name)// 这个方法一般不会被调用,只有Service被破坏了或者被杀死的时候才会被调用 { binder = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { binder = (Binder) service; } }; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { bnStart = (Button) findViewById(R.id.bn_start_service); bnStop = (Button) findViewById(R.id.bn_stop_service); bnBind = (Button) findViewById(R.id.bn_bind_service); bnUnBind = (Button) findViewById(R.id.bn_unbind_service); bnStart.setOnClickListener(this); bnBind.setOnClickListener(this); bnStop.setOnClickListener(this); bnUnBind.setOnClickListener(this); } @Override public void onClick(View v) { intent = new Intent(this, MyService.class); if (v == bnStart) { startService(intent);// 正常启动 } else if (v == bnBind) { bindService(intent, conn, BIND_AUTO_CREATE);// 绑定启动 } else if (v == bnStop) { stopService(intent);// 正常结束 } else if (v == bnUnBind) { unbindService(conn);// 解除绑定 } } }
注册service:
<service android:name="com.kevin.service.MyService" > </service>
好了,看效果吧。
先看下正常启动service。
点击startService按钮:
点击stopService按钮:
再看下绑定启动service:
点击bindService按钮:
点击unBindService按钮:
如果既点击了startService按钮,又点击了bindService按钮(不论点击顺序),需要点击stopService和unBindService两个按钮才能结束服务。
源码下载
相关文章推荐
- android开发中遇到的异常总结
- android第三方框架 xlistview 的使用
- android 4.0 Menu的一些问题(学习笔记)
- Android中view的简单应用---随手指移动的小球
- Allocation Tracker(Android Studio) 分析应用内存问题
- Android Studio插件和一些小技巧
- android读取系统相册返回为空的解决办法
- Android 各种调试方法
- Android 动画浅析
- android studio 指南针 项目
- Android制作粒子爆炸特效
- Android 了解Activity
- android任意view爆炸效果--第三方开源--ExplosionField
- 多线程在android上面的下载(3)更新界面
- android onSaveInstanceState应用实例
- ScrollView自动滑动到底部
- Android软键盘遮住界面解决办法:通过软键盘改变视图位置
- Android控件_ProgressBar使用
- Android studio 点击按钮跳转到新的Activity
- 【Android实战】Android沉浸式状态栏实现(下)