0

I create an android application to view notification at a particular time when the application is not running background . I use AlarmMabager & BroadcastReceiver for above purpose .

This application can pass a notification at a specific time whether user is not starting the application .

Here are the steps which I use to do ,

MainActivity.java

public class MainActivity extends Activity 
{

    private PendingIntent pendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) 
     {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      Calendar calendar = Calendar.getInstance();

      calendar.set(Calendar.MONTH, 7);
      calendar.set(Calendar.YEAR, 2016);
      calendar.set(Calendar.DAY_OF_MONTH, 7);

      calendar.set(Calendar.HOUR_OF_DAY, 13);
      calendar.set(Calendar.MINUTE, 40);
      calendar.set(Calendar.SECOND, 0);
      calendar.set(Calendar.AM_PM,Calendar.PM);

      Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
      pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

    } //end onCreate
}

MyReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
       Intent service1 = new Intent(context, MyAlarmService.class);
       context.startService(service1);

    }   
}

MyAlarmService.java

public class MyAlarmService extends Service 
{

   private NotificationManager mManager;

    @Override
    public IBinder onBind(Intent arg0)
    {
       // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() 
    {
       // TODO Auto-generated method stub  
       super.onCreate();
    }

   @SuppressWarnings("static-access")
   @Override
   public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);

       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

       Notification notification = new Notification(R.mipmap.ic_launcher,"This is a test message!", System.currentTimeMillis());
       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

       mManager.notify(0, notification);
    }

    @Override
    public void onDestroy() 
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MyAlarmService"
                 android:enabled="true" />

        <receiver android:name=".MyReceiver"/>

    </application>
</manifest>

After completing all the steps I run the application in my device but it doesn't show any notification . Is there anything wrong ? What should I do to correct this .

Thank you .

Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61
  • 1
    Months are zero-based in `Calendar`. Your alarm is set for next month. – Mike M. Jul 07 '16 at 08:54
  • @MikeM. : - I put 7 for the months . Is it wrong ? – Terance Wijesuriya Jul 07 '16 at 08:55
  • 1
    It is if you want it to fire this month. `Calendar.JULY == 6` and `Calendar.AUGUST == 7`. Also, `HOUR_OF_DAY` is based on the 24-hour clock, so you shouldn't set AM/PM, too. – Mike M. Jul 07 '16 at 08:59
  • @MikeM. :- Thanks for the help . Now It is working . But have a little problem . – Terance Wijesuriya Jul 07 '16 at 09:20
  • How could I use this for device boot completed action ? Anyone can help me . – Terance Wijesuriya Jul 08 '16 at 03:50
  • 1
    That's a different question. Anyway, there are plenty of examples of how to do that here already, like [this one](http://stackoverflow.com/questions/5290141/android-broadcastreceiver-on-startup). – Mike M. Jul 08 '16 at 04:01
  • @MikeM. : Think , I use this concept to fire a notification when device is rebooted . So what should I change in this to achieve it ? – Terance Wijesuriya Jul 08 '16 at 04:13
  • 1
    Since `MyAlarmService` issues the `Notification`, and `MyReceiver` starts `MyAlarmService`, you really just need to add the `RECEIVE_BOOT_COMPLETED` permission to the manifest, and add the `BOOT_COMPLETED` `` to the ``'s ``. – Mike M. Jul 08 '16 at 04:18

0 Answers0