Search code examples
androidservicefragmentonactivityresult

calling setResult from Service class


In my UiFragment frgament I have where I try to connect with my device.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case REQUEST_CONNECT_DEVICE_SECURE:
                    // When DeviceListActivity returns with a device to connect
                    if (resultCode == Activity.RESULT_OK) {
                        connectDevice(data);
                    }
                    break;
            }
    }

and I also have StartScanService class where I need to do something similar to setResult (as I do that in Activity class)

public class StartScanService extends Service {
 ...
 ...
private void connect(String tmp){
        adapter.cancelDiscovery();

        String address = tmp.substring(tmp.length() - 17);

        // Create the result Intent and include the MAC address
        Intent intent = new Intent();
        intent.putExtra(EXTRA_DEVICE_ADDRESS, address);

        // Set result 
        //setResult(Activity.RESULT_OK, intent);

    }

}

So I need to call fragment class method from service class. Is it possible to do that?


Solution

  • I do that using interface. First I create interface

    public interface ServiceCallBack {
        void connectToDevice(Intent intent);
    }
    

    and in my Service class I call interface method 'connectToDevice'

    public class StartScanService extends Service {
    ...
    ...
    ...
    private ServiceCallBack serviceCallBack;
    
    private void connect(String tmp){
            adapter.cancelDiscovery();
    
            String address = tmp.substring(tmp.length() - 17);
    
            // Create the result Intent and include the MAC address
            Intent intent = new Intent();
            intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
    
            if (serviceCallBack != null) {
                serviceCallBack.connectToDevice(intent);
            }
        }
    

    }

    My Fragment class must implements ServiceCallBack interface

    public class UiFragment extends Fragment implements ServiceCallBack{
    private ServiceConnection connectionService = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG + " VEG", "onServiceConnected");
            StartScanService.ScanBinder scan = (StartScanService.ScanBinder) service;
            startScanService = scan.getScanService();
            startScanService.setHandler(handler);
            startScanService.setServiceCallBack(UiFragment.this);
            startScanService.startScan();
            bound = true;
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {
            bound = false;
            Log.d(TAG, "onServiceDisconnected");
        }
    };
    
    @Override
    public void connectToDevice(Intent intent) {
        connectDevice(intent);
    }
    }