I am trying to implement an Async task that gets a string from a url inside a service.
I am using a startedService which calls the Async task get the correct string, update a public DB class content and return to the main activity, the problem is that the list adapter which i need to notify of the change in the DB is at the main activity and i don't have access to it from the Service , I am a a noobie so I am not familiar with what better to use , started or bind service for that job, any sugestions ?
thank you
You can use BroadcastReceiver :
In your Activity:
#Override
public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("mybroadcast"));
}
// handler for received Intents for the "my-event" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
#Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
And to Broadcast from service use:
Intent intent = new Intent();
intent.setAction("mybroadcast");
sendBroadcast(intent)
Related
I am working on using the LocalBroadcastReceiver to send messages from an IntentService to an activity. I have a basic activity all the activities in my project inherit from that contains the activity code below. And a basic IntentService that is initialized by a WakefulBroadcastReceiver that contains the service code below.
In my activity I have:
#Override
protected void onCreate(Bundle savedInstanceState) {
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("push-message"));
super.onCreate(savedInstanceState);
}
#Override
protected void onPause(){
LocalBroadcastManager.getInstance(this).unregisterReceiver(
mMessageReceiver);
super.onPause();
}
#Override
protected void onResume(){
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("push-message"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
And in my service I have:
public class SimpleMessagerService extends IntentService {
public SimpleMessagerService() {
super("SimpleMessagerService");
}
#Override
protected void onHandleIntent(Intent intent) {
Intent newintent = new Intent("push-message");
// You can also include some extra data.
String message = intent.getExtras().getString("message");
newintent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
The service onHandleIntent is being triggered, and when I put a break point in it and evaluate the expression: LocalBroadcastManager.getInstance(this); I can see the mMessageReceiver in the mReceivers list; however when I put a breakpoint in mMessageReceiver's onReceive, I find that it is never being triggered.
More info:
It seems my service cannot actively do anything when it is called, but does not throw an exception. I tried saving my current context in the application file and throwing up a toast message from the service. The process seems to succeed, but the toast message never appears. This is what I have in the manifest for the service:
<service
android:name="packagename.services.SimpleMessagerService"
android:exported="false">
</service>
turns out a couple things where wrong. First exported needed to be true in the manifest. Second, because the service is an Intent service it operates in a different thread than the main activities, so in order to send a broadcast to them I have to make it look something like:
Handler mHandler = new Handler(getMainLooper());
mHandler.post(new Runnable() {
#Override
public void run() {
Intent newintent = new Intent("push-message");
newintent.putExtra("message", message);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(newintent);
}
});
So the main thread is hit. I hope this helps someone else with the same problem.
I'd like to credit rubenlop88 in the for posting this solution for a similar problem in the thread,
java.lang.RuntimeException: Handler (android.os.Handler) sending message to a Handler on a dead thread
I have a service that get data from an other application.
When I get date I send message to broadCast in order to refresh the UI.
The onReceive method is called many times and data displayed multiple times.
this is my code:
DataService.java
if(sizeLat == 1) {
sendMessage("Alerte1;");
}
else {
sendMessage("Alerte2;");
}
private void sendMessage(String message) {
Log.w("","==> send message");
Intent intent = new Intent("my-event");
// add data
intent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
MainActivity.java
// handler for received Intents for the "my-event" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("","Onreceiver");
String message = intent.getStringExtra("message");
if(message.equals("Alerte1")){
parentItems.add(message);
adapter.notifyDataSetChanged();
}}};
#Override
protected void onResume() {
Log.d(TAG, "On Resume");
super.onResume();
// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event"));}
How can I resolve the problem ?
Put broadcast register line in onCreate and unregister it in onDestroy() method. The line which you have to move from onResume() to onCreate is:-
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event"));}
Possibly, you have two instances of the activity living at the same time. Make a breakpoint on the message receiver and check the address of the instance of your activity class and see if they are different each time the onReceive is called.
There are a few reasons why you could have two instances living at the same time, but one of the most common is leaking context within the activity.
More on this topic.
I fixed same problem by unregister BroadcastReceiver in onPause method
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver,new IntentFilter("my-event")));
Register it in OnResume Method
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event"));}
I have an intent service in my app that is called from the main thread. The intent service is started upon clicking on a button. Once started, the service connects to the server and retrieves information.
I want to send broadcast to the activity once the data is retrieved. If I send it from the onHandleIntent(), the data might not be retrieved yet.
Can't I send the broadcast from the method that retrieves the data? If not, any alternatives?
code sample:
onHandleIntent()
{
myMethod();
//Here where it is expected to send the broadcast
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.example.intent.action.MESSAGE_PROCESSED");
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra("TAG",Message);
getApplicationContext().sendBroadcast(broadcastIntent);
}
MyMethod()
{
//Retrieving data from server, which returns Message.
//Here Where I want to send broadcast (Message is ready)
}
Thank you for your help.
You could also use a handler/runnable combo to act as a timer, so that you check whether the value is null or not before sending the broadcast. See this for how to do that.
edit:
It would look like this:
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
sendBroadcast();
}
};
onHandleIntent()
{
myMethod();
runnable.run();
}
MyMethod()
{
//Retrieving data from server, which returns Message.
//Here Where I want to send broadcast (Message is ready)
}
sendBroadcast(){
// If your value is still null, run the runnable again
if (Message == null){
handler.postDelayed(runnable, 1000);
}
else{
//Here where it is expected to send the broadcast
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.example.intent.action.MESSAGE_PROCESSED");
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra("TAG",Message);
getApplicationContext().sendBroadcast(broadcastIntent);
}
}
You could do the following in your activity class:
1- Create a BroadcastReceiver
private class MyBroadcastReceiver extends BroadcastReceiver
#Override
public void onReceive(Context context, Intent intent) {
//Get your server response
String server_response = intent.getExtras().getString("TAG");
//Do your work
}
}
2- Create an object in your activity (as a member of the activity)
MyBroadcastReceiver mReceiver= new MyBroadcastReceiver ();
3- Register it in your onResume() method and deregister it in your onPause() method.
#Override
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.intent.action.MESSAGE_PROCESSED");
registerReceiver(mChatReceiver, filter);
}
#Override
public void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
That should be enough!, Hope it helps!
I use Retrofit to connect my app to a webbackend. We use the asynchronous callback methods to do requests at different points in the app.
How would I add a listener that is notified every time a http-request fails.
My current idea is to create a custom callback class that all callbacks in my app must extend. This callback will inform my listener on every failure.
If I change all Interface to only except subtypes of this class and make the failure and success method final - with a callback to an abstract method - it should not be possible to miss a call in the whole application.
Is there a simpler way then this?
The easy approach to do this is using a callback, but you can also send an event to your activity using any event framework or using a local event broadcast such as:
to send the event you have to use something like:
private void sendMessage() {
Intent intent = new Intent("event-networkerror");
intent.putExtra("message", "data");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
To subscribe to the event:
public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("event-networkerror"));
}
// handler for received Intents for the "my-event" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
You can use the ErrorHandler to send the broadcast:
class MyErrorHandler implements ErrorHandler {
public Throwable handleError(RetrofitError cause) {
Response r = cause.getResponse();
// process error r.getStatus()
Intent intent = new Intent("event-networkerror");
intent.putExtra("status",r.getStatus());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
return cause;
}
}
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("...")
.setErrorHandler(new MyErrorHandler())
.build();
I am able to receive C2DM message fine but I want to send the data to a running activity, i.e when the activity is running, if the receiver receives C2DM message it is to send the data to the running activity. The code of receiver is (no bugs in the code):
public class C2dmreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action))
{
final String payload = intent.getStringExtra("key1");
Log.d("C2DM", "message = " + payload );
}
}}
I have tried like this inside the activity in an attempt to register the receiver in the activity so that the receiver can send data and the running activity can receive the data :-
C2dmreceiver c2dmr = new C2dmreceiver();
Registration.this.registerReceiver(c2dmr, new IntentFilter());
I don't know what to put inside the IntentFilter(), also what else I have to put in the code of the activity and the code of the receiver so that while the activity is running and some C2DM message comes the receiver can send the data to the running activity.
So, please tell me the code that is to put in the activity and in the receiver and may also be in the manifest so that the data from the receiver could be send to running activity.
Any advice is highly appreciated.
First of all it's not the best idea to subscribe c2dm receiver in activity. Do it in manifest. For passing data to activity you can create static string field in Activity and set you String there.
You can do something like this:
in Activity
public static YourActivity mThis = null;
#Override
protected void onResume() {
super.onResume();
mThis = this;
}
#Override
protected void onPause() {
super.onPause();
mThis = null;
}
In your BroadcastReceiver:
#Override
public void onReceive(Context context, Intent intent) {
...
if (YourActivity.mThis != null) {
((TextView)YourActivity.mThis.findViewById(R.id.text)).setText("received c2dm");
}
else {
...
}