Service初步

来源:互联网 发布:go并发编程实战 epub 编辑:程序博客网 时间:2024/05/17 03:27

转载请注明出处:http://blog.csdn.net/u011569040/article/details/45268007


Service是什么?

1.Service是一个应用程序组件

2.Service没有图形化界面

3.Service通常用来处理一些耗时比较长的操作

4.可以使用Service更新ContentProvider,发送Intent以及启动系统的通知等等


Service不是什么?

1.Service不是一个单独的进程

2.Service不是一个线程


service 启动 分 显式启动和隐式启动(注册清单里面添加intent-filter),隐式启动可以启动其它应用的                                         Service,显式启动只能在同一应用组件之间。

来自:尚学堂_尧玮_070集-Service概述和启动的视频

public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) {//bind是绑定的意思return null;}@Overridepublic void onCreate() {Log.i("main", "onCreate");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i("main", "onStartCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {Log.i("main", "onDestroy");super.onDestroy();}}

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setListener();}private void setListener() {startServiceListener();stopServiceListener();}private void stopServiceListener() {findViewById(R.id.stopService).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent (MainActivity.this,MyService.class);stopService(intent);}});}private void startServiceListener() {findViewById(R.id.buttonService).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this ,MyService.class);startService(intent);}});}}

清单里面注册

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.seivice1"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.seivice1.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service android:name="com.example.seivice1.MyService"></service>    </application></manifest>
点击“开启Service,打印”,再点击“开启Service”只打印,说明仅运行onStartCommand方法,未运行onCreate方法,onCreate方法只运行一次即创建的时候运行,点击“关闭Service”,

打印 ,点击“关闭Service”后再点击“开启Service”,则 onCreate()和onStartCommand 又都执行。


很重要的程序,反复打印可以知道绑定和解绑之类的功能,来自尚学堂_尧玮_071集-Service的生命周期与绑定的视频


import com.sxt.day07_03.MyService.MyBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends Activity {MyBinder mBider;Intent mIntent;private ServiceConnection conn=new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {//当我们已经绑定了,但是因为一些意外情况崩溃了,Activity和Service里面的联系中断了//被迫解除绑定,这时候可以写一些善后处理的代码}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//当绑定成功的时候,return的IBinder对象里面有一些信息,就可以得到了mBider=(MyBinder) service;Log.i("main",service.toString()+",count:"+mBider.getCount());}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setListener();}private void setListener() {setStartServiceClickListener();bindServiceClickListener();unBindServiceClickListener();}private void unBindServiceClickListener() {findViewById(R.id.btnUnbindService).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(mIntent==null){return ;}unbindService(conn);}});}private void bindServiceClickListener() {findViewById(R.id.btnBindService).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mIntent=new Intent(MainActivity.this, MyService.class);//第一个参数是一个Intent对象,第二个参数是绑定成功的时候回调接口里的实现类,//第三个参数是Service如果没有启动,我们可以设置连启动带绑定bindService(mIntent, conn, Context.BIND_AUTO_CREATE);}});}private void setStartServiceClickListener() {findViewById(R.id.btnStartService).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mIntent=new Intent(MainActivity.this, MyService.class);startService(mIntent);}});}}

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 int mCount=10;@Overridepublic IBinder onBind(Intent intent) {MyBinder binder=new MyBinder();return binder;}class MyBinder extends Binder{//IBinder这个接口里面很多方法,Binder类实现这个接口的部分方法(因为IBinder里面的抽象方法太多冗长)//MyBinder实现Binder,就间接的实现了IBinder,在onBind方法中就可以被returnpublic int getCount(){return ++mCount;}}@Overridepublic void onCreate() {super.onCreate();Log.i("main","onCreate()");}@Overridepublic boolean onUnbind(Intent intent) {//解除绑定成功的时候会回调这个方法Log.i("main","onUnbind");return true;}@Overridepublic void onRebind(Intent intent) {super.onRebind(intent);//再次绑定的时候会执行onRebind,而不是执行onBindLog.i("main","onRebind()");}@Overridepublic void onDestroy() {super.onDestroy();Log.i("main","onDestroy()");}}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/btnStartService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="start service" />    <Button        android:id="@+id/btnBindService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="bind service" />    <Button        android:id="@+id/btnUnbindService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="unBind service" /></LinearLayout>

 <service android:name="com.sxt.day07_03.MyService"/>
结果:点击绑定按钮bind service,打印:

点击解除绑定按钮unBind service,打印:

因为bind service这个绑定时 连启动带绑定 ,所以当点击 解绑的时候,service会被销毁

换一种方式

点击启动按钮start service,只打印了:

再点bind service,打印:

点击解绑按钮,打印只有:,所以并未销毁

再点击bind service,调用了OnRebind方法打印:





0 0
原创粉丝点击