Get Activity's widget from service - java

The main activity has an AlarmManager which calls a Service every X minutes. I need a way that the methods inside the service's class can update a TextView in the main activity, but I dont know how to get the TextView's object in the service. Is there any way?
Here is part of the code:
Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class);
pintent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 60000, pintent);

In your alarm service you have a onReceive method
public void onReceive(Context context, Intent arg1) {
String data = "haha";
if (data.isEmpty() == false && data.contentEquals("") == false) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "sing";
CharSequence message = data;
//get the activity
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
data, System.currentTimeMillis());
notif.defaults |= Notification.DEFAULT_SOUND;
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
Method call:
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ReminderReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
100000, pendingIntent);
}

I had the same need, in an accelerometer-reading service and an app that would start/stop the service and display the current average accelerometer magnitude.
I used Service Binding to allow the Service to send a Message to the Activity, containing the accelerometer value.
See http://developer.android.com/guide/components/bound-services.html#Messenger for background on Binding. For a good example, see the API Samples' MessengerServiceActivities and MessengerService classes.
I did the following. I've left out the mundane details (such as Synchronization to avoid races) for clarity. Notice that I use bind() as well as StartService(). The bind() is for sending messages between the Activity and Service; the StartService() is so the Service keeps running after the Activity exits.
Basically, the Activity and Service exchange Messengers that will allow each to send Messages to the other. The Activity sends custom Messages to the Service in order to Subscribe to Service Messages or Unsubscribe from Service Messsages. When the Service wants to send data to the Activity, it sends a custom Message to the Activity's Messenger. The Activity, on receiving such a message, displays the new value to the user.
The answer that suggested using Notifications uses a simple way to get data onto the screen. This more complex Message-passing answer is needed if you want to display data in your Activity (vs. the Notification bar).
I apologize for the length of the example code below, but there are a lot of necessary details to convey.
In my Activity, named AccelServiceControl:
private FromServiceHandler handler; // My custom class for Handling Messages from my Service.
private Messenger fromService; // For receiving messages from our Service
...
public void onCreate(Bundle savedInstanceState) {
...
handler = new FromServiceHandler(this);
fromService = new Messenger(handler);
...
}
protected void onResume() {
...
// While we're in the foreground, we want to be bound to our service.
// onServiceConnected() will return the IBinder we'll use to communicate back and forth.
bindService(new Intent(this, AccelService.class), this, Context.BIND_AUTO_CREATE);
}
protected void onPause() {
...
// Note: By itself, this doesn't stop the Service from sending messages to us.
// We need to send a custom Unsubscribe Message to stop getting messages from the Service.
unbindService(this);
}
public void onClick(View v) {
// Send a custom intent to start or stop our Service.
if (buttonStart == v) {
startService(new Intent(AccelService.ACTION_START));
} else if (buttonStop == v) {
startService(new Intent(AccelService.ACTION_STOP));
}
...
}
public void onServiceConnected(ComponentName name, IBinder service) {
...
// Ask our Service to send us updates.
toService = new Messenger(service);
Message msg = Message.obtain(null, FromClientHandler.MSG_SUBSCRIBE); // our custom Subscribe message
msg.replyTo = fromService;
try {
toService.send(msg);
} catch (RemoteException e) {
// Failed because the Service has died.
// We handle this in onServiceDisconnected().
}
}
In the Activity's custom Message Handler:
....
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_ACCEL_UPDATE:
...
Bundle bundle = msg.getData();
double accelValue = bundle.getDouble(ACCEL_UPDATE_VALUE);
...then display the new accelValue in, for example, a TextView.
...
}
}
In the Service, named AccelService:
...
private Messenger fromClient; // For receiving messages from our Client(s).
private FromClientHandler handler; // needed just for unlinking at in onDestroy().
// Since we have only one Client, we store only one Activity's Messenger
private Messenger subscribedMessenger;
public void onCreate() {
...
handler = new FromClientHandler(this);
fromClient = new Messenger(handler);
}
public void onDestroy() {
// Unlink ourselves from our Handler, so the Garbage Collector can get rid of us. That's a topic in itself.
handler.unlink();
....
}
public int onStartCommand(Intent intent, int flags, int startId) {
...
int returnValue = START_NOT_STICKY;
String action = intent.getAction();
if (ACTION_START.equals(action)) {
doActionStart();
returnValue = START_STICKY;
} else if (ACTION_STOP.equals(action)) {
...
// Our Service is done
stopSelf();
}
...
return returnValue;
}
public IBinder onBind(Intent intent) {
// Hand back a way to send messages to us.
return fromClient.getBinder();
}
...when we want to send data to the Activity:
Message msg = Message.obtain(null, FromServiceHandler.MSG_ACCEL_UPDATE);
Bundle bundle = new Bundle();
bundle.putDouble(FromServiceHandler.ACCEL_UPDATE_VALUE, avgAccel);
msg.setData(bundle);
try {
subscribedMessenger.send(msg);
} catch (RemoteException e) {
// Failed because the Client has unbound.
subscribedMessenger = null;
}

