Can you use pending intents with localbroadcasts? - android

I am interested in using pending intents with local broadcasts.
To make myself clear, I am using the following for registering receivers and sending broadcast: android.support.v4.content.LocalBroadcastManager.
I have a local broadcast receiver in a service which works. I am trying to send local broadcasts from a custom notification layout which includes click-able items.
The local broadcast receiver - just receives simple action intents.
I was trying something like this to no avail:
Intent backintent = new Intent("GOTO_START_BROADCAST");
PendingIntent backIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, backintent, 0);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setOnClickPendingIntent(R.id.imageView1, backIntent);

I am interested in using pending intents with local broadcasts.
That is not possible.
The point behind a PendingIntent is to allow some other process to perform an action you request, such as sending a broadcast.
The point behind LocalBroadcastManager is to keep broadcast within your process.
Hence, a PendingIntent can issue a regular broadcast, but not one via LocalBroadcastManager.

Related

How to broadcast intent to multiple WidgetProviders

I have 2 widgets, each having it's own WidgetProvider. This is the class hierarchy:
AppWidgetProvider
|
CommonWidgetProvider
| |
WidgetAProvider WidgetBProvider
Both widgets have a button which updates the widget, but I would like to update all widgets (both WidgetA and WidgetB) no matter on which one you click.
I broadcast the update intent like this (in WidgetA):
don't worry about EXTRA_APPWIDGET_IDS containing only current widgets' id - I later retrieve all widgets' IDs in the Provider
// setup click on update icon
Intent intent = new Intent(data.context, WidgetAProvider.class); // how can I broadcast to both A AND B? Now it will broadcast only to A.
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {data.widgetId});
PendingIntent pendingIntent = PendingIntent.getBroadcast(data.context,
data.widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetUpdateContainer, pendingIntent);
How can I broadcast this update to both WidgetProviders?
Don't use the PendingIntent to directly send a broadcast because this way you always have to target explicitly one of the AppWidgetProviders.
Instead, use the PendingIntent to start an IntentService to send a broadcast to every BroadcastReceiver which uses a certain IntentFilter. You can inhibit third party BroadcastReceivers from receiving your broadcast by using signature permissions (see for example this post)

AlarmManager and WakefullBroadcastReceiver how private are the passed Extras?

