我想在用户从时间选择器中选择时间时设置通知。为此,我已经搜索了,我知道应该使用日历来设置时间和警报管理器来设置警报。
我做了相应的事。但是通知不会在选定的时间内提高。
设置时间选择器的updateTime方法。
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);
}
}
我的活动中有3个时间选择器,所以我做了相应的工作。 我调用了onCreate方法的日历实例。
setAlarm方法
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();
}
通知接收者类:
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);
}
}
清单
<?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>
我在这一切中缺少什么?
任何人都可以帮忙吗?
答案 0 :(得分:0)
您可以在活动中使用SharedPreferences来保存所选时间
SharedPrefereces pref = getSharedPreferences("com.example.siddhi.timetablelayout", MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
edit.putInt("startTime", startTime);
edit.commit();
然后在您的服务上加载您的SharedPreferences
SharedPrefereces pref = getSharedPreferences("com.example.siddhi.timetablelayout", MODE_PRIVATE);
在onReceive方法上,检查SharedPreferences节省的时间是否等于当前时间:
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();
}