在预定义时间发送通知不起作用

时间:2016-01-06 17:22:01

标签: android calendar notifications broadcastreceiver alarmmanager

我在这里读了几个问题,也有类似的问题。不幸的是,没有其他问题的答案设法解决问题。我知道如何在按钮点击上创建通知,但我无法在特定时间创建通知。我在以下代码段中的目标是每天19:00创建通知。以下是代码段:

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Calendar calendar = Calendar.getInstance();
    setAlarm(calendar);
}

public void setAlarm(Calendar calendar) {

    Intent alertIntent = new Intent(this, Receiver.class);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, alertIntent, 0);

    calendar.set(Calendar.HOUR_OF_DAY, 19);
    calendar.set(Calendar.MINUTE, 00);

    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                   24*60*60*1000, pendingIntent);
}

接收器:

public class Receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
    createNotification(context, "Blabla", "Blablablabla", "Alert");

    }

    public void createNotification(Context context, String msg,
                                  String msgText, String msgAlert) {

    PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.clear_dayicon)
            .setContentTitle(msg)
            .setContentText("msgText")
            .setTicker(msgAlert);

    mBuilder.setContentIntent(pi);
    mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
    }
}

清单:

 // only ones I called after started working with notifications and stuff

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".Receiver2" android:enabled="true">
    <intent-filter> <action android:name="NOTIFICATION_SERVICE" /></intent-filter>
</service>
<receiver android:name=".Receiver"/>

一些帮助将非常感激。由于我是初学者,我需要有人向我展示我的错误。否则,无论我读了多少文档,我都无法发现它们。这段代码对我来说很好看:P

1 个答案:

答案 0 :(得分:-1)

试试这个:

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   Calendar calendar = Calendar.getInstance();
   calendar.set(Calendar.HOUR_OF_DAY, 19);
   calendar.set(Calendar.MINUTE, 00);

   setAlarm(calendar);
}
public void setAlarm(Calendar calendar) {
  boolean alarmNoActive = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);

    if(alarmNoActive){
        Intent itAlarm = new Intent("ALARM");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,itAlarm,0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);

        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                24*60*60*1000, pendingIntent);
    }
}

在清单中放了这个权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"

在申请中:

  <receiver
        android:name="path of the BroadcastReceiver.Receiver"
        android:label="BroadcastReceiverAux">
        <intent-filter>
            <action android:name="ALARM" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>