I am trying to implement some alarm scheduling by using AlarmManager. Since when a alarm is triggered, I want to use a WakefulBroadcastReceiver which starts an IntentService to do some background job.
I have some questions related to security/privacy of the parameters passed for alarm's intents.
When setting a PendingIntent for a alarm I do something like:
Intent myIntent = new Intent(context, MyReceiver.class);
myIntent.putExtra("test", "testValue");
Bundle bundle = new Bundle();
bundle.putParcelable("bundleValue", bundleTestValue2);
myIntent.putExtra("test3", bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 323,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
My question is: how private are the values set as Extra for the pendingIntent of the alarm? Is there a chance of them getting read by some other app since is being used by Android Alarm's Manager after it is scheduled?
By having a receiver like
public class MyReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
startWakefulService(context, MyIntentService);
}
And on android manifest
<receiver
android:name=".receivers.MyReceiver"
android:exported="false"/>
<service
android:name=".MyIntentService"
android:exported="false"/>
And the service
public class MyIntentService extends IntentService {
#Override
protected void onHandleIntent(Intent intent) {
try {
//logic here
} catch{
} finaly{
MyReceiver.completeWakefulIntent(intent);
}
}
Call from within my Activity
sendBroadcast(new Intent(context, MyReceiver.class).putExtra(...);
Schedule a pending intent from an alarm
Intent myIntent = new Intent(context, MyReceiver.class);
myIntent.putExtra("test", "testValue");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 323,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
how exposed is this receiver to other apps? Can it react to other apps except mine? Does this rise any security possible issues?
Thank you.
Later edit:
Since the WakefullBroadcastReceiver seems the only way that guarantees that my service will get a partial wakelock, how can I make sure 100% that no other apps will be aware of my receiver and that my receiver won't get any other calls except ones made from my activity or from my set Alarm?
How would a WakefullBroadcastReceiver pattern works versus CommonsWare's WakefulIntentService ?
Later Edit:
I've finally managed to finish my implementation.
As stated before, both my WakefulBroadcastReceiver and IntentService are declared as exported="false" in my Android Manifest which from what I understand means that only my app can access them. Since the receiver is not exported, does it receive broadcasts from outside the app?
When setting an alarm I use this PendingIntent:
Intent myIntent = new Intent(context, MyReceiver.class);
myIntent.putExtra("databaseId", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 323,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
When calling from my Activity I do:
sendBroadcast(new Intent(context, MyReceiver.class).putExtra("databaseId", "1"));
Is this enough?
Privacy considerations related to Intent extras
In general, I would say that it is an insecure practice to put sensitive data in Intent.
In theory, if Intent can only be consumed by specific application (discussed later) then only that application should be able to see its contents. However, given a vast amount of Android devices and OS versions (incl. rooted devices and custom ROMs), I wouldn't count on it.
You did not specify the kind of sensitive data you'd like to pass in Intent extras, therefore I can only give these general recommendations:
Make sure you understand the "confidentiality level" of the data in question: is it secret, or just restricted? Or, maybe, it is public (in which case no protection needed)?
Try to find another approach that doesn't involve passing sensitive data in Intent (I myself never encountered such a need).
If you absolutely must pass sensitive data in Intent extras - consider encrypting it. The encryption model should be adequate to "confidentiality level" of the data, and to potential harm which could be done if that data is being intercepted (it can go all the way up to "server side" encryption).
Privacy/security considerations related to BroadcastReceiver
In general, BroadcastReceiver is a component which receives "system wide" broadcasts. The fact that the broadcasts are "system wide" should speak by itself about the level of privacy associated with them.
That being said, there is one mechanism by which developers can restrict broadcasts' scopes: custom permissions. Usage of custom permissions allows for two "levels of control" over broadcasts:
If broadcast requires specific permission then only if BroadcastReceiver has that permission will it receive the broadcast.
If BroadcastReceiver filters the incoming broadcasts by specific permissions, then only broadcasts carrying that permission will be delivered to that receiver.
While the above points can seem similar on the first sight, these are distinct schemes that can be used separately, or combined. The first scheme associates a broadcast with a permission (and the sender of that broadcast doesn't necessarily have that permission by himself), while the second scheme filters all broadcasts by specific permission (and the receiver must have that permission).
A better approach in your case
EDIT: this COULD BE a better approach if "wakefullness" wouldn't be part of the requirements. But it is. Since there is no guarantee that Service started by AlarmManager will get a chance to acquire a wake lock - this approach is not suitable for OP's case.
Please note that broadcasts and custom permissions were designed in order to introduce "decoupling" at application level - this scheme allows for sender application to be completely agnostic of the receiving application, as long as they agree on one custom permission (well, the same scheme is employed for pre-installed public permissions as well, but you wouldn't want to guard your sensitive data with a public permission).
In your case, however, sender and receiver are the same application. In such setting you don't really need all the trouble associated with broadcasts - just construct PendingIntent that starts the required Service inside your app, and you get it all at once:
PendingIntent and the associated Intent start a specific Service in your application (by name), therefore no other application can intercept it (theoretically, remember the above discussion).
Target Service can be non-exported, therefore no other application can access it in any way.
You welcome :)

Knowing about Sticky intent in Android

In android there are 3 kinds of Intents,
Intent,
Sticky Intent,
Pending intent.
so What is sticky intent?
Intent - is a message passing mechanism between components of Android, except for Content Provider. You can use Intent to start any
component.
Sticky Intent - Sticks with Android, for future broadcast listeners. For example if BATTERY_LOW event occurs then that Intent
will stick with Android so that any future requests for
BATTERY_LOW, will return the Intent.
Pending Intent - If you want some one to perform any Intent operation at future point of time on behalf of you, then we will use
Pending Intent.
An intent that is used with sticky broadcast, is called as sticky intent.
This intent will stick with android system for future broadcast receiver requests.
OR
sendStickyBroadcast() performs a sendBroadcast(Intent) known as sticky, i.e. the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent). One example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED. When you call registerReceiver() for that action -- even with a null BroadcastReceiver -- you get the Intent that was last broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.
Pending Intent: Pending Intent is actually an object which wraps an Intent to do some future work by another app.
It lets us pass a future Intent to another application and allows that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.
A PendingIntent is generally used in cases were an AlarmManager needs to be executed or for Notifications. A PendingIntent provides a mean for applications to work, even after their process exits.
PendingIntent uses the following methods to handle the different types of intents:
PendingIntent.getActivity() : Retrieve a PendingIntent to start an Activity
PendingIntent.getBroadcast() : Retrieve a PendingIntent to perform a Broadcast
PendingIntent.getService() : Retrieve a PendingIntent to start a Service
Example :
Intent intent = new Intent(this, SomeActivity.class);
// Creating a pending intent and wrapping our intent
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
// Perform the operation associated with our pendingIntent
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
Intent: Intent is basically a message passing mechanism between different components of Android, except for Content Provider. You can use intent to start any component in Android.
Sticky Intent: These are the Intents which sticks with Android for future broadcast listener.
Sticky Intent is also a type of Intent which allows communication between a function and a service sendStickyBroadcast(), performs a sendBroadcast(Intent) known as sticky, the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent).
One example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED. When you call registerReceiver() for that action — even with a null BroadcastReceiver — you get the Intent that was last Broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.
Intent : Intent is an asynchronous message which is use to communicate between the components in android , except Content Provider.
for example you can start activity by
startActivity(Intent intent);
Sticky Intent : sticky intents are associated with the android system for the future broadcast events.
Pending Intent : Those intent which you want to trigger at some time in future when you application is not alive.
An intent that is used with sticky broadcast, is called as sticky intent. This intent will stick with android system for future broadcast receiver requests.
Sticky Intent allows a communication between function and a service sendStickyBroadcast() performs a sendBroadcast(Intent) know as sticky, the Intent you are sending stays around after the broadcast is complete so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this works the same as sendBroadcast(Intent).

Android - Periodically broadcast an event

How do I periodically broadcast an event after user has installed my application.
I have implemented a broadcast receiver which will trigger a task onReceive. Now I need to periodically broadcast this event, so that the task will be executed periodically.
I know I need to use AlarmManager.
The code is somewhat like this.
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, 0, 5*1000, pendingIntent);
Q1) Where do I place this code if I do not have any Activity? Do I implement this as a service?
Q2) What should be the intent-filter be for my manifest in such a "self created event"?
PS: Actually my current broadcast receiver is waiting for the connectivity change event. The task is to periodically attempt/try to access the internet. But I cannot solely rely on connectivity change to trigger this task. That's why I need a timer which will either fire this event or trigger the method in onReceive().
I know in this case my intent-filter will be "android.net.conn.CONNECTIVITY_CHANGE"
Q1) Where do I place this code if I do not have any Activity?
You need to implement an activity. Nothing of your application will run on Android 3.1+ until the user has launched your activity.
So, you run this code:
The first time the user runs your activity
On a reboot (via a BOOT_COMPLETED BroadcastReceiver)
In the future when the user runs your activity, if you have determined that no broadcasts have gone out in too long, because the user force-stopped your application
Also, get rid of getApplicationContext(), as you should not need it here.
Q2) What should be the intent-filter be for my manifest in such a "self created event"?
You do not need one, as your Intent identifies the component.

