2016-03-15 46 views
0

我正在寻找代码,我可以通过一天和一天的时间我不知道,所以用户将确定一天,我必须发送通知,每当这一天来,也如果用户点击重复必须重复它,Android上的特定日期通知

我看到很多代码,只是时间不是天 任何人都可以帮助我修改它们,以便我可以达到我在找什么?

这是我得到的代码。

MainActivity

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

     Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION"); 
     notificationIntent.addCategory("android.intent.category.DEFAULT"); 

     PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     Calendar cal = Calendar.getInstance(); 
     cal.add(Calendar.SECOND, 15); 
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast); 
    } 
} 

NotificationActivity

public class NotificationActivity extends Activity { 

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

AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent notificationIntent = new Intent(context, NotificationActivity.class); 

     TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
     stackBuilder.addParentStack(NotificationActivity.class); 
     stackBuilder.addNextIntent(notificationIntent); 

     PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 

     Notification notification = builder.setContentTitle("Demo App Notification") 
      .setContentText("New Notification From Demo App..") 
      .setTicker("New Message Alert!") 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(pendingIntent).build(); 

     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notification); 
    } 
} 

回答

0

创建Calendar例如像这样根据用户输入:

Calendar calendar = Calendar.getInstance(); 
// Settings calendar for 01/05/2016 12:33:00 AM 
calendar.set(Calendar.YEAR, 2016); 
calendar.set(Calendar.MONTH, 4); // January has value 0 
calendar.set(Calendar.DAY_OF_MONTH, 1); 
calendar.set(Calendar.HOUR_OF_DAY, 12); 
calendar.set(Calendar.MINUTE, 33); 
calendar.set(Calendar.SECOND, 0); 
calendar.set(Calendar.AM_PM, Calendar.AM); 

而要重复闹铃重复使用setInexactRepeating()

由于Android文档中:

预约重复警报具有不精确的触发时间要求;例如, 例如,每小时重复一次警报,但不一定在每小时的最高位置有 。

例如,每天要重复闹铃在指定的时间(日历),

// Milliseconds of 24 hours 
long interval = 24 * 60 * 60 * 1000; 
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
       calendar.getTimeInMillis(), interval , broadcast); 

而且setExact()不会重复报警。

安排一个报警,在规定的时间准确发送。


完整代码与通知和报警管理。

public class MyAlarmReceiver extends BroadcastReceiver 
{ 
    // The app's AlarmManager, which provides access to the system alarm services. 
    private AlarmManager alarmMgr = null; 
    private NotificationManager mNotificationManager; 
    NotificationCompat.Builder builder; 

    public TankAlarmReceiver() 
    { 

    } 


    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     mNotificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent intent = new Intent(context, NotificationActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     // If you use intent extras, remember to call PendingIntent.getActivity() with the flag 
     // PendingIntent.FLAG_UPDATE_CURRENT, otherwise the same extras will be reused for every 
     // notification. 
     PendingIntent contentIntent = PendingIntent.getActivity(context, deviceId.hashCode(), intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(context) 
         .setSmallIcon(getNotificationIcon()) 
         .setContentTitle(title) 
         .setStyle(new NotificationCompat.BigTextStyle() 
           .bigText(msg)) 
         .setContentText(msg); 

     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } 

    /** 
    * Sets a repeating alarm that runs once a day at approximately 8:30 a.m. When the 
    * alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver. 
    * 
    * @param settingsModel 
    */ 
    public void setAlarm(Context context, TankSettingsModel settingsModel) 
    { 
     if(alarmMgr == null) 
      alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

     // Initialize calendar and PendingIntent here 

     long alarmIntervalInMin = 1 * 60 * 60 * 1000; 
     long triggerAtMillis = calendar.getTimeInMillis(); 
     alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
       triggerAtMillis, alarmIntervalInMin, pendingIntent); 

     // Enable {@code SampleBootReceiver} to automatically restart the alarm when the 
     // device is rebooted. 
     ComponentName receiver = new ComponentName(context, MyBootReceiver.class); 
     PackageManager pm = context.getPackageManager(); 

     pm.setComponentEnabledSetting(receiver, 
       PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
       PackageManager.DONT_KILL_APP); 
    } 

    /** 
    * Cancels the alarm. 
    * 
    * @param deviceId 
    */ 

    public void cancelAlarm(Context context, String deviceId) 
    { 
     // Code for cancelling the alarm comes here... 
    } 
} 
+0

thanx很多它完美的工作! – user3604741

+0

@ user3604741很高兴它的工作,不要忘记接受答案,如果它帮助你! :-) –

+0

我想问你其他的东西,我应该什么时候把通知放在我的代码中?我将它与我的应用程序合并,但它给了我错误。任何想法? – user3604741

0

我认为ÿ ou可以使用以下内容。

cal.set(Calendar。DAY_OF_MONTH,Yourday);