How to safely send data from a service to running activity - android

Lets say I have a String test = "value" available in a service and want to send it to my running mainactivity.
How does one send that data safely?
Im looking for the most simple example possible, where the data can not be seen by other apps.
When I look at intents it says:
A broadcast is a message that any app can receive. The system delivers
various broadcasts for system events, such as when the system boots up
or the device starts charging. You can deliver a broadcast to >>>>other
apps<<< by passing an Intent to sendBroadcast() or
sendOrderedBroadcast().
However I want to send it to my app only as it contains private data.
In my service I tried:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(getString(R.string.notification_intent), "6");
startActivity(intent);
Where MainActivity.class is the class that should receive the intent, but in this way I get:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
How can I safely send data from a service to an activity?

Q: How do I send data from an Android service to my activity?
A: You have several alternatives:
1. Use intents:
How to get data from service to activity
Send msg from service:
private static void sendMessageToActivity(Location l, String msg) {
Intent intent = new Intent("GPSLocationUpdates");
// You can also include some extra data.
intent.putExtra("Status", msg);
Bundle b = new Bundle();
b.putParcelable("Location", l);
intent.putExtra("Location", b);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
Register to receive message in activity:
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
mMessageReceiver, new IntentFilter("GPSLocationUpdates"));
Custom message receiver in activity:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("Status");
Bundle b = intent.getBundleExtra("Location");
lastKnownLoc = (Location) b.getParcelable("Location");
...
I would NOT characterize this as "unsafe" - it can be a perfectly reasonable approach.
2. Have the activity bind to the service
https://developer.android.com/guide/components/bound-services
Service:
public class LocalService extends Service {
// Binder given to clients
private final IBinder binder = new LocalBinder();
...
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return binder;
}
Activity:
#Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, LocalService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
...
#Override
protected void onStop() {
super.onStop();
unbindService(connection);
mBound = false;
...
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection connection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
...
// To use the service, your client would call mService.someMethod()...
I'm not sure exactly what you're looking for, but example 1 is probably your best bet.
Here's a tutorial that might help give you more details/more ideas:
Basics Of Services In Android:
Part 1: Basics
Part 2: Binding services to an activity
Part 3: Using Messenger
https://developer.android.com/guide/components/broadcasts
Android provides three ways for apps to send broadcast:
The sendOrderedBroadcast(Intent, String) method sends broadcasts to one receiver at a time.
sendBroadcast(Intent) method sends broadcasts to all receivers in an undefined order. This is called a Normal Broadcast. This is more
efficient, but means that receivers cannot read results from other
receivers, propagate data received from the broadcast, or abort the
broadcast.
LocalBroadcastManager.sendBroadcast method sends broadcasts to receivers that are in the same app as the sender. If you don't need to
send broadcasts across apps, use local broadcasts. The implementation
is much more efficient (no interprocess communication needed) and you
don't need to worry about any security issues related to other apps
being able to receive or send your broadcasts.
Additionally:
https://developer.android.com/guide/components/broadcasts#restrict-broadcasts-permissions
Restricting broadcasts with permissions
Permissions allow you to restrict broadcasts to the set of apps that hold certain permissions. You can enforce restrictions on either
the sender or receiver of a broadcast.
Sending with permissions
When you call sendBroadcast(Intent, String) or sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int,
String, Bundle), you can specify a permission parameter. Only
receivers who have requested that permission with the tag in their
manifest (and subsequently been granted the permission if it is
dangerous) can receive the broadcast. For example, the following code
sends a broadcast:
Personally, I don't see any "security" issue at all with simply using an intent.
But if you want or need to, you can use the above techniques to further lock down communications.
'Hope that helps!

Related

Be notified when Service is started

I have a Service that scans for BLE devices. The Activity should show some data gathered by the Service.
A Receiver has been implemented, to be notified when the Bluetooth is enabled, so that we know when to start the Service.
If the Service is running, and the Activity is opened, it just executes bindService(). However, if the Service isn't running (because the Bluetooth is disabled), the App is opened and the Bluetooth is enabled, it won't bind because the binding process has already been skipped.
How can I be notified about the Service starting or automatically binding when started?
Thank you.
You can use the LocalBroadCastManager to send a broadCast from your service to your activity.
Helper to register for and send broadcasts of Intents to local objects within your process. This has a number of advantages over sending global broadcasts with sendBroadcast(Intent):
You can use localbroadcast reciever from your service.
In your service use these code
intent = new Intent("my-integer");
intent.putExtra("message",""+a);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
In your activity use this code
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String data = intent.getStringExtra("message");
if (!data.equals("0")) {
//Do something
} else {
//Do something else
}
}
}
};

How To Get Android Data From a Bound Service To a Extended Application

