Receiving same intent broadcast multiple times but only sending once - android

I have an intent that gets sent under a certain condition. The logs prove it is only sent ONCE, but the receiver is receiving it multiple times milliseconds apart.
10-01 10:09:59.201: I/System.out(13543): SENDER CHECKPOINT
10-01 10:09:59.211: I/System.out(13543): RECEIVER CHECKPOINT
10-01 10:09:59.291: I/System.out(13543): RECEIVER CHECKPOINT
I have confirmed that there is only ONE registration of the Broadcast Receiver and only ONE action filter used with the registered BR. It is only used in a single activity where a service running in another thread broadcasts the intent. Again, the logs substantiate the ONE broadcast and multiple receipts. What's more, the extras are null in the echo receipt.
How can this be? Is it possible the OS is echoing it?
Code that sends the broadcast:
private void sendBroadcast(boolean status, String message, String action){
System.out.println("SENDER CHECKPOINT");
Intent intent = new Intent(action);
Bundle bundle = new Bundle();
bundle.putBoolean("status", status);
bundle.putString("message", message);
intent.putExtras(bundle);
sendBroadcast(intent);
}
Code that receives the broadcast:
private class displayUpdate extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("RECEIVER CHECKPOINT");
};
}
Registration:
#Override
public void onResume() {
super.onResume();
try {
// just in case onResume is called w/o a pause
try{activity.unregisterReceiver(displayReceiver);}catch(Exception e){}
filterRefreshUI = new IntentFilter(REFRESH_UI);
activity.registerReceiver(displayReceiver, filterRefreshUI);
} catch (Exception e) {
e.printStackTrace();
}
}
Unregistration:
#Override
public void onPause() {
super.onPause();
try{unregisterReceiver(displayReceiver);}catch(Exception e){}
}
REALLY IT'S THAT SIMPLE! Yet the receiver fires TWICE!
In case you were wondering - Registration/Unregistration are handled in the onPause/onResume to prevent leaking memory if the App is killed by the OS.

Related

Android correct place to intialize LocalBroadcastReceiver for an activity

I have an android activity that has BroadcastReceiver as below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, intentFilter);
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("TAG", intent.getAction());
}
};
The problem is I am receiving message from network and depending on message type I create the activity or send Broadcast message to activity, since I receive message very fast the message type to create activity arrives right before(in few milliseconds) the message type to send Broadcast message to the same activity and I get an error handleWindowVisibility: no activity for token android.os.BinderProxy researching a bit I found that the activity might not be created correctly before I send the broadcast intent.
So I made the thread sleep for 3 seconds.
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Now everything works as expected but the above looks ugly and a bit hackish, is there a better way to send broadcast intent right before the activity creation?
Instead of creating the Activity and sending a broadcast Intent to it, just put the contents of the broadcast Intent in the Intent you use to start the Activity (as an "extra"). Then you don't need the 3 second delay, you just only send the broadcast Intent if the Activity is already running.

IntentService - Wakelock release issue

