使用任务计划程序在特定日期和时间显示通知

问题描述:

我是android新手。我想在特定的日期和时间(例如2017年9月9日上午10:00时)显示通知。我正在使用闹铃管理器,但无法正常工作。使用任务计划程序在特定日期和时间显示通知

请告诉我正确的方法。 MainActivity

公共类MainActivity延伸活动{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.YEAR, 2017); 
    calendar.set(Calendar.MONTH, 9); 
    calendar.set(Calendar.DAY_OF_MONTH, 9); 
    calendar.set(Calendar.HOUR_OF_DAY, 10); 
    calendar.set(Calendar.MINUTE, 0); 
    calendar.set(Calendar.SECOND, 0); 

    Intent inn = new Intent(MainActivity.this,AlarmActivity.class); 
    PendingIntent displayIntent = PendingIntent.getActivity(
      getBaseContext(), 0,inn, 0); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, 
      calendar.getTimeInMillis(), displayIntent); 

} 

AlarmActivity

公共类AlarmActivity延伸活动 {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_alarm); 

    final Intent notificationIntent = new Intent(this, AlarmActivity.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent contentIntent = PendingIntent.getActivity(AlarmActivity.this, 0, notificationIntent, 0); 

    Notification notification1 = new Notification.Builder(AlarmActivity.this) 
      .setContentTitle("Notification") 
      .setContentText("Get Notigication") 
      .setContentIntent(contentIntent).setWhen(System.currentTimeMillis()) 
      .setOngoing(true) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .build(); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    notificationManager.notify(1, notification1); 
} 

舱单 权限

+0

你有没有尝试过的东西?使用您的代码更新该问题 – Yannick

+0

它不适用于所有手机,因为您从应用程序托盘中删除应用程序会将应用程序杀死该应用程序 – phpdroid

为了触发警报,您​​必须使用WakefulBroadcastReceiver,您将触发将在您的活动中生成通知的服务。

public class AlarmReceiver extends WakefulBroadcastReceiver { 

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

    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri); 
    ringtone.play(); 
    ComponentName comp = new ComponentName(context.getPackageName(), 
      AlarmService.class.getName()); 
    startWakefulService(context, (intent.setComponent(comp))); 
    setResultCode(Activity.RESULT_OK); 
} 

}

然后,您将创建一个这样的服务。

public class AlarmService extends IntentService { 


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


@Override 
public void onHandleIntent(Intent intent) { 
    contentId = intent.getStringExtra("id"); 
    String title = intent.getStringExtra("title"); 
    String subtitle = intent.getStringExtra("subtitle"); 
    sendNotification(title, subtitle); 
} 
    public void sendNotification(String title, String subtitle) { 


    Notification notification = new Notification.Builder(this) 
      .setSmallIcon(R.drawable.notification_icon) 
      .build(); 

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.reminder_notification_view); 
    contentView.setInt(R.id.layout, "setBackgroundColor", 
      Color.WHITE); 
    contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher); 
    contentView.setTextViewText(R.id.title, title); 
    contentView.setTextViewText(R.id.text, subtitle); 
    notification.contentView = contentView; 

    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

    notification.contentIntent = PendingIntent.getActivity(this, someRandomId, 
      intent, 0); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; //Do not clear the notification 
    notification.defaults |= Notification.DEFAULT_LIGHTS; // LED 
    notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration 
    notification.defaults |= Notification.DEFAULT_SOUND; // Sound 

    mNotificationManager.notify(someRandomId, notification); 

} 

}

+0

什么是DLConstants? – SACHIN

+0

您可以忽略该包。我编辑了答案。 –