对方法使用意图

Using intents for methods

编辑:更新代码:

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);
    }

我的接收者class:

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);
    }
}

最后,displaynotificationactivity 方法:

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 方法。我通过按下按钮启动通知方法,将来会更改它。

像这样创建一个 BroadcastReceiver class:

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().

等方法

应该有效 ;-)