7.Android四大组件之Service

1.启动Service的两种方式

1.1 startService及stopService

状态:

  • onCreate

如果多次调用startService,并service已经处于运行的状态时,onCreate仅仅会被回调一次

  • onStartCommand

如果多次调用startService,每次启动时onStartCommand均会被调用

  • onBind:

Service中的onBind方法是抽象方法,所以Service类本身就是抽象类,也就是onBind方法是必须重写的,即使我们用不到。在通过startService使用Service时,我们在重写onBind方法时,只需要将其返回null即可

onBind方法主要是用于给bindService方法调用Service时才会使用到

  • onDestory

通过startService方法启动的Service会无限期运行,只有当调用了Context的stopService或在Service内部调用stopSelf方法时,Service才会停止运行并销毁,在销毁的时候会执行Service回调函数

在这里插入图片描述

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button startbutton;
    private Button stopbutton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        startbutton = (Button)findViewById(R.id.startbutton);
        stopbutton = (Button)findViewById(R.id.stopbutton);



        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.startbutton:
                        Intent startintent = new Intent(MainActivity.this, TesterService.class);
                        startService(startintent);
                        break;
                    case R.id.stopbutton:
                        Intent stopintent = new Intent(MainActivity.this, TesterService.class);
                        stopService(stopintent);
                        break;
                }

            }
        };
        
        startbutton.setOnClickListener(onClickListener);
        stopbutton.setOnClickListener(onClickListener);
    }
}
TesterService.java

public class TesterService extends Service {

    private static String TAG = "TesterService";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG,"onBind");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG,"onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"onStartCommand");
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        Log.i(TAG,"onDestroy");
        super.onDestroy();
    }
}

activity_main.xml


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/startservice"
        android:textColor="#3F51B5"
        android:id="@+id/startbutton"/>


    <Button
        android:id="@+id/stopbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/stopservice"
        tools:text="@string/stopservice"
        android:layout_below = "@id/startbutton"/>

Androidmainfest.xml中Service注册

        <service android:name=".TesterService">
        </service>

1.2 bindService及UNBindService

在这里插入图片描述

  • bindService

绑定并启动,由于需要绑定,则存在连接,调用此方法会回调onServiceConnection方法,原型如下:

   bindService(Intent, ServiceConnection, int)
  • intent

  • ServiceConnection

  • flag

  1. BIND_AUTO_CREATE:绑定服务后自动创建服务
  2. BIND_DEBUG_UNBIND
  3. BIND_EXTERNAL_SERVICE
  • unBindService

解绑定服务,其中onServiceDisconnected连接正常关闭的情况下是不会被调用的, 该方法只在Service 被破坏了或者被杀死的时候调用

2.实例演练

2.1 startService及stopService方法

MainActivity启动Service,并使用startService及stopService,其生命周期的流程图如下:

在这里插入图片描述

注意事项:
如果startService后没有停止stopService后,再次startService后回调方法onCreate不会调用,仅回调onStartCommand方法,只有在stopService后重新startService才能回调onCreate方法

2.2 bindService及unBindService方法

MainActivity调用bindService及unbindService,其生命周期的流程图如下:

在这里插入图片描述

注意事项:
MainActivity销毁的时候,MainActivity会自动与Service解除绑定,故service会自动解绑,当然MainActivity也可以通过明确调用Context的unbindService方法与Service解除绑定

3.项目代码

4.Service的类型

Service分为本地服务和远程服务

4.1 本地服务

见第一章节描述

4.2 远程服务

自动与Service解除绑定,故service会自动解绑,当然MainActivity也可以通过明确调用Context的unbindService方法与Service解除绑定

发布了100 篇原创文章 · 获赞 42 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Hh20161314/article/details/104269875
今日推荐