Related

Why does foreground service stop working when device go into sleep mode

I want to create an app that is constantly checking for location change and put the current location in the firebase (e.g. an app for runners).
Unfortunately the foregroundservice is being stopped or paused every time the device go into sleep mode.
For starters I wanted to create a foreground service that is continuously writing information to the base (that would be a time stamp or a simple string) every second.
After some time it just stops writing to firebase without calling stopself().
The service is working fine on the emulator (even if put to sleep), but stops when tested on a real device – in my case Huawei, Android 8.1.0.
What should I do to force service to run in every state of the device?
My MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(this, MyService.class);
intent.putExtra("action", "start");
startForegroundService(intent);
}
else {
Intent intent = new Intent(this, MyService.class);
intent.putExtra("action", "start");
startService(intent);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(this, MyService.class);
intent.putExtra("action", "stop");
startForegroundService(intent);
}
else {
Intent intent = new Intent(this, MyService.class);
intent.putExtra("action", "stop");
startService(intent);
}
}
}
MyService:
public class MyService extends Service {
int i =0;
private String CHANNEL_ID = "2345";
#Override
public void onCreate() {
super.onCreate();
}
public MyService() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(1000, createNotification());
String action = intent.getExtras().getString("action");
switch (action){
case "start":
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
myfunction();
handler.postDelayed(this, 1000);
}
};
break;
case "stop":
stopfunction();
break;
}
handler.postDelayed(runnable, 1000);
return START_NOT_STICKY;
}
private void stopfunction() {
stopSelf();
}
private void myfunction() {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("locations");
myRef.child("location").setValue(i);
i++;
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
#RequiresApi(Build.VERSION_CODES.O)
private void createChannel(){
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, getString(R.string.infoTxt),
NotificationManager.IMPORTANCE_HIGH);
channel.setShowBadge(false);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(channel);
}
private Notification createNotification(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
createChannel();
}
Intent notificationItent = new Intent(this, MainActivity.class);
notificationItent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0, notificationItent, 0);
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setColor(ContextCompat.getColor(this, android.R.color.background_dark))
.setContentIntent(intent)
.setSmallIcon(R.drawable.ic_launcher_background)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOnlyAlertOnce(true)
.setContentTitle("GPS Location")
.build();
}
}
I've tried everything: service, foreground service, broadcast receiver, jobSheduler, WorkerManager – nothing helped. Then I found it’s a new HUAWEI feature called “power-intensive app monitor “. It kills every app that runs in the background for a long time unless user gives special permissions to it.
The path to do this:
Settings -> Security & privacy -> Location services -> recent location requests: YOUR APP NAME -> Battery -> uncheck Power-intensive prompt, App launch: Manage manually: check all three positions: Auto-launch, secondary launch, run in background.
I don’t know is there a way to do this programmatically. I think the best way is to create a sort of help activity and explain the user what to do if application won’t work.
Foreground services generally should be used for task which require user attention such as visual processes.
use Background service instead

