Android RequestLocationUpdates to pendingItent with extra bundle - android

I'm triggering a BoradcastReceiver when receiving a location update
PendingIntent pendingIntent = PendingIntent
.getBroadcast(this, 54321, intent, PendingIntent.FLAG_CANCEL_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient,
mLocationRequest, pendingIntent);
And my Receiever
public static class LocationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean hasLocation = LocationResult.hasResult(intent);
}
}
If I run the above code eveything works fine hasLocation is always true, perfect.
But If I wish to pass some variable to the Receiver so I do:
Intent intent = ..
intent.putExtra("test", "hello");
PendingIntent pendingIntent = PendingIntent
.getBroadcast(this, 54321, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Bit now in the reciever LocationResult.hasResult(intent); is always false
Is this a bug? Is there a workaround to this? How can I pass variable to the reciever?

I found your question while googling. I will share my solution in case anyone else finds this question like me.
First, this is my situation, which is similar to yours:
requestLocationUpdates() stores the location data in the mExtras field of the intent. For some reason, if I add another Extra to the Intent using Intent.putExtra(), the location data is not added. So onHandleIntent() is called, but the intent is missing the location data. If I don't add any Extras, then the location data comes through and everything is fine.
My workaround:
I used Intent.addCategory() and getCategory() to do the exact same thing as putExtra("myExtraName", String). If you want to pass other data types, convert them to a string then parse them in onHandleIntent().
My environment:
I am using Play Services version 11.0.4 and FusedLocationProviderClient, since FusedLocationProviderApi has been deprecated recently. The FusedLocationProviderClient.requestLocationUpdates() documentation doesn't seem to address this.

Related

Why android FusedLocationApi broadcasting same location within a period of few milliseconds?

I have an android app in which I have used FusedLocationApi to get location updates of the user.
Following is the scenario:
1. I have a singleton Watcher class in which I define the pending intent to get the locations even when app is in background. Following is the code:
private PendingIntent pendingIntent;
private Watcher() {
Intent locationIntent = new Intent(context, Receiver.class);
pendingIntent = PendingIntent.getBroadcast(
context, 007 /*requestcode*/, locationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
Then, when I have successfully connected location services, I request for location updates:
LocationServices.FusedLocationApi.
requestLocationUpdates(googleApiClient, locationRequest, pendingIntent);
Now, whenever Receiver class which extends WakefulBroadcastReceiver gets the location update, it starts the Service.class. The service class extends IntentService.
Following is the code:
#Override
synchronized protected void onHandleIntent(final Intent intent) {
if(LocationResult.hasResult(intent)) {
LocationResult locationResult= LocationResult.extractResult(intent);
Location location = locationResult.getLastLocation();
printLocation(location);
}
So my question is, given the above steps, why does the onHandleIntent gets woken up by the LocationReceiver multiple times within a period of 5 milliseconds. The lat, lng and accuracy are all the same. I have defined the
setFastestInterval(5 seconds); and
setInterval(1 minute);
Also the location accuracy is BALANCED_POWER_ACCURACY;
In my app, the
LocationServices.FusedLocationApi.
requestLocationUpdates(googleApiClient, locationRequest, pendingIntent);
do gets called multiple times. But according to the documentation: "Any previously registered requests that have the same PendingIntent (as defined by equals(Object)) will be replaced by this request." And I am using the same pendingIntent object to call requestLocationUpdates.
Thanks in advance.
Hi Kanika you can set the minimum displacement before passing your location request object this will help you to get updates only when you have certain minimum displacement. you can use below method for doing same.
setSmallestDisplacement(float smallestDisplacementMeters)
Also FYI api reference link , https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest

Android Notification Action is not fired (PendingIntent)

I am trying to add an Notification action item in my app which is a music player. When a stream is started a notification should be triggered and an stop button for the stream should be displayed in the notfication. The notification working fine so far, I am having trouble with the stop action item. Here is how it is declared in the service starting the stream:
Intent stopIntent = new Intent(this, MusicPlayerNew.class);
stopIntent.putExtra("STOP", "STOP");
PendingIntent stopPendingIntent = PendingIntent.getActivity(this, 0,
stopIntent, PendingIntent.FLAG_UPDATE_CURRENT, null);
mBuilder.addAction(R.drawable.ic_stat_stop, "Stop", stopPendingIntent);
Now in the onResume()-method of my activity I check with getIntent().getStringExtra() for the "STOP" extra, but the intent I retrieved via getIntent() has no extras set :(
I also tried to check to send an broadcast (i have a broadcast receiver working to communicate from the service to the activity)
Intent stopIntent2 = new Intent(MusicPlayerNew.STOP_MEDIAPLAYER);
PendingIntent stopPendingIntent2 = PendingIntent.getBroadcast(this, 0,
stopIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.ic_stat_stop, "Stop", stopPendingIntent2);
Now this works if the activity is currently in the foreground. If the activity is in the background the stop button does nothing :(
EDIT:
I have the BroadcastReceiver in my Activity as a private class
private class DataUpdateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
..
}}
In the onResume() register my app for this receiver:
intentFilter = new IntentFilter(STOP_MEDIAPLAYER);
registerReceiver(dataUpdateReceiver, intentFilter);
onPause()
unregisterReceiver(dataUpdateReceiver);
Now if I remove the unregistering from the onPause()-method the broadcast is received even if the app/activity is not in the foreground anymore. But is this the right way to do it? I got this register/unregister-stuff from a tutorial on the web i think..
This is very late answer but it may help someone:
You should choose the right kind of Pending intent based on the intent you want to run. Here are some Examples:
For Activity use below:
Intent i = new Intent(this, YourActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
For Service use below:
Intent i = new Intent(this, YourService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
For Broadcast Receiver use below:
Intent i = new Intent(this, YourReciver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
You may need to change the request code and Flags if required
I find solution in this thread on google code https://code.google.com/p/android/issues/detail?id=61850
To fix it you must add PendingIntent.FLAG_CANCEL_CURRENT flag to your PendingIntent.
PendingIntent stopPendingIntent2 = PendingIntent.getBroadcast(this, 0,
stopIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
I ran into this problem today. In my case it was using cached intent extras from a previous instance of the intent as all the parameters for the pendingIntent constructors was same. I found two solutions for this...
Using FLAG_CANCEL_CURRENT as mentioned by Nik.
Passing an unique requestCode to the pendingIntent as follows
PendingIntent pi = PendingIntent.getService(this, UNIQUE_ID, pi, 0);
In my case, the second method solved the problem as I need to keep the previous notifications alive. May be this will help someone with similar issue.
I ran into this problem today and it was caused by the activity not being registered or added to AndroidManifest.xml. I thought I had it in there but it wasn't. Also, no errors were being logged by trying to invoke the action with its intent.
I figured this out by creating an intent and then calling startAcitivty(intent) without using a notification. It then gave me an error stating the activity was likely missing from the manifest.
If none of the other answers solve your problem then hopefully this will. Usually tricky problems are the result of something simple and silly.
Do not use explicit Intent
In my case, I created a dynamically context registered BroadcastReceiver within my Service class for listening the notification actions.
class MyService:Service(){
private val receiver: BroadcastReceiver = NotificationActionReceiver()
...
inner class NotificationActionReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
...
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
...
registerReceiver(receiver,IntentFilter("SOME_ACTION"))
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
...
unregisterReceiver(receiver)
super.onDestroy()
}
PendingIntent with explicit Intent
val nextIntent = Intent(this, NotificationActionReceiver::class.java) //Explicit Intent
val nextPendingIntent: PendingIntent= PendingIntent.getBroadcast(this,0x11,nextIntent, PendingIntent.FLAG_CANCEL_CURRENT)
However with this setup, the BroadcastReceiver never triggered.
In order to make it work I need to replace my explicit intent with the implicit one
So all I did was,
val nextIntent = Intent("SOME_ACTION") //Implicit Intent
val nextPendingIntent: PendingIntent= PendingIntent.getBroadcast(this,0x11,nextIntent, PendingIntent.FLAG_CANCEL_CURRENT)
NOTE: Since the BroadcastReceiver is dynamically context registered, you don't have to worry about restrictions on implicit intents
More than using broadcast receiver, you should use a service and declare a new action in your service this way:
public final String ACTION_STOP = "yourpackagename.ACTION_STOP";
And then create your intents like this:
Intent stopIntent = new Intent(this, YourService.class).setAction(YourService.ACTION_STOP);
PendingIntent stopPendingIntent = PendingIntent.getService(this, 0, stopIntent, 0);
Of course, stop playback in your service's function startCommand, if the intent's action equals ACTION_STOP.
This should do the trick ;)
You do not receive the broadcast when it is in the background because you are unregistering in onPause. Move the unregisterReceiver code to onDestroy function. This will be called only when the activity is destroyed. Or you can unregister once the expected event has occurred.
There are multiple questions here:
Part 1: Why is your intent not receiving the "STOP" extra?
Though it is not seen in the code you have provided, I wanted to confirm if you are using the flag FLAG_ACTIVITY_SINGLE_TOP for the notification intent ? If so, the intent you receive in your activity would be the intent that started the activity and hence the "STOP" extra will not be available. You will need to extend the onNewIntent() is this case (where the new intent is sent). More info here.
If you have not used FLAG_ACTIVITY_SINGLE_TOP, then it means that a new activity is created when notification is tapped, in which case the Intent must have the "STOP" parameter. If you can provide me all relevant code, I can help you better.
Part 2: Using Broadcast Receiver
This is not straight forward, as you already discovered. You would need to unregister in onDestroy and if your activity is closed by the user, the onDestroy may be called and your broadcast receiver may not active at the time the notification is tapped by the user. If you dont unregister at all, it may seem to be working, but this is a memory leak, GC may clean up anytime which could lead to a crash in your program, ie., you MUST unregister. If you need to go with broadcast receiver approach, you need to have a service to do this and service comes with its own pitfalls -> restart by system, battery drain etc. I would strongly recommend you go with your first approach.
I had a very similar issue but a very different solution. Pending intent is also not fired if you have declared <service android:enabled="false"></service> in your manifest.xml file.
Replace from android:enabled="false" to android:enabled="true"
This might not be a direct issue of the problem. But if you create the service in android studio using default template it automatically adds these properties to the service.
For me, the solution was to set the flags of the intent :
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);

Issues alarm manager in every 1 min Android?

I want to make a service which fire alarm manager in every 1 min interval..But
my Alarm run once(first time only).
I follow Lalit Answer
private class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getBaseContext(), "Alarm", Toast.LENGTH_LONG).show();
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, ConnectionReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1*60*1000, pi);
}
}
Juts register broadcast receiver for:
http://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_TICK
Try this code in Your broadcast receiver's onReceive method
AlarmManager mgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
60000+System.currentTimeMillis(),
getPendingIntent(ctxt));
and you can get pending intent
private static PendingIntent getPendingIntent(Context ctxt) {
Intent i=new Intent(ctxt, AReceiver.class);
return(PendingIntent.getBroadcast(ctxt, 0, i, 0));
}
where AReceiver class is for start service like Notification
it is working fine in my app so i hope it helps you
I know this question already has an answer, but for others who have had the same issue but need to use AlarmManager. The reason why it only runs once is because the new PendingIntent you create does not get recreated, but rather is reusing the one before it. So in other words, the reason why your alarm only ran once was because it kept reusing it. Using the flags to refresh the intent extras if there are any should be doing the trick, but that also does not work.
A trick to use to make sure it does not reuse the PendingIntent and ultimately the Intent you provide is to use setAction() and give it some unique "Action". I did it like this:
intent.setAction("com.yourname."+System.currentTimeMillis());
As you see this makes sure its unique. Though, the above accepted answer it the best approach, if someone does not want that, they need to understand why and how to remedy that issue. Hope it helps anyone else.

PendingIntents keep caching same object

i ve been facing some problems trying to pass data through intents and pending intents to a BroadcastReceiver, concerning proximity alerts. More specifically, am trying to pass an object, that among others holds the user's constantly changing position. I ve tried various tactics being proposed here (and not only) but none worked, resulting to either null values or same-as-first-time created intents, when the intent is retrieved on the BroadcastReceiver's side. Tactics used:
Flagging the intent that carries the object with:FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_CLEAR_TOP+FLAG_ACTIVITY_SINGLE_TOP
Result:Null values on the BroadacastReceiver's side
Flagging the pending intent created using the initial intent, with:FLAG_UPDATE_CURRENT or FLAG_CANCEL_CURRENT
Result:Null values on the BroadacastReceiver's side
Acquiring a random ID for intent or the pending intent using System.currentTimeMillis();
Result:Intents are not fired or received at all
Nothing described above. Result:Retrieving the same initial value every time.
Code for the calling method (stripped from any experimentations/producing null values):
private void setProximityAlert(MyCar myCar) {
String locService = Context.LOCATION_SERVICE;
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(locService);
float radius = myCar.getMyCarRadius();
long expiration = myCar.getMyCarExpiration();
myService.setMyDriverLat(userLat);//setting user's position
myService.setMyDriverLng(userLng);//setting user's position
Intent intent = new Intent(myCar.getMyCarName());
intent.putExtra("myCar",myCar);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, -1, intent, 0);
locationManager.addProximityAlert(myCar.getMyCarLat(), myCar.getMyCarLng(), radius, expiration, proximityIntent);
}
Code for the calling method that sets the intent filter and registers the BroadcastReceiver:
public void addNewCarPoint (MyCar myCar){
IntentFilter filter = new IntentFilter(myCar.getMyCarName());
registerReceiver(new ProximityAlertReceiver(), filter);
setProximityAlert(myCar);
}
Code for the BroadcastReceiver's side:
public class ProximityAlertReceiver extends BroadcastReceiver {
#Override
public void onReceive (Context context, Intent intent) {
MyCar myCar=(MyCar)intent.getParcelableExtra("myCar");
driverLoc=(String)Double.toString(myCar.getMyDriverLat());
Toast.makeText(context, userLoc, Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, MyCarDiscoveryPrompt.class);
context.startActivity(i);//firing intent
}
public void intentDataLoader(){
}
}
Any ideas would be more than welcome.
Thank you in advance.
Hmm i think i ve found something:
I placed the BroadcastReceiver (ProximityAlerReceiver), used to detect proximity alerts in the same class (MyCarTracking.class), where the LocationListener.class is located. This,
provides immediate access to fresh location updates, creating a new intent wrapped in a new pendingIntent to be fired to the BroadcastReceiver (only when the proximity criteria are met).
flags:FLAG_ACTIVITY_NEW_TASK+FLAG_ACTIVITY_SINGLE_TOP and FLAG_CANCEL_CURRENT on intent and pendingIntent, were kept respectively. More specifically:
Code for LocationListener:
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);//update application based on new location
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);//update application if provider disabled
}
public void onProviderEnabled(String provider){
// Update application if provider enabled
}
public void onStatusChanged(String provider, int status, Bundle extras){
//update application if provider hardware status changed
}
};
Code for setProximityAlert() method:
private void setProximityAlert() {
String locService = Context.LOCATION_SERVICE;
Context context =getApplicationContext();
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(locService);
float radius = myCar.getMyCarRadius();
long expiration = myCar.getMyCarExpiration();
Intent intent = new Intent(CAR_DISCOVERED);
intent.putExtra("myCar",myCar);
locationManager.getLastKnownLocation(provider);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//flagging intent
PendingIntent proximityIntent = PendingIntent.getBroadcast(context, -1, intent, PendingIntent.FLAG_CANCEL_CURRENT);//flagging pendingIntent
locationManager.addProximityAlert(myCar.getMyCarLat(), myCar.getMyCarLng(), radius, expiration, proximityIntent);//setting proximity alert
}
This solution works producing fresh intents with fresh location updates.
Thank you all for your help and your interest :)
Try adding
intent.setData(uri);
where uri is some unique value for each pending intent
I've been struggling with this problem as well. It took me a whole night to find that a weird bug I had was related to this issue.
Here's a good discussion on google code on the subject: http://groups.google.com/group/android-developers/browse_thread/thread/b2060b27c8934921
I've solved all my problems by (ab)using both the uri in SetData and the (reserved) request code in PendingEvent.GetWhatever.
I'm also using FLAG_CANCEL_CURRENT on my intents and making sure only pendingintents that share the same purpose get the same data, action and uri.
Hope it helps a little bit.

