Android - startActivityForResult outside an Activity?


Last Updated: 2011-09-30 12:33:35
  1. kkudi

    kkudi Well-Known Member

    Hi

    I have a wrapper class (BluetoothDiscoverer) which is instantiated within a Service. This class obtains a BluetoothAdapter and checks whether Bluetooth is enabled before scanning for neighbouring devices.

    Now if Bluetooth is not enabled I want to be able to do the following within this class (BluetoothDiscoverer):

    Code (Text):
    1.  
    2.  
    3. Intent enableBluetoothIntent  = new
    4. Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    5.  
    6. startActivityForResult(enableBluetoothIntent, BLUETOOTH_ENABLER);
    7.  
    Now I have read this: use startActivityForResult from non-activity

    but I don't want to pass my Main Activity into this object since I want to deal with the result (whether the user accepts to enable bluetooth or not) within the BluetoothDiscoverer class.

    Now If I make BluetoothDiscoverer a subclass of Activity

    I seem to be getting a NullPointerException when the startActivityForResult is about to be called.

    I think this is because I need to add an onCreate()/onDestroy() method, but this defeats the purpose of what I am doing as I need to be able to call methods on the BluetoothDiscoverer object within the service that instantiates this class.

    I also need to register a broadcast receiver for retrieving neighbouring devices when a scan is initiated. If the BluetoothDiscoverer class is not an Activity, how do I register this receiver?

    Is there a work around for this?

    Thank you
    Andreas

    Advertisement
  2. trotimax

    trotimax New Member

    Hi,

    I am in the same situation. Did you find solution to your problem ?

    Thanks for your reply,
  3. trotimax

    trotimax New Member

    Hi,

    I am in the same situation. Did you find solution to your problem ?

    Thanks for your reply,

Share This Page