Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - startActivityForResult outside an Activity?

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):

Intent enableBluetoothIntent  = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBluetoothIntent, BLUETOOTH_ENABLER);

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

like image 802
kkudi Avatar asked Mar 07 '11 10:03

kkudi


People also ask

Is startActivityForResult deprecated?

onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.

What is the replacement of startActivityForResult?

0 . It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.

How do you call a startActivity from a non activity class?

public Context call mcontext;<br> // ..... <br> Intent intent = new Intent(call mcontext, AboutActivity. class);<br> call mcontext. startActivity(intent);

How do you manage startActivityForResult?

First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .


1 Answers

startActivityForResult() is only available from real on-screen activities. Please redesign your application so that the user interface is driven from activities, then have your service scan for devices.

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?

You get rid of BluetoothDiscoverer and move its logic into the Service, which is a Context and therefore can register receivers.

like image 91
CommonsWare Avatar answered Sep 29 '22 08:09

CommonsWare