I have an activity which just displays a daily counter. When the day ends, i want to send a notification with last day counter, insert it in a db and if the app is running update the counters label to zero.
I think i must register a static <receiver>android.intent.action.DATE_CHANGED</receiver>
. so as to get notified even if the activity is not running.
There I insert the value in DB and i send the notification.
And a dynamic created broadcast receiver which i will unregister onPause, which receives the same events, but it will only update the UI and specifically the label.
Is this the best solution?
Is there any way from a broadcastReceiver
to make my Activity
(if is running) to go to resume again ?
Is it possible from broadcastReceiver
to call a Service
, which will update my UI if my Activity is running?
Use broadcastreceiver that would catch the event
public class EventReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
NotificationService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
broadcastreceiver give event service
public class NotificationService extends IntentService {
private final ServiceBinder binder = new ServiceBinder();
private ServiceListener serviceListener;
public NotificationService() {
super("NotificationService");
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
serviceListener = null;
return true;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
//send notification
if(serviceListener != null){
serviceListener.updateActivity();
}
}
public void setServiceListener(ServiceListener serviceListener) {
this.serviceListener = serviceListener;
}
public class ServiceBinder extends Binder {
public NotificationService getSevice(){
return NotificationService.this;
}
}
public static interface ServiceListener{
public void updateActivity();
}
}
then you need to connect to the service in your activity
public class MainActivity extends Activity implements NotificationService.ServiceListener {
private boolean isBound;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
NotificationService.ServiceBinder binder = ((NotificationService.ServiceBinder)service);
binder.getSevice().setServiceListener(MainActivity.this);
isBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
if(!isBound) {
Intent intent = new Intent(this, NotificationService.class);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
}
@Override
protected void onPause() {
super.onPause();
if(isBound){
unbindService(serviceConnection);
isBound = false;
}
}
@Override
public void updateActivity() {
runOnUiThread(new Runnable() {
@Override
public void run() {
//update you list here
}
});
}
}
and do not forget to mention the service and broadcastreceiver in the manifest file and specify the intent filter
<receiver android:name="EventReceiver">
<intent-filter>
<action android:name="........." />
</intent-filter>
</receiver>
<service android:name="NotificationService"/>
You can simply use a LocalBroadcastReceiver to send events to your Activity , a working example could be found here
After more reading i found that the best solution is the above.
A static broadcast receiver for DATE_CHANGED actions. This will start a Service which will update the db and send a local custom broadcast (com.myBroadcast.WakeUpActivity)
The Activity if is running will register a receiver for the previous custom broadcast (will unregister onPause), and will update the UI.