I currently have a bound Service with my Main activity.
I was wondering if it was possible to have a Thread Running inside this bound Service that can pass integers to my Main Activity. This Service needs to auto update my Main Activity's Text View with any new Random number Integer without clicking a button.
Should i look into Handlers?? Or Message/ Bundles??
Any help would be appreciated ! Thank you !
You can define one Receiver your MainActivity and you can use send Broadcast to that receiver to update your UI.. That is a simple way to do it
In your service Just define one intent and put values into it like follwing
Intent i = new Intent();
i.setAction("RECEIVERACTION");
i.putExtra("data", "mydata");
sendBroadcast(i);
and in your activity
public static class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String data = intent.getStringExtra("data");
textView.setText(data);
}
}
Hope it helps!!!
Please go through api guides. Specifically
https://developer.android.com/guide/components/bound-services.html
https://developer.android.com/guide/components/services.html
This should give you a pretty good idea to move forward and also code samples
Related
I'm developing an android application, which when user enters licence code, retrieves from web service some simple key-value data and saves to sharedpreferences.
My app also have a service, which starts on separate process and it needs to get a part of previously saved data from sharepreferences.
My main app lets say is in "com.foo.myMainApp" package. And my service is "com.foo.myMainApp/myService".
The problem is, that the service can't access main program's shared prefs, because they are saved in different location.
I'm struggling with this problem for a couple of days and can't find a way out.
Any help would be appreciated.
Use BroadcastReceivers.
in Service
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("ACTION");
broadcastIntent.putExtra("MESSAGE", "hello");
sendBroadcast(broadcastIntent);
In MainACtivity
register this broadcast receiver and your good to go receiving messages.
private class Message extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// get the message here
}
}
// register this onStart
registerReceiver(new Message(),new IntentFilter("ACTION") );
hope this helps :)
Thanks to a good bit of help I got from someone in my last question I am further ahead then before. Now I need to figure out how make this broadcast into a service. Or do I need to make a 3rd file and make a service out of that class?
Any help would be cool!
create a class which extends Service.
public class myService extends Service{
and use the Application Context object passed to your BroadcastReceiver to start the service.
context.startService(intent);
If you need to carry data over from your Intent object put a set of extras into the Intent object you create when you call
sendBroadcast(Intent intent);
I want to know whether an app can be a BroadcastReceiver and sender? Please expain with an example.
Application can't be a BroadcastReceiver. BroadcastReceiver is an application component. But answer to your question is yes: you can send broadcasts from one component and receive it in another.
For ex. in activity:
Intent intent = new Intent(...);
sendBroadcast(intent);
In receiver:
#Override
public void onReceive(Context context, Intent intent) {
// here is your intent
}
Yes, it can. An example can be found here.
If by app you mean activity, so yes you can but you will have to embed your BroadcastReceiver in your activity and register/unregister it yourself. That way, you just need to add your activity as Activity in the Manifest and you activity will be able to receive a broadcast and send broadcast as well.
I m not too sure how it behaves in term of life cycle though. You will need to look it up if it s what you want.
I have an application that get/send data from/to a remote DB on internet.
I need to get my application working in background mode, then i supose that i have to put all the send/get remote data in a service.....
but.... How can this service change values of variables and UI textfields of my activities?
i can't find any information about this, all the tutorials i am finding are of simple services that doesn't do something like that
can someone explain me how to do it please?
Use a BroadcastReceiver
In your Activity place the following code:
private BroadcastReceiver onBroadcast = new BroadcastReceiver() {
#Override
public void onReceive(Context ctxt, Intent i) {
// do stuff to the UI
}
};
Register the receiver in your onResume():
registerReceiver(onBroadcast, new IntentFilter("mymessage"));
Be sure to unregister in onPause():
unregisterReceiver(onBroadcast);
In your Service, you can post the message to the Application, which will be heard by your Activity:
getApplicationContext().sendBroadcast(new Intent("mymessage"));
If you need to, you can add data to the Intent's bundle to pass to your Activity as well.
my suggestion to you is, create a handler for the UI part which updates the text field or UI components.
Secondly, have notifications from the service to the activity by way of interface class.
I'm just getting into Android development, and I have a question about communicating between a receiver class and an activity class. I'm very new to JAVA and Android so I hope I don't sound too stupid. I'm developing an application where I intercept an SMS message and then based on various elements of that SMS I might delete it once it's been saved to the inbox. I have a receiver class that intercepts the txt message, and I am also able to delete messages from my inbox with code in the activity class using a button at the moment. The problem I have is communicating between the receiver class and the activity class where the code to delete a message resides. I tried putting that code directly into the receiver class but as I'm sure most of you already know the BroadcastReceiver class doesn't seem to support what I need to delete messages. I've been searching for an answer to this for a while, but haven't been able to find anything. Honestly I'm not sure I know enough about JAVA and Android to even recognize a solution if I saw it.
If you need to complete a job without an interface look into creating a Service, if you need user interface just start an Activity
You can use the Context parameter of the onReceive method of the receiver to start a new service/activity
You can use Extras to pass params between context. So you can put as extra the message id or entire message and pass it to your service/activity and deal it there.
You could implement the handling messages logic using an IntentService. When your receiver gets the new incomming message, start the IntentService passing an intent with the message data.
Receiver
onReceive(Context context, Intent intent) {
//Setup Intent
Intent i = new Intent(context, MyIntentService.class);
i.setAction(MyIntentService.HANDLE_MESSAGE);
//Pass data to intent
i.putExtra(MyIntentService.MESSAGE_DATA, data);
//Start Intent Service
context.startService(i);
}
MyIntentService
onHandleIntent(Intent i){
String action = i.getAction();
if(action != null && action.equals(MyIntentService.HANDLE_MESSAGE){
//Get data and implement message logic
}
}
Hope it helps.