I have a class that extends Application, it gets a generated ID from a bound service.
public MyApp extends Application
{
public void onCreate()
{
super.onCreate();
Intent intent = new Intent(this, MyService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
String id = myService.getID();
}
}
My problem is, id results to null. How can I get the Id from the service to my application class? I assume, the compiler creates the application before starting the service.
You need to handle the onServiveConnected event on serviceConnection object.
Straight from the docs:
A client can bind to the service by calling bindService(). When it
does, it must provide an implementation of ServiceConnection, which
monitors the connection with the service. The bindService() method
returns immediately without a value, but when the Android system
creates the connection between the client and service, it calls
onServiceConnected() on the ServiceConnection, to deliver the IBinder
that the client can use to communicate with the service.\

peekService returns null even though startService does not

I'm trying to use an Android Service from a BroadcastReceiver. The Service lives in a different application than the BroadcastReceiver. My understanding is that the right way to do this is to first call Context#startService, followed by BroadcastReceiver#peekService. The call to startService seems to work correctly, as it returns the expected ComponentName. However, when I make the call to peekService, null is returned. Any thoughts on what I'm doing wrong?
Thank you! Here is a code listing with the relevant parts.
// The Service in question is com.tingley.myapp.MyService
// Note that this Receiver is in a different application
package com.tingley.myotherapp;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Start the Service
Intent serviceIntent = new Intent();
serviceIntent.setClassName("com.tingley.myapp", "com.tingley.myapp.MyService");
ComponentName componentNameOfStartedService = context.startService(serviceIntent);
Log.d("", "Started service name: " + componentNameOfStartedService);
// Get an IBinder to the Service
IBinder serviceBinder = peekService(context, serviceIntent);
Log.d("", "Got binder from peeking service: " + serviceBinder);
}
}
The Log statements print the following:
Started service name: ComponentInfo{com.tingley.myapp/com.tingley.myapp.MyService}
Got binder from peeking service: null
The documentation for peekService() does not fully describe the conditions required to get an IBinder. As Dianne Hackborn (Google Android engineer) explains in this post: "Calling startService() is not enough -- you need to have called bindService() for the same intent, so the system had retrieved the IBinder for it".

Not getting the flow of below program

public class MainActivity extends Activity {
public TextView batteryTxt;
private BroadcastReceiver receiver;
BroadcastReceiver mybroadcast = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int batterylevel = intent.getIntExtra("level", 0);
batteryTxt.setText("Battery Level: " + Integer.toString(batterylevel) + "%");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
batteryTxt = (TextView) findViewById(R.id.textView1);
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mybroadcast, filter);
}
}
Eventhough i have not used intent.putExtra() in above program, how intent.getIntExtra("level", 0) is working?
It's an IntentFilter, which sends an Intent to the BatteryManager to check ACTION_BATTERY_CHANGED. The BatteryManager then calls intent.putIntExtra to put the int that you're reciving in the Intent.
The intent is what your BroadcastReceiver receives from the system, when the action "ACTION_BATTERY_CHANGED" is performed. It's the information about battery level in this case, and "0" is the default value (in case there isn't extra named "level"). The intent is not created by any activity in this app.
You need to read the documentation on BroadcastReceivers and Intents.
http://developer.android.com/reference/android/content/BroadcastReceiver.html
http://developer.android.com/reference/android/content/Intent.html
Essentially these two mechanisms act as Android's preferred method of transferring state between applications and processes.
In short:
Broadcast Receivers are registered for Intents, and whenever an intent is "Fired" or "Launched" which corresponds to the "Mime-Type" for which your intent is registered, that Broadcast Receiver will be activated. At this time your Broadcast Receiver will be given the opportunity to handle state passed to it via the intent which was sent.
In your case:
You have created a Broadcast Receiver which is registered (presumably) for the Battery Service intents. That means every time the battery service sends out an Intent to all interested parties you'll receive an a message. The Battery Service includes in it's intent certain data which is useful to an application,service or process which is interested in the state of the Battery. In this case it is the "level".

send broadcast with combination of localbroadcastmanager sendorderedbroadcast