multiple proximity alert based on a service

I have my application, which uses ProximityAlerts to fire up when the user enters on the designated radios.
My proximityAlert fires up a service which shows a Toast telling me that I've entered the designated radio of the events.
The problem comes that I cannot make my application to fire several registered locations, it only react to the last one that was registered and ignore the earlier registered events
Any help please? I have seen people using broadcast receiver but in my case I use a service instead.
To build an Android app with proximity alert, the main ingredient are Broadcast Receiver, Intent, PendingIntent, and AddProximityAlert.
The workflow should be as following: -
Register an IntentFilter with a Broadcast Receiver
Create an Intent
Get a PendingIntent that will perform a broadcast
Set proximity alert
In order to make your app fire several registered locations, your must first register the broadcast receiver with correct IntentFilter and create the respective Intent for each Proximity Alert. Remember to use unique intent action for each location as follows
.....
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT + uniqueID);
.....
Intent mIntent = new Intent(PROX_ALERT_INTENT + uniqueID);
......
For more information, you can read my post on the topic
There are a couple points to be made here:
Use a broadcast receiver to handle the PendingIntent that fires for a proximityAlert.
When creating the PendingIntent for a proximityAlert, the PendingIntent must be unique for each location. The easiest way to do this is to create a unique action string for each location.
Here's an example of a PendingIntent you can create for a proximityAlert:
int uniqueID = <unique_id_for_location>;
String intentAction = "PROXIMITY_ALERT." + uniqueID;
PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(),
uniqueID, new Intent(intentAction), PendingIntent.FLAG_CANCEL_CURRENT);
Then, add the proximityAlert for your location (I'm assuming you know how to do this part).
Next, register your broadcast receiver to handle the intent action string you created for your pendingIntent:
IntentFilter filter = new IntentFilter(intentAction);
registerReceiver( new ProximityItentReceiver(), filter );
Here, ProximityIntentReceiver is the name of the BroadcastReceiver class you have created.
It's all about the request_id in pendingIntent, you assign a requestid for pendingIntent..and if your first service uses the request id 1 means and second service uses the request id 2..so the first service not be killed and the service occurs concurrently ..
the count i used as request id in pending intent
int COUNT=Integer.parseInt(some_text.getText().toString());
if(COUNT==1)
{
PendingIntent proxi_pi=PendingIntent.getService(class_name.this,COUNT,service_class_intent,Intent.FLAG_ACTIVITY_NEW_TASK);
location_manager.addProximityAlert(location.getLatitude(), location.getLongitude(), radius, -1, proxi_pi);
}
else if(COUNT==2)
{
PendingIntent proxi_pi=PendingIntent.getService(class_name.this,count,service_class_intent,Intent.FLAG_ACTIVITY_NEW_TASK);
location_manager.addProximityAlert(location.getLatitude(), location.getLongitude(), radius, -1, proxi_pi);
}
else if(COUNT==so..on){
}
i hope this helps you..
Have you considered using a broadcast receiver then launching a service from the receiver?
When I first used them I found the system cached the results somewhat so had to set an ID (Which the docs claim not to be used). By doing this the intents weren't cached and I got the appropriate information to handle multiple proximity alerts.
Source code is available through github (See the bottom of this post) Gaunt Face - Multiple Proximity Alerts

Categories

Resources