Android Open Source - Android-Universal-Notifier Notification Service






From Project

Back to project page Android-Universal-Notifier.

License

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.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
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 = "";
  }
  
  @Override
  // 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();
    intentCT.setAction(Const.ACTION_SHOW_CURRENT_TASKS);
    sendBroadcast(intentCT);
    
    return super.onStartCommand(intent, flags, startId);
    }
  
  @SuppressWarnings("deprecation")
  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);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);        
        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
    startAlarmManager();
    
    m_updateLog = new UpdateLogBroadcastReceiver();
    IntentFilter intentFilter_log = new IntentFilter(Const.ACTION_ADD_TO_LOG);
    getApplicationContext().registerReceiver(m_updateLog, intentFilter_log);
    
      super.onCreate();
    }
  
   public void onDestroy() {
    super.onDestroy();
    getApplicationContext().unregisterReceiver(m_updateLog);
   }
   
   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
        TasksQueue.getInstance(this);
   }

  @Override
  public IBinder onBind(Intent arg0) {  
    return null;
  }
  
  public class UpdateLogBroadcastReceiver extends BroadcastReceiver {
    @Override
    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

com.mairos.universalnotifier.UI.LogFragmentTab.java
com.mairos.universalnotifier.UI.MainActivity.java
com.mairos.universalnotifier.UI.SettingsActivity.java
com.mairos.universalnotifier.UI.TabListener.java
com.mairos.universalnotifier.UI.TaskInfoActivity.java
com.mairos.universalnotifier.UI.TasksFragmentTab.java
com.mairos.universalnotifier.model.AlarmManagerBroadcastReceiver.java
com.mairos.universalnotifier.model.AttachmentData.java
com.mairos.universalnotifier.model.Const.java
com.mairos.universalnotifier.model.JSInterpreter.java
com.mairos.universalnotifier.model.LauncherBroadcastReceiver.java
com.mairos.universalnotifier.model.Logger.java
com.mairos.universalnotifier.model.NotificationService.java
com.mairos.universalnotifier.model.NotificationTask.java
com.mairos.universalnotifier.model.TasksQueue.java
com.mairos.universalnotifier.model.TasksXMLLoader.java
com.mairos.universalnotifier.network.GMailSender.java
com.mairos.universalnotifier.network.JSSEProvider.java
com.mairos.universalnotifier.network.NetworkOperations.java
com.mairos.universalnotifier.network.SMSSender.java