Android Open Source - Android-Universal-Notifier Notification Service

From Project

Back to project page Android-Universal-Notifier.


The source code is released under:

Apache License

If you think the Android project Android-Universal-Notifier listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mairos.universalnotifier.model;
// w  w w  .j  av a 2  s  .c  o  m
import com.mairos.universalnotifier.R;
import com.mairos.universalnotifier.UI.MainActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.ResultReceiver;

// starts AlarmService for regularly update TasksQueue (via AlarmManagerBroadcastReceiver), 
//     init TasksQueue and keeps logs
public class NotificationService extends Service {
  private static final int UPDATE_INTERVAL = 60; // for regular update TasksQueue from web - in seconds
  private static final int NOTIFICATION_ID = 1;
  public static final String SERVICE_RESPONSE = "response_from_service";
  public static final int UPDATE_LOG = 0;
  // callback to GUI
  private ResultReceiver m_receiver = null;
  // the way to get info about log updates
  private UpdateLogBroadcastReceiver m_updateLog;
  // TODO - one day, it'll be ContentProvider here
  private static String m_currentLogStr = "";
  public static String getCurrentLog(){
    return m_currentLogStr;
  public static void clearLog(){
    m_currentLogStr = "";
  // on every MainActivity re-creation
  public int onStartCommand(Intent intent, int flags, int startId) {
    // save GUI callback
    m_receiver = intent.getParcelableExtra(SERVICE_RESPONSE);
    // update Tasks list in GUI
    Intent intentCT = new Intent();
    return super.onStartCommand(intent, flags, startId);
  public void onCreate() {
    // high priority for Service in OS
    Notification notification = new Notification(R.drawable.ic_launcher, "Notifier started", System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);  
        notification.setLatestEventInfo(this, "Notifier", "Notification service in progress", contentIntent);
    startForeground(NOTIFICATION_ID, notification);
    // start regular updating the tasks list - in AlarmManagerBroadcastReceiver.onReceive
    m_updateLog = new UpdateLogBroadcastReceiver();
    IntentFilter intentFilter_log = new IntentFilter(Const.ACTION_ADD_TO_LOG);
    getApplicationContext().registerReceiver(m_updateLog, intentFilter_log);
   public void onDestroy() {
   private void startAlarmManager(){
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class);
        intent.putExtra(AlarmManagerBroadcastReceiver.ALARM_MODE, AlarmManagerBroadcastReceiver.ALARM_MODE_UPDATE_TASKS);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * UPDATE_INTERVAL , pi);
        // init for first update

  public IBinder onBind(Intent arg0) {  
    return null;
  public class UpdateLogBroadcastReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
      if (intent.getStringExtra(Const.TF_MESSAGE) != null){
        m_currentLogStr += intent.getStringExtra(Const.TF_MESSAGE) + "\n";
        if (m_receiver != null) m_receiver.send(UPDATE_LOG, null);

Java Source Code List