在android的特定时间每天本地通知

时间:2015-02-04 07:05:19

标签: android android-notifications android-notification-bar

在我的项目中需要每天下午2点显示通知

我不知道每天为重复通知设置时间间隔

我的主要活动代码设置了一个时间间隔

 Intent myIntent = new Intent(Splash.this ,JobsNotification.class);     
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getService(Splash.this, 0, myIntent, 0);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,12);
        calendar.set(Calendar.MINUTE,26);
        calendar.set(Calendar.SECOND,15);
        alarmManager.setRepeating(AlarmManager.RTC,
                calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY,
                pendingIntent);

通知服务

public class JobsNotification extends Service 
   {
    @Override
    public IBinder onBind(Intent arg0)
    {
       // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() 
    {
       // TODO Auto-generated method stub  
       super.onCreate();
    }

   @SuppressWarnings("deprecation")
@Override
   public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);

       String strTitle="10 New Jobs Posted Today";


    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.dailyjob)
            .setContentTitle(strTitle)
            .setContentText("");
    Intent resultIntent = new Intent(this, Notify.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(Notify.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
      mNotificationManager.notify(1, mBuilder.build());
     //  Uri notificationtune = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
     //  Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notificationtune);
     //  r.play();
    }

    @Override
    public void onDestroy() 
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

请帮助我每天只在特定时间设置通知

先谢谢

2 个答案:

答案 0 :(得分:0)

试试此代码

  // get a Calendar object with current time
        Calendar cal = Calendar.getInstance();
        // add 5 minutes to the calendar object
        cal.add(Calendar.SECOND, 10);

        Intent intent = new Intent(this, AlarmReceiver.class);
        intent.putExtra("alarm_message", "O'Doyle Rules!");

        // In reality, you would want to have a static variable for the request code instead of 192837
        PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

     // Get the AlarmManager service
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
       // am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5, sender);

AlarmReceiver.java

    public class AlarmReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            try
            {
                Bundle bundle = intent.getExtras();
                String message = bundle.getString("alarm_message");
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }
            catch (Exception e)
            {
                Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
                e.printStackTrace();

            }
        }

}

的AndroidManifest.xml

添加权限

  <receiver android:name=".view.AlarmReceiver" >
        </receiver>

在日历中设置特定时间。

答案 1 :(得分:-3)

通知到达时每天2点,设置第二天的通知......
这是您可以获得永久通知的唯一方法:)