I want to set notification when user selects time from time picker. For this as I have searched I got to know should use calendar to set the time and alarm manager to set the alarm.
I have done accordingly. But the Notification dose not raise on the selected time.
Setting time on time picker's updateTime method.
private void updateTime(int hour, int minute) {
df = new SimpleDateFormat("HH:mm a");
if (timeType == 0) {
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.DATE, day);
datefrom = c.getTime();
startTime = String.valueOf(datefrom);
aTime = df.format(datefrom.getTime());
showFromTime.setText(aTime);
Toast.makeText(getApplicationContext(), String.valueOf(datefrom), Toast.LENGTH_LONG).show();
} else if (timeType == 1) {
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.DATE, day);
dateto = c.getTime();
endTime = String.valueOf(dateto);
bTime = df.format(dateto.getTime());
showToTime.setText(bTime);
Toast.makeText(getApplicationContext(), String.valueOf(dateto), Toast.LENGTH_LONG).show();
}
else if (timeType == 2)
{
// c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
c.set(Calendar.DATE, day);
notification = c.getTime();
notificationTime = df.format(notification);
Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();
setAlarm(c);
}
}
I have 3 time pickers in my activity so I have done accordingly. Calendar instance I have called onCreate method.
setAlarm method
private void setAlarm(Calendar targetmCalen) {
Intent intent = new Intent(getBaseContext(),AddEventActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetmCalen.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
Toast.makeText(getBaseContext(),
"call alarmManager.setExact()",
Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(),"Notification Set",Toast.LENGTH_SHORT).show();
}
Notification Receiver class:
public class NotificationReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID = 0;
NotificationManager notificationManager;
Notification myNotification;
EventTableHelper db;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();
Calendar c =Calendar.getInstance();
Intent myIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Event Time")
.setContentText("Time")
.setWhen(c.getTimeInMillis())
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_alarm_black_48dp)
.build();
Log.i("Notify","Notification");
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.siddhi.timetablelayout" >
<permission
android:name="com.example.siddhi.go_jek.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddEventActivity" >
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<receiver android:name=".NotificationReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
What am I missing in all this?
Can anyone help with this?
You can use SharedPreferences on your activity to save the selected time
SharedPrefereces pref = getSharedPreferences("com.example.siddhi.timetablelayout", MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
edit.putInt("startTime", startTime);
edit.commit();
Then load your SharedPreferences on your Service
SharedPrefereces pref = getSharedPreferences("com.example.siddhi.timetablelayout", MODE_PRIVATE);
On your onReceive method, check if SharedPreferences saved time equals current time:
String startTime=pref.getString("startTime");
Calendar currentDate = c.getInstance();
String currentTime = String.valueOf(c.getTime());
if(currentTime.equalsIgnoreCase(startTime))
{
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Event Time")
.setContentText("Time")
.setWhen(c.getTimeInMillis())
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_alarm_black_48dp)
.build();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.