I have an alarm application.
Flow looks like this :
WakefulBroadcastReceiver(Acquires wakelock) --->> Intent service -->> startActivity
public class AlarmService extends IntentService {
protected void onHandleIntent(Intent intent) {
Intent activityIntent = new Intent(this, TriggeredActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
Basically WakefulBroadcaseReceiver starts an intent service using startWakefulService(). Inside intent service's onHandleIntent(), only work I am doing is further starting a new activity using startActivity(). That new activity is where I am using mediaPlayer in a loop, which sounds the alarm. That activity has a dismiss button, which waits for user click to stop the media player & activity finishes.
Now the problem I am facing is that after calling startactivity() inside intent service, I can not wait for TriggeredActivity to finish(no equivalent to startActivityForResult in Service) and then complete wakeful intent. Related link
startActivity(activityIntent);
WakefulBCastReceiver.completeWakefulIntent(intent); /* can't do here */
So I am not explicitly releasing wakelock here.
My question is will the wakelock be released automatically(link-to-death), when the process that is holding it is killed.
If yes, then in my particular scenario, I need not call WakefulBCastReceiver.completeWakefulIntent().
Yes, you need to use completeWakefulIntent.
You need to put your TriggeredActivity intent into EXTRAs.
#Override
public void onReceive(Context context, Intent intent) {
Intent intentService = new Intent(context, NotificationsIntentService.class);
// Inserting data inside the Intent
intentService.putExtra(NotificationsIntentService.EXTRA_NOTIF, new Intent(context, TriggeredActivity.class));
startWakefulService(context, intentService);
}
NotificationsIntentService.class
public class NotificationsIntentService extends IntentService {
private static final String TAG = "DebugNotifIntent";
public static final String EXTRA_NOTIF = "extra_notif";
public NotificationsIntentService(){
super(NotificationsIntentService.class.getSimpleName());
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent: ");
Intent extraIntent = intent.getParcelableExtra(EXTRA_NOTIF);
extraIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(extraIntent);
NotificationWakefulBroadcastReceiver.completeWakefulIntent(intent);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
I have managed to find a solution for my problem. I am now using a Messenger for message based cross process communication between intent service & triggered activity.
I am passing a handler - alarmServiceHandler, from intent service to activity through a messenger.
Handler alarmServiceHandler = new Handler(){
#Override
public void handleMessage(Message msg) {
if(msg.arg1 == 1) {
completedTriggeredActivity = true;
}
}
};
Inside onHandleIntent(), I am passing handler through Messenger object in intent's extra data.
Messenger alarmServiceMessenger = new Messenger(alarmServiceHandler);
Intent activityIntent = new Intent(this, TriggeredActivity.class);
activityIntent.putExtra("AlarmServiceMessenger", alarmServiceMessenger);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
while(!completedTriggeredActivity){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
WakefulBCastReceiver.completeWakefulIntent(intent);
In TriggeredActivity, I am retrieving messenger in Dismiss button's OnClickListener, just before calling finish() on the activity. And sending back a message to AlarmService with arg = 1, implying end of processing in triggered activity.
buttonDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Messenger alarmServiceMessenger = getIntent().getParcelableExtra("AlarmServiceMessenger");
Message alarmServiceMessage = Message.obtain();
alarmServiceMessage.arg1 = 1;
try {
alarmServiceMessenger.send(alarmServiceMessage);
} catch (RemoteException e) {
e.printStackTrace();
}
finish();
}
After starting triggered activity, I am putting AlarmService in sleep mode till boolean variable completedTriggeredActivity has not been set to true in handleMessage(). Once true, it means triggered activity has finished & now I can proceed with releasing wake lock.
I would be glad to receive comments about my approach & any suggestions towards a better solution to my problem, than the one I have deviced.

Closing running Application from BroadcastReceiver

In my App, there is a condition which check every day and if it gets true then I want my App get close in between the run like a crash and stack also gets clear .
I have try and tested many solutions but didn't find the one that works the way i wanted .
My BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
PreferenceForApp prefs = new PreferenceForApp(context);
Bundle bundle = intent.getExtras();
if (bundle!=null){
if(bundle.containsKey("exception")) {
// String e = bundle.getString("exception")
if(bundle.get("exception").toString().equalsIgnoreCase("http request failed with error_msg No Match Found")) {
prefs.setIsDeviceValidated(false);
prefs.setIsLogIn(false);
Log.i("Time", "Exception Occur");
Intent CSPIntent=new Intent(context,CSPLoginActivity.class);
CSPIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
CSPIntent.putExtra("close_activity", true);
Log.i("Time", "IntentExit");
context.startActivity(CSPIntent);
}
}
}
}
}
And code to finish in an Activity I am calling from broadcastReceiver:
if (getIntent().getBooleanExtra("close_activity",false)) {
Log.i("Time", "ExitCSPLogin");
this.finish();
}
This code is not closing App in between the run.
You need to register BroadcastReceiver in your activity and send broadcast to BroadcastReceiver when you want to close application.
In your Activity try this:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_CLOSE");;
BroadcastReceiver Receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
registerReceiver(Receiver, intentFilter);
in onDestroy() method of you Activity unregister BroadcastReceiver:
#Override
protected void onDestroy() {
unregisterReceiver(Receiver);
super.onDestroy();
}
Now when you want close application send broadcast to BroadcastReceiver:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_CLOSE");
sendBroadcast(broadcastIntent);
Hope this helps!
you have to check below condition in your app's mainActivity's onCreate method every time when user enter in your app. or in onResume if you want to to close your app immediately
if (!prefs.getIsDeviceValidated()) {
Log.i("Time", "ExitCSPLogin");
this.finish();
}
i assume you have more then one activity in your app, so insted of check above flag in every activity we 'll put it in main activity. allow user to use your app until he/she come at mainActivity
Note: create Broadcast Receiver for your App(add in manifest), not for specific activity

Android Service completely stopped

I have an android service that I need to stop and restart. In the service's onDestroy(), I have some clean up code that may take a while to execute.
Is there away to notify the application that's using the service that it's finish executing onDestroy()? Or something the application can do to check if onDestroy() has finish its execution?
Yes, use broadcast in onDestroy() method!
Intent intent = new Intent( "Service_destroyed" );
// You can also include some extra data.
intent.putExtra("message", "Service is destroyed!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
That will notify the other activities. Be sure to register the broadcast first in the activities and unregister when not needed anymore.
Register:
LocalBroadcastManager.getInstance(this).registerReceiver(mServiceDestroyedReceiver, new IntentFilter( "Service_destroyed" ));
Here is the method to catch the broadcast:
private BroadcastReceiver mServiceDestroyedReceiver= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d(TAG, "Service is destroyed message: " + message);
}
};
Unregister broadcast receiver:
LocalBroadcastManager.getInstance(this).unregisterReceiver( mServiceDestroyedReceiver);

How to send data to a running activity from Broadcast Receiver,

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 {
...
}

Categories

Resources