I'm wanting to implement what CommonsWare describes on this blog post: http://commonsware.com/blog/2010/08/11/activity-notification-ordered-broadcast.html. The post makes sense, and I was able to browse the example source here: https://github.com/commonsguy/cw-advandroid/tree/master/Broadcast.
What I'm curious about is if calling LocalBroadcastManager.getInstance(UnzipService.this).sendBroadcast(broadcast); inside of a service will still be picked up by a broadcast receiver of the type you define in your manifest.
In case what I'm asking isn't clear, what I'm trying to do is use the LocalBroadcastManager because the broadcasts from my service don't necessarily need to be seen system wide and I'd rather keep them private if possible, but I also want to display notifications if the user closes my app and the service is still running. Is there a way to combine both of those capabilities without sending a broadcast twice inside of the service?
(What I don't want to have to do) like:
LocalBroadcastManager.getInstance(UnzipService.this).sendBroadcast(broadcast);
sendOrderedBroadcast(broadcast);
What I'm curious about is if calling LocalBroadcastManager.getInstance(UnzipService.this).sendBroadcast(broadcast); inside of a service will still be picked up by a broadcast receiver of the type you define in your manifest.
No. LocalBroadcastManager only works with receivers registered with the LocalBroadcastManager singleton itself. Moreover, LocalBroadcastManager does not support ordered broadcasts, last I checked.
what I'm trying to do is use the LocalBroadcastManager because the broadcasts from my service don't necessarily need to be seen system wide and I'd rather keep them private if possible
So long as you are not using an <intent-filter> on your BroadcastReceiver in the manifest, and therefore are using an explicit Intent as the broadcast itself, your broadcast will only be seen by yourself and the bit of the OS that manages broadcasts. Other apps will not be able to spy upon it.
If you only have 2 objects that might handle your broadcast (in your case an Activity and a notifications controller), you can achieve the behavior of a ordered broadcast using only the LocalBroadcastManager.
The general idea is:
Set up your Service so that it broadcasts an Intent to your Activity with a particular action when you want to display your result
In your Activity create a BroadcastReceiver that handles your Service result Intent, and register it on the LocalBroadcastManager with an IntentFilter using the action from step 1
In your Service, when the results are available, try to send the result Intent using LocalBroadcastManager.getInstance(Context).sendBroadcast(Intent) this method returns a boolean that indicates if the broadcast has been sent to at least one receiver. If this boolean is false, it means that your Activity didn't handle your broadcast and you should show a notification instead.
In your service:
public UnzipService extends IntentService {
public static final String ACTION_SHOWRESULT = UnzipService.class.getCanonicalName() + ".ACTION_SHOWRESULT";
#Override
protected void onHandleIntent(Intent intent) {
Thread.sleep(500); // Do the hard work
// Then try to notify the Activity about the results
Intent activityIntent = new Intent(this, YourActivity.class);
activityIntent.setAction(ACTION_SHOWRESULT);
activityIntent.putExtra(SOME_KEY, SOME_RESULTVALUE); // Put the result into extras
boolean broadcastEnqueued = LocalBroadcastManager.getInstance(this).sendBroadcast(activityIntent);
if (!broadcastEnqueued) { // Fallback to notification!
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
.notify(SOME_ID, new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setTicker("results available")
.setContentText("results")
.build());
}
}
}
In your Activity:
public YourActivity extends Activity {
private BroadcastReceiver resultReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
processResult(intent); // Results Intent received through local broadcast
}
}
private IntentFilter resultFilter = new IntentFilter(UnzipService.ACTION_SHOWRESULT);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate();
Intent intent = getIntent();
if (UnzipService.ACTION_SHOWRESULT.equals(intent.getAction())) {
// The Activity has been launched with a tap on the notification
processResult(intent); // Results Intent contained in the notification PendingIntent
}
}
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this)
.registerReceiver(resultReceiver, resultFilter);
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(resultReceiver);
super.onPause();
}
private void processResult(Intent intent) {
// Show the results from Intent extras
}
}
This should be a complete working example.
I hope this helps who is trying to implement ordered broadcasts with LocalBroadcastManager from support library!
I understand you want to achieve the following:
"I have an event that occurs in the background. I want to update my activity, if the activity is on the screen. Otherwise, I want to raise a Notification." (#TheCommonsBlog)
You can achieve this behaviour by implementing a ResultReceiver.
Examples Restful API service and
http://itekblog.com/background-processing-with-intentservice-class/
What you basically do is instance a ResultReceiver in your Activity and pass it to the Service like a Parcelable parameter through an intent. Then, each time your service whats to update the UI, the service verifies the ResultReceiver object for NULL. If not NULL, you update the Ui via the onReceiveResult interface. Else, you raise a notification. When your activity dismisses, make sure you set the ResultReceiver on the Service to NULL.
Hope it helps.
PS: IMO, broadcasts are too much work and hard to control.
Use LocalBroadcastManager and broadcasts become easy to use.
I am not in favor of updating an Activity if an event occurs in the background. The user might already be doing something else in the Activity. Seems to me that a Notification is sufficient; it's always visible and remains until the user dismisses it. Gmail and Gcal work like this; Gmail doesn't update the current screen if a new mail comes in. If you want to know how to handle the task flow for handling a notification when the user is already in the app, see the Notifications API guide and also the [Notifying The User2 training class.

Categories

Resources