编辑:更新的代码:
notificationsetter方法:
public void notificationsetter(){
Intent myIntent = new Intent(this, MyReceiver.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
Log.v("log","notificationsetter");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 55);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
}
Myreceiver类:
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyActivity";
@Override
public void onReceive(Context context, Intent intent) {
String i = "Log recieved";
Log.v(TAG, "index=" + i);
Frontpage.displayNotificationActivity(context);
}
}
最后,显示通知活动方法:
public static void displayNotificationActivity(Context context){
int notificationIdOne = 111;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("Leibniz Vertretungsplan");
mBuilder.setContentText("Schau dir den Vertretungsplan an!");
mBuilder.setTicker("Leibniz Vertretungsplan");
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(context);
String text = pref.getString("notifications_new_message_ringtone","");
mBuilder.setSound(Uri.parse(text));
mBuilder.setAutoCancel(true);
SharedPreferences pref2= PreferenceManager.getDefaultSharedPreferences(context);
boolean vibrate = pref2.getBoolean("notifications_new_message_vibrate",false);
if(vibrate){
mBuilder.setVibrate(new long[] {1000, 1000});
}
mBuilder.setLights(0xffff0000,5000,5000);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
//mBuilder.setNumber(++numMessagesOne);
Intent resultIntent = new Intent(context, Vertretungsplanheuteactvity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(Frontpage.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_ONE_SHOT //can only be used once
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
myNotificationManager.notify(notificationIdOne, mBuilder.build());
}
问题:我在日历中设置时没有显示通知。 Logcat说使用了notificationsetter,但是onReceive方法并没有。我按下按钮启动notifcation方法,将来会改变它。
答案 0 :(得分:1)
像这样创建一个BroadcastReceiver
类:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MainActivity.displayNotification(context);
}
}
将此添加到您的清单:
<receiver android:name=".MyReceiver"/>
为您的闹钟创建PendingIntent
,如下所示:
Intent myIntent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
更改您的displayNotification()
方法,使其获得Context
参数并将其设为static
(以便可以从MyReceiver
调用)。在displayNotification()
中,将this
的所有用途替换为context
。您还需要使用context
引用来调用getApplicationContext()
和getSystemService()
等方法。
应该工作; - )