Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First Create a button for fire Alarm Manager-</p> <p>@Override</p> <pre><code>public void onClick(View v) { // TODO Auto-generated method stub if(v==btnStartAlarm){ fireAlarm(); } </code></pre> <p>Here is the my Alarm Manager-</p> <pre><code>public void fireAlarm() { /** * call broadcost reciver */ Intent intent = new Intent(context, AlarmReceiver.class); intent.setAction("com.manish.alarm.ACTION"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 111, intent,0); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); AlarmManager alarm = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE); alarm.cancel(pendingIntent); alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),100000, pendingIntent); } </code></pre> <p>And now create a BroadcastReceiver class and fire Notification Method-</p> <pre><code>public class AlarmReceiver extends BroadcastReceiver { private final String SOMEACTION = "com.manish.alarm.ACTION"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (SOMEACTION.equals(action)) { //do what you want here generateNotification(context,"Hi how are you?"); } } @SuppressWarnings("deprecation") private void generateNotification(Context context, String message) { System.out.println(message+"++++++++++2"); int icon = R.drawable.ic_launcher; long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, message, when); String title = context.getString(R.string.app_name); String subTitle = context.getString(R.string.app_name); Intent notificationIntent = new Intent(context, OutPut.class); notificationIntent.putExtra("content", message); PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); notification.setLatestEventInfo(context, title, subTitle, intent); //To play the default sound with your notification: notification.defaults |= Notification.DEFAULT_SOUND; notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults |= Notification.DEFAULT_VIBRATE; notificationManager.notify(0, notification); } } </code></pre> <p>And Create A class for display notification message-</p> <pre><code>public class OutPut extends Activity { TextView textmessage; String stringValue; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_output); textmessage = (TextView) findViewById(R.id.textView1); Intent intent = getIntent(); stringValue = intent.getStringExtra("content"); textmessage.setText(stringValue); System.out.println(stringValue); } } </code></pre> <p>And inside your manifest.xml just put permission for vibration-</p> <pre><code>&lt;uses-permission android:name="android.permission.VIBRATE" /&gt; </code></pre> <p>Here Download link from github- <a href="http://www.androidhub4you.com/2013/12/android-alarm-manager-example-how-to.html" rel="nofollow">http://www.androidhub4you.com/2013/12/android-alarm-manager-example-how-to.html</a></p> <p><a href="https://github.com/manishsri01/AndroidAlarmManagerDemo" rel="nofollow">https://github.com/manishsri01/AndroidAlarmManagerDemo</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload