StartActivityForResult from a Service - java

Id like to enable bluetooth like in this example.
However my class isnt a Activity but a Service and therefore I can't call startActivityForResult. How can i solve this problem?. I know there are other questions that have been answered like
use startActivityForResult from non-activity
but this doesn't solve my problem because my application consists of the service and nothing else.

I know this is an older question, but I've run into a similar challenge, and my solution was to create an activity with android:theme="#android:style/Theme.NoDisplay", and then call startActivityForResult() from that. That creates an invisible activity that can both request and receive the intents, before writing data somewhere and then finishing itself.

Unfortunately you can't do that.
The only solution I found (hack) is to first open an Activity with a Dialog style and then do the call there.

I know this is an older question, but I've run into a similar challenge, and my solution was to start the activity using startActivity() instead of startActivityForResult(), and using Intent.FLAG_ACTIVITY_NEW_TASK. Then call startService() from the activity instead of setResult(), then use onStartCommand() instead of onActivityResult().
Binding to the service as #Pomagranite suggested could also work, but that seems more complicated :)

I think the solution is to start the activity from your service then have the activity bind to the service and register a callback

Related

Is it possible that after launching new activity the previous activity keeps sending data to new activity via intent?

I am working on an app in which i'm sending data to another activity via intent . is it possible that after launch of new activity data transfer (updates) continue to receive by new activity? links would be appreciated thanks in advance.
If another activity is loaded, then the previous one goes on the onPause state.
It wouldn't be accurate expecting anything from a paused activity.
Depending on the case and the function of the second activity, you could either use a service, or depending on your case, you might also be able to use fragments and stick with one activity.
An example of your code would be helpful to understand the case.

Android: Update TextView from Non-Activity Class

I am writing an Android App and i need to display some results from a non-activity class.
Basically my App gets the current location, activity and other things, and on those i run some functions, for example to check if the user has been at that place before.
Anyway, an intent triggers these functions, and at the end of the function I want to update a TextView with the result of these functions.
But it doesn't seem to work from anywhere besides MainActivity. I tried making the TextView static, but that doesn't seem to work, and returning the results to MainActivity is not possible either in since it is not called directly from MainActivity.
Does some have an idea on how to solve this problem?
This is my first android project, so there's still a lot of thing i don't know about. Thank you!
I don't know exactly what your code looks like, but do you have a model class that holds all your data (for example, some kind of singleton)? If so, you could just save the updated text there, and then update the TextView in a callback method.
If that doesn't fit what you're doing, I would considering refactoring your code so that the text you want is sent back to your MainActivity. You mentioned that the method is not called directly, but could your calculations pass the text to an intermediate class, which passes it to your MainActivity?
You should not update the ui drom background thread.
If you want to update the ui from background thread use Handlers,AsyncTask or use some other thread concepts.
If you want to update the UI fro Service or Broadcast receiver for eg you receive a message inside onReceive of broadcasrReceiver now to update the ui use sendBroadcast and inside your activity registet a dynamic Broadcast receiver and then update

Android: Creating a class which handles all Bluetooth related operations

I am trying to write an Android application which uses Bluetooth but ran into some problems. I succeeded in getting things working by following the guide on Android Developers portal. But i want to organize my code a bit and move everything related to Bluetooth to a separate class/src file. I run into a problem already when trying to turn on Bluetooth. As per above mentioned guide this is done with:
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
However my class is not an activity and startActivityForResult is not defined. I can pass my main activity's context to my Bluetooth class and call startActivityForResult on that. This works. But result is then returned to my main activity and again I have to write code into it, instead of my Bluetooth dedicated class.
Alternatively I can make my Bluetooth class extend the Activity class. But then startActivityForResult returns a nullPointer exception (I take it because my "Activity" is not initialized).
I am completely new in Android app development so I am hoping for some pointers about what to look at to help me solve my problem. I would really like to have everything related to Bluetooth in one class/src file so that I can reuse it in any future apps.
All ideas in how to achieve that will be greatly appreciated.
Thank you.
Composition, not inheritance.
Pass the Activity to your BluetoothGodClass as a contructor, store it in an instance variable, and use that to call the respective method. BUT.
What do you really need to do? Does the UI need to control bluetooth? Create a method turnOnBluetooth(Activity activity) and use that. Do you really need to receive the specific result of the activity you called? If not, use a Context or a BroadcastReceiver.

What is the difference between the Android constructor and onCreate()?

I'm a little confused by the difference between Java and Android Java. Let's say I have an Activity class AndroidX. There is no main function and there is no AndroidX() constructor as we know it. I realize that onCreate() most probably initializes the AndroidX Activity, but why is there no main? What's the difference?
Consider that your activities are many *main*s and your manifest directs the execution to one of them.
Also consider that the constructor as we know it before is hidden and now it is always called onCreate()
Fair enough to keep going?
This graphic may help some.
http://developer.android.com/images/activity_lifecycle.png
In the Activity documentation they elaborate on what each function is meant for (i.e. onCreate(), onResume(), etc).
http://developer.android.com/reference/android/app/Activity.html
There is no "main" because that assumes that your app is either running or not running. But on android there are many other possible states your app could be in paused, stopped, started, etc...
Check out this link for an excellent overview of the Android Activity lifecycle.
How onCreate works is described in the Activity page of the Android Developer Reference. Specifically here:
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
In a sense you can consider this method the constructor for your Activity, as the initialization is handled there (see the Activity Lifecycle).
As for main, consider it hidden from you. Generally what you do is register listeners for UI elements such as buttons or text fields, then act on input from those UI elements. These listeners handle calls to your methods which might manipulate data or change how the UI displays.

How does a Service communicate with its Activity? How can a Service call a method in the Activity, which started that Service?

Suppose I have an Activity that's a list. It calls a Service when this Activity starts. The Service will do background stuff--download, parse, and fill the list.
My question is this: How can the Service communicate with the Activity?
How can I call a method in the Activity, from the Service? (I'm new to OOP)
The Service is started like this:
hello_service = new Intent(this, HelloService.class);
startService(hello_service);
One of the things my service does is download and parse an XML. After that, it needs to fill a list! So, I want to pass the parsed stuff back to the Activity, and call a method in the Activity to fill that list.
I recently asked a very similar question, best way for service that starts activity to communicate with it, that got a very helpful answer.
Also, consider using AsyncTask instead of a service if the service doesn't need to be alive when the activity is closed. Then you don't need to bother with the server/activity-communication.
You can set activity object to service and invoke methods of your activity from service. But you must to care about thread-safe when you update your UI.
Hope this helps! Tutorial

Categories