Andriod

Service绑定和解绑

2018-11-05  本文已影响8人  昨天剩下的一杯冷茶

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hzx.myservice.MainActivity">

    <Button
        android:id="@+id/start_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Service"/>

    <Button
        android:id="@+id/stop_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop Service"/>

    <Button
        android:id="@+id/bind_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bind Service"/>

    <Button
        android:id="@+id/unbind_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Unbind Service"/>
</LinearLayout>


自定义的服务类

package com.example.hzx.myservice;

//import Service;

import android.app.DownloadManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

/**
 * Created by hzx on 2018/11/5.
 */

public class MyService extends Service {

    private final String TAG = "123";

    private DownloadBinder mBinder = new DownloadBinder();

    class DownloadBinder extends Binder{
        public void startDownload(){
            Log.d(TAG,"-->startDownload executed");
        }

        public int getProgress(){
            Log.d(TAG,"-->getProgress executed");
            return 0;
        }
    }
    public MyService(){
    }

    public IBinder onBind(Intent intent){
        //throw new UnsupportedOperationException("Not yet implemented");
        return mBinder;
    }
    public void onCreate(){
        super.onCreate();
        Log.d(TAG,"-->onCreate");
    }

    public  int onStartCommand(Intent intent, int flags, int startId){
        Log.d(TAG,"-->onStartCommand");
        return super.onStartCommand(intent,flags,startId);
        //stopSelf();
    }

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

Activity

package com.example.hzx.myservice;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private final String TAG = "123";
    private MyService.DownloadBinder downloadBinder;

    private ServiceConnection connetction = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            downloadBinder = (MyService.DownloadBinder)iBinder;
            downloadBinder.startDownload();
            downloadBinder.getProgress();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

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


        Button startService = (Button)findViewById(R.id.start_service);
        Button stopService = (Button)findViewById(R.id.stop_service);

        startService.setOnClickListener(MainActivity.this);
        stopService.setOnClickListener(MainActivity.this);

        Button bingService = (Button)findViewById(R.id.bind_service);
        Button unbindService = (Button)findViewById(R.id.unbind_service);

        bingService.setOnClickListener(MainActivity.this);
        unbindService.setOnClickListener(MainActivity.this);



    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.start_service:
                Log.d(TAG,"activity start_service");
                Intent startIntent = new Intent(this,MyService.class);
                startService(startIntent);
                break;
            case R.id.stop_service:
                Log.d(TAG,"activity stop_sercie");
                Intent stopIntent = new Intent(this,MyService.class);
                stopService(stopIntent);
                break;
            case R.id.bind_service:
                Log.d(TAG,"activity bind_service");
                Intent bindIntent = new Intent(this,MyService.class);
                bindService(bindIntent,connetction,BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                Log.d(TAG,"activity unbind_service");
                unbindService(connetction);
                break;
                default:
                    break;
        }
    }
}

注意:
1、 需要在AndroidManifest.xml文件注册

        <service android:name=".MyService"
            android:enabled="true"
            android:exported="true"/>

效果

image.png
上一篇下一篇

猜你喜欢

热点阅读