Android Set Alarm and perform action at scheduled time | Schedule Android Notifications


Let us assume you want to set up alarms at scheduled time which should trigger notifications. Here is how you can achieve it.

You can trigger an alarm at a scheduled time and then use an IntentService to capture it once alarm goes off.

In the main activity (lets assume the name is MainActivity, you can create an alarm like this.
    AlarmManager mgr = (AlarmManager) MainActivity.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(MainActivity, NotifService.class);
    PendingIntent pi = PendingIntent.getService(MainActivity, 0, intent, 0);
    mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + PERIOD, pi);
    //where the PERIOD defines after how much milliseconds you want the alarm to go off.

To configure the AlarmManager differently, you can read more about it.

NotifService is what captures the alarm when it goes off. Create a class called NotifService it extends IntentService

public class NotifService extends IntentService {

    public NotifService() {
      super("My service");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
         //write your code for sending Notification
         Intent intent = new Intent(); 
         intent.setAction("com.xxx.yyy.youractivitytobecalled");
         PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder (getApplicationContext());
        builder.setContentTitle("Your Application Title");
        builder.setContentText("Notification Content");
        builder.setContentIntent(pendingIntent);
        builder.setTicker("Your content");
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setOngoing(false);
        builder.setPriority(0);

        Notification notification = builder.build();
        NotificationManager notificationManger = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
        notificationManger.notify(1, notification);

    }
    }

Register NotifService in your AndroidManifest.xml
 
    <application
             fill content here />

    <service android:name=".NotifService" >
    </service>

    </application>

Android Menu Options | Action Bar Menu Options

The menu options in Android which appear as 3 vertical dots on the action bar or any custom bar is a very utility way of providing users with selection choices without being intrusive. Here is a video on all you wanted to know about managing Menu options in Android.



The source code is as follows.

Inside the /res/menu folder, create a file start_menu.xml




<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
        android:id="@+id/action_login"
        android:orderInCategory="1"
        android:showAsAction="never"
        android:title="@string/login"/>
<item
        android:id="@+id/action_orderhistory"
        android:orderInCategory="2"
        android:showAsAction="never"
        android:title="@string/orderhistory"/>

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="3"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    
</menu>

In your activity you can put this code to inflate the menu



public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start_screen, menu);
        return true;
    }