在日期选择器的日期设置android通知

时间:2015-12-03 17:15:05

标签: android notifications

我有一个带日期选择器的机器人。 我想选择一个日期并用动作栏项目保存。 此时,应设置选定日期的通知。

这是我第一次收到通知。 谁能告诉我,我怎么意识到这一点? 我用min。 SDK 15

我尝试这样的事情,但通过此直接通知开始:

intent = new Intent(this, Overview.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    long[] pattern = {200,200,200,200,200,200,200,200,200};
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    Notification n  = new Notification.Builder(this)
            .setContentTitle("My Titel")
            .setContentText("This is the Body")
            .setSmallIcon(R.drawable.ic_notification_appicon)
            .setContentIntent(pIntent)
            .setAutoCancel(true)
            .setLights(Color.GREEN, 500, 500)
            .setVibrate(pattern)
            .setSound(alarmSound)
            .build();


    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, n);

1 个答案:

答案 0 :(得分:0)

如果您想设置通知,它应该在n日通知您需要使用广播接收器,并且因为广播接收器只在短时间内启动它更好的做法也使用意图服务在这里您有一个示例如何做到这一点。

这是广播接收器类。

 public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {


        Intent intent1 = new Intent(context, MyNewIntentService.class);

        context.startService(intent1);

}

}

并在清单中注册。

 <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="false" >
    </receiver>

这是意图服务类。

public class MyNewIntentService extends IntentService {
private static final int NOTIFICATION_ID = 3;

public MyNewIntentService() {
    super("MyNewIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
   Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("My Titel");
        builder.setContentText("This is the Body");
        builder.setSmallIcon(R.drawable.ic_notification_appicon);
        builder.setAutoCancel(true);
        builder.setLights(Color.GREEN, 500, 500);

    Intent notifyIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    Notification notificationCompat = builder.build();
    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
    managerCompat.notify(NOTIFICATION_ID, notificationCompat);

}

}  并在清单中注册。

 <service
        android:name=".MyNewIntentService"
        android:exported="false" >
    </service>

然后在您的活动中将闹钟管理器设置为在特定日期启动广播接收器,此示例使用日期选择器来帮助您:

   DatePicker datePicker= (DatePicker)timeDialogView.findViewById(R.id.yourid);

   Calendar now = Calendar.getInstance();
            Calendar current = Calendar.getInstance();

now.set(now.get( datePicker.getYear()), now.get(datePicker.getMonth()), now.get(datePicker.getDayOfMonth()), the hour, and the minute);
            if(now.compareTo(current)<=0){
                Toast.makeText(MainActivity.this,"invalid time",Toast.LENGTH_LONG).show();
            }
            else{
 Intent notifyIntent = new Intent(context, MyReceiver.class);
        notifyIntent.putExtra("title",item.getText());
        PendingIntent pendingIntent = PendingIntent.getBroadcast
                (context, NOTIFICATION_REMINDER, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC,now.getTimeInMillis(), pendingIntent);

}

我希望这会对你有帮助!