Set up a daily reminder? android code
The below code triggers an alarm at 8 every day in the morging. enjoy coding
How-do-I-generate-a-daily-notification-at-8-a-m-in-my-Android-app
Addition things u can add is add a timer picker and select it from that
1.Create an Activity
and paste the code below
public class MenuActivity extends Activity {
private PendingIntent pendingIntent;
AlarmManager alarmManager;
Intent alarmIntent;
public void onCreate(Bundle savedInstanceState) {
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(MenuActivity.this, MyReceiver.class); // AlarmReceiver1 = broadcast receiver
pendingIntent = PendingIntent.getBroadcast( MenuActivity.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
alarmManager.cancel(pendingIntent);
Calendar alarmStartTime = Calendar.getInstance();
Calendar now = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 8);
alarmStartTime.set(Calendar.MINUTE, 0);
alarmStartTime.set(Calendar.SECOND, 0);
alarmStartTime.set(Calendar.AM_PM,Calendar.AM);
if (now.after(alarmStartTime)) {
Log.d("Hey","Added a day");
alarmStartTime.add(Calendar.DATE, 1);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
setContentView(R.layout.activity_menu);
super.onCreate(savedInstanceState);
}
2. create a broadcast Receiver class
copy and paste the below code
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.e("called","called");
Intent service1 = new Intent(context, NotificationService1.class);
service1.setData((Uri.parse("custom://"+System.currentTimeMillis())));
context.startService(service1);
}
}
3. create a service class for your app
public class NotificationService1 extends IntentService {
private static final String DEBUG_TAG = "SimpleIntentService";
public NotificationService1() {
super(DEBUG_TAG);
// TODO Auto-generated constructor stub
}
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
@Override
protected void onHandleIntent(Intent intent) {
// do What ever want in your application
}
}
No comments:
Post a Comment