Android Service member variable goes null after running for some time

I'm trying to create a foreground service to communicate with a bluetooth device and I've done so using this structure:
Using a service, which also registers a BroadcastReceiver as a private member variable (to handle Gatt events), then the service is started using the startService command. Then, I bind to that service inside my current Activity. Inside the onStartCommand, I assign a BluetoothDevice object (my_device) which was passed as an extra in the onStartCommand intent to a member variable in the service. However, after the service has run and the member variable is assigned, somehow it loses that member variable instance and it becomes null again when I try to do anything in the BroadcastReceiver that handles BLE Gatt Events.
public final class My_BLEService extends Service {
private My_BLEDevice my_device = null;
private BroadcastReceiver gattUpdateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Problem: my_device is null inside this function
}
};
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Create the Foreground notification
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
// Get BluetoothDevice object
if(this.my_device == null) {
// Initialize the my_device object
BluetoothDevice bleDevice = intent.getParcelableExtra(My_AppManager.RFSTAR);
this.my_device = new my_BLEDevice(bleDevice);
}
// Connect to the bluetooth device first thing:
if(!this.isConnected) {
this.connect();
}
// Start up the broadcast receiver here
if(!this.receiverRegistered) {
registerReceiver(this.gattUpdateReceiver, this.bleIntentFilter());
this.receiverRegistered = true;
}
Intent notificationIntent = new Intent(My_BLEService.this, MonitoringActivity.class);
// FIXME: Necessary? - notificationIntent.putExtra(My_AppManager.RFSTAR, bleDevice);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.logo_md);
// TODO: Add close button
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("BLE Monitor")
.setTicker("BLE Monitor")
.setContentText("BLE")
// TODO; This isn't appearing correctly the first time
.setSmallIcon(R.drawable.logo_md)
//.setLargeIcon(
// Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
// Note: this doesn't seem to do anything (setting it to false doesn't change it either)
.setOngoing(true).build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
} else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
if(this.receiverRegistered) {
unregisterReceiver(this.gattUpdateReceiver);
this.receiverRegistered = false;
}
if(this.isConnected) {
this.disconnect();
}
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
Alright, I created the problem myself. The problem was, in my service I was resetting the member variable my_device in the onBind method:
public IBinder onBind(Intent intent) {
this.my_device = intent.getParcelableExtra(Kidpod_AppManager.RFSTAR);
return this.kBinder;
}
with an extra which was not set when the bindService method was called in the activity, so it was setting it to null.

my Android service won't keep running

I have a service that performs an AsyncTask which calls itself after each completion. As you'll see below, I am starting my service in the foreground. It starts successfully and keeps running as intended while I have it plugged into my computer and spitting output to LogCat. I know this because to test, I have my AsyncTask loop spitting out a notification every 5 minutes. However, when I unplug it from my computer, the notifications don't come! It's as if the service just completely stops after I start it!
NOTE: My service is a regular service, not an IntentService.
Here is my onStartCommand...
#SuppressWarnings("deprecation")
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
getData(intent);
self = this;
// Enter foreground state
String title = "Service started.";
String subject = "Service is running.";
String body = "Monitoring...";
Notification notification = new Notification(R.drawable.ic_launcher, title,
System.currentTimeMillis());
if(notificationSounds)
notification.defaults |= Notification.DEFAULT_SOUND;
else
notification.sound = null;
Intent notificationIntent = new Intent(this, MainActivity3.class);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, subject, body, pendIntent);
startForeground(1500, notification);
new BatteryLifeTask(appContext).execute();
return START_NOT_STICKY;
}
Here is my AsyncTask:
// Looping AsyncTask for continuous mode
private class BatteryLifeTask extends AsyncTask<Void, Void, Void> {
// Member variables
Context appContext;
int batteryPct0;
public BatteryLifeTask(Context context) {
super();
appContext = context;
}
protected Void doInBackground(Void... params) {
System.out.println("Entering doInBackground");
// Get the initial battery level
batteryPct0 = getBatteryPercent();
System.out.println("Initial battery percent: " + batteryPct0);
// Check time
Calendar c = Calendar.getInstance();
Date dateNow = c.getTime();
// getTime returns ms, need minutes. 60000ms in a minute.
long currTime = dateNow.getTime() / 60000;
if(currTime >= timeToUse){
finished = true;
stopSelf();
}
System.out.println("Leaving doInBackground");
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(!finished) {
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
System.out.println("Entering postExecute. waitTime is " + waitTime);
Runnable r = new Runnable() {
#Override
public void run() {
if(!finished) { // In case postDelayed is pending, avoids extra notification
System.out.println("An interval has passed.");
calculateHelper(batteryPct0);
new BatteryLifeTask(appContext).execute();
}
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
}
}
}
And here is my code for creating notifications:
// Method for creating a notification
#SuppressWarnings("deprecation")
void notify0(int id, String title, String subject, String body, boolean playSound){
NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(android.R.drawable.
PendingIntent pending = PendingIntent.getActivity(
getApplicationContext(), 0, new Intent(), 0);
notify.setLatestEventInfo(getApplicationContext(), subject, body, pending);
if(playSound)
notify.defaults |= Notification.DEFAULT_SOUND;
else
notify.sound = null;
// Cancel running notification if exists
NM.cancel(id);
// Push notification
NM.notify(id, notify);
}
Can anyone help me? This is driving me insane! My app works PERFECTLY when plugged in and hooked up to USB debugging. But when unplugged, the service seems to completely halt and do nothing.
This is because you are returning START_NOT_STICKY on the Service's onStartCommand().
START_NOT_STICKY if this
service's process is killed while it is started (after returning from
onStartCommand(Intent, int, int)), and there are no new start intents
to deliver to it, then take the service out of the started state and
don't recreate until a future explicit call to
Context.startService(Intent).
You should return START_STICKY instead
START_STICKY If this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then
leave it in the started state but don't retain this delivered intent.
Later the system will try to re-create the service.
Check this service api changes especially the section of Service lifecycle changes.
Return START_STICKY instead of START_NOT_STICKY and review your design. In your case it's better to use the AlarmManager with a 5 minute timeout and then start an IntentService.

How to implement multiple timed notifications in Android

I have this final feature for the app that I am creating. The app that I have made is a calendar that saves events and notifies the user when the time arrives. The problem that I encounter is that when I create multiple notifications (or multiple events), it only notifies the very latest that was created. I tried to make different IDs for the notifications but to no success. Here the codes that I modified. It was from a tutorial that I've seen.
AlarmTask.java
public class AlarmTask implements Runnable{
// The date selected for the alarm
private final Calendar date;
// The android system alarm manager
private final AlarmManager am;
// Your context to retrieve the alarm manager from
private final Context context;
private final long alarmID;
public AlarmTask(Context context, Calendar date, long id) {
this.context = context;
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
this.date = date;
this.alarmID = id;
}
#Override
public void run() {
// Request to start are service when the alarm date is upon us
// We don't start an activity as we just want to pop up a notification into the system bar not a full activity
Intent intent = new Intent(context, NotifyService.class);
intent.putExtra(NotifyService.INTENT_NOTIFY, true);
intent.putExtra("alarmID", alarmID);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
// Sets an alarm - note this alarm will be lost if the phone is turned off and on again
am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
}
}
NotifyService.java
public class NotifyService extends Service {
/**
* Class for clients to access
*/
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
// Unique id to identify the notification.
private static final int NOTIFICATION = 143;
// Name of an intent extra we can use to identify if this service was started to create a notification
public static final String INTENT_NOTIFY = "com.gpplsmje.mac.calendar.utils.INTENT_NOTIFY";
// The system notification manager
private NotificationManager mNM;
#Override
public void onCreate() {
Log.i("NotifyService", "onCreate()");
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// If this service was started by out AlarmTask intent then we want to show our notification
if(intent.getBooleanExtra(INTENT_NOTIFY, false)){
showNotification(intent.getLongExtra("alarmID", 0));
}
// We don't care if this service is stopped as we have already delivered our notification
return START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients
private final IBinder mBinder = new ServiceBinder();
/**
* Creates a notification and shows it in the OS drag-down status bar
*/
#SuppressWarnings("deprecation")
private void showNotification(long alarmID) {
SaveEvent event = new SaveEvent(this);
event.open();
Log.d("Notification: ID", alarmID + "");
// This is the 'title' of the notification
CharSequence title = event.getEventName(alarmID);
// This is the icon to use on the notification
int icon = R.drawable.icon_reminder;
// This is the scrolling text of the notification
CharSequence text = event.getEventDesc(alarmID);
// What time to show on the notification
long time = System.currentTimeMillis();
event.close();
Intent backToEventDetail = new Intent(this, CalendarEventDetail.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, backToEventDetail, 0);
Notification notify = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(icon)
.setContentIntent(contentIntent).getNotification();
notify.defaults = Notification.DEFAULT_SOUND;
notify.flags = Notification.FLAG_AUTO_CANCEL;
// Send the notification to the system.
mNM.notify(Integer.parseInt(String.valueOf(alarmID)), notify);
// Stop the service when we are finished
stopSelf();
}
}
From what I understand with the code, the AlarmTask.java receives the alarm date and sets the it to notify on that date. The ID that I passed is the ID of the event that I saved in the phone's database. But I couldn't get it to add multiple notifications. It only receives the latest that I saved. I would want it to get all the events and set notification for each of those events. Can somebody help me with it?
Create pending intent like this
PendingIntent contentIntent = PendingIntent.getActivity(this, (int)(Math.random() * 100), backToEventDetail, PendingIntent.FLAG_UPDATE_CURRENT);
Create Pending intent with below code
PendingIntent contentIntent = PendingIntent.getActivity(this, Integer.parseInt(String.valueOf(alarmID)), backToEventDetail, Intent.FLAG_ACTIVITY_NEW_TASK );

Displaying Notification after Background Process done Android

I am loading an HTTP request in the background using loopj HTTP CLIENT and when it is done, I want to display a "success" notification (dialog, toast, etc.)
I have the code in a seperate (non-activity) class with a static method that executes the background request. In the end, the response is in a AsyncHttpResponseHandler under a onSuccess method. In this method, I can print out the response, confirm that the request went through, save data to the sd card/ Shared Preferences, but how do I access the UI thread to display a notification?
Thanks in advance.
you can do it using a Handler, or by calling Activity.runOnUiThread(). so you either pass a Handler, or an Activity object to your static method, then in your onSuccess() method, do,
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// i'm on the UI thread!
}
}
);
or,
handler.post(new Runnable() {
#Override
public void run() {
// i'm on the UI thread!
}
}
);
I guess you mean a service as a background process. Service has many built in methods like onCreate, onStartCommand, onDestroy, etc. I suggest using a Notification, because notifications do not require a UI thread to do the job.
Create a method to generate a notification and call it after your HTML read is over.
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
You could fire a local broadcast with the message, and show a toast with a receiver.
Do this in the class doing the updates:
Intent intent = new Intent("ACTION_TOAST");
intent.putExtra("message", "Success!");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
Then in any activity that might want to know about the update, do this:
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if ("ACTION_TOAST".equals(intent.getAction()) {
Toast.makeText(MyActivity.this, intent.getStringExtra("message"),
Toast.LENGTH_SHORT).show();
}
}
}
#Override
protected void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver(
receiver, new IntentFilter("ACTION_TOAST"));
}
#Override
protected void onStop() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}
You still need to pass a context into your static method, but this works even if that context is a Service or some other context that can't show Toasts / create UI.

Categories