Android: How to get location information from intent bundle extras when using LocationManager.requestLocationUpdates()

I am trying to use Android's LocationManager requestLocationUpdates. Everything is working until I try to extract the actual location object that in my broadcast receiver. Do I need to specifically define the "extras" to my custom intent so that the Android LocationManager before I pass it to requestLocationUpdates so it knows how to add it into the intent, or will it create the extras-bundle regardless when it passes the fired intent to the broadcast receiver?
My code looks like this:
Intent intent = new Intent("com.myapp.swarm.LOCATION_READY");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, 0);
//Register for broadcast intents
int minTime = 5000;
int minDistance = 0;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
minDistance, pendingIntent);
I have a broadcast receiver that is defined in the manifesto as:
<receiver android:name=".LocationReceiver">
<intent-filter>
<action android:name="com.myapp.swarm.LOCATION_READY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
And the broadcast receiver class as:
public class LocationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Do this when the system sends the intent
Bundle b = intent.getExtras();
Location loc = (Location)b.get("KEY_LOCATION_CHANGED");
Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show();
}
}
My "loc" object is coming up null.
OK, I managed to fix it by changing the KEY_LOCATION_CHANGED in the broadcast receiver code to:
public class LocationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Do this when the system sends the intent
Bundle b = intent.getExtras();
Location loc = (Location)b.get(android.location.LocationManager.KEY_LOCATION_CHANGED);
Toast.makeText(context, loc.toString(), Toast.LENGTH_SHORT).show();
}
}
i ve tried to code and test the solution you proposed, since i am facing similar problems concerning proximity alerts and intents carrying location objects. According the information you provided, you managed to overcome the null object retrieval, on the BroadcastReceiver's side. What you might did not observe is that now you should be receiving the same location as the one your intent was first created (also seen it as: intent caching problem).
In order to overcome this problem, i used FLAG_CANCEL_CURRENT, as being proposed by many people here and it works pretty fine, fetching fresh (and juicy :P) location values. So the line defining your pending intent should look like this:
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
However, you can ignore this if:
your purpose was just to receive a location value once
you managed to overcome it in some other way not visible in your post

Categories

Resources