I am a newbie in android developer. I tried to make an application that allow user make reminding. The users can add time and their reminding, these data will be saved in sqlLite. And I have some problems when I used Alarmmanage to setup time to display notification for user. This problem is the notification won't display when I closed app or I still in MainActivity. Here is my code:
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
viewPager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
timeFormatter = new SimpleDateFormat("HH:mm", Locale.US);
timeHourFormatter = new SimpleDateFormat("HH", Locale.US);
dbHelper = new DatabaseHelper(MainActivity.this);
currentDate = Calendar.getInstance();
aminutes = Integer.parseInt(timeFormatter.format(currentDate.getTime()).substring(3, 5));
//second = timeFormatter.format(currentDate.getTime()).substring(6, 8);
//showMessages(timeFormatter.format(currentDate.getTime()) + "second: "+ second);
if(aminutes > 10){
timeMinutes = timeHourFormatter.format(currentDate.getTime()) + ":"+aminutes;
}
else{
timeMinutes = timeHourFormatter.format(currentDate.getTime()) + ":0"+aminutes;
}
//showMessages(dateFormatter.format(currentDate.getTime())+ " " + timeMinutes);
arrReminder = dbHelper.getRemindForAlarm(dateFormatter.format(currentDate.getTime()), timeMinutes);
if(arrReminder.size() >0){
hours = Integer.parseInt(arrReminder.get(0).get_Time().substring(0, 2));
minutes = Integer.parseInt(arrReminder.get(0).get_Time().substring(4,5));
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, NOW_NOTIFICATION, intent, PendingIntent.FLAG_ONE_SHOT);
cancelNotification(NOW_NOTIFICATION);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 2 * 1000, pendingIntent);
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// start();
// showMessages(year + "-" + month + "-"+day + " " + hours + ":" + minutes);
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivity(intent);
// alarmMamager();
}
});
}
public void cancelNotification(int requestCode) {
try {
Intent notificationIntent = new Intent(getApplicationContext(), MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
MyReceiver
public void onReceive(Context context, Intent intent) {
long[] v = {500, 1000};
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setVibrate(v).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).setSmallIcon(R.drawable.ic_remind).setContentTitle("My notification").setContentText("This is a test message!");
mBuilder.setAutoCancel(true);
Intent intent1 = new Intent(context, MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingNotificationIntent);
mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mManager.notify(0, mBuilder.build());
}
When I tried putting default hours and minutes, the problem won't appear. I don'nt know why. Anyone can please help me fix this problem. Thanks you.
Aucun commentaire:
Enregistrer un commentaire