Android BindService

簡單寫一個bindService 給fragment 使用

SanyaService.java

package com.sanya.applications.sanyaservice;

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

public class SanyaService extends Service {
    private final static String TAG = "SanyaService";
    private final IBinder binder = new SanyaBinder();

    public SanyaService() {
    }

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class SanyaBinder extends Binder {
        SanyaService getService() {
            // Return this instance of SanyaService so clients can call public methods.
            return SanyaService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG,"onBind");
        return binder;
    }

    @Override
    public boolean onUnbind(Intent intent){
        Log.d(TAG,"onUnbind");
        return super.onUnbind(intent);
    }

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

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

    /** Method for clients. */
    public String getServiceName() {
        String message = "This is SanyaService";
        return message;
    }
}

MainFragment.java

package com.sanya.applications.sanyaservice;


public class WifiScanFragment extends Fragment {
    SanyaService mSanyaService;
    boolean mBound = false;


    /** Defines callbacks for service binding, passed to bindService(). */
    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            // We've bound to SanyaService, cast the IBinder and get SanyaService instance.
            SanyaService.SanyaBinder binder = (SanyaService.ZybridBinder) service;
            SanyaService = binder.getService();
            mBound = true;
            Log.d(TAG, " onServiceConnected()");
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
            Log.d(TAG, " onServiceDisconnected()");
        }
    };
   

    public WifiScanFragment() {
    }

    @Override
    public void onStart() {
        super.onStart();
        //+service
        // Bind to ZybridHomeService.
        Intent intent = new Intent(requireContext(), ZybridHomeService.class);
        requireActivity().bindService(intent, connection, Context.BIND_AUTO_CREATE);
        //-service
    }

    @Override
    public void onStop() {
        super.onStop();
        //+service
        Log.d(TAG, "WifiScanFragment -- onStop");
        requireActivity().unbindService(connection);
        mBound = false;
        //-service
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentWifiScanBinding.inflate(inflater, container, false);
        //+service
        Log.d(TAG, " get service Name: " + SanyaService.getServiceName());
        //-service
        return binding.getRoot();
    }
}

AndroidManifest.xml

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

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

購物車