每天在特定时间发出通知

时间:2014-07-24 09:49:50

标签: android

您好我是Android的新手。我想在某个时间每天通知用户。我试图使用AlarmManager启动一个服务,在这个服务中我设置了一个通知。我的代码在下面,现在它们无法正常工作:

我的闹钟服务代码:

public class MyAlarmService extends Service 
{

   private NotificationManager mManager;

    @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("static-access")
   @Override
   public void onStart(Intent intent, int startId)
   {
       Intent intents = new Intent(getBaseContext(), MainActivity.class);
       intents.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
       PendingIntent m_PendingIntent = PendingIntent.getActivity(
                getBaseContext(), 0, intents, PendingIntent.FLAG_UPDATE_CURRENT);
       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);



       NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!")
                .setDefaults(Notification.DEFAULT_SOUND
                | Notification.DEFAULT_VIBRATE);

       mBuilder.setContentIntent(m_PendingIntent);

       mManager.notify(0, mBuilder.build());
    }

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

}

我的收件人代码:

public class MyReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
       Intent service1 = new Intent(context, MyAlarmService.class);
       context.startService(service1);

    }   
}
MainActivity中的

代码:

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);

现在,我设置时间后通知不会显示。我的问题在哪里?

2 个答案:

答案 0 :(得分:1)

       Intent myIntent = new Intent(ThisApp.this , myService.class);     
       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
       pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);

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

       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);  //set repeating every 24 hours

答案 1 :(得分:1)

您应该覆盖服务中的onStartCommand()方法,而不是onStart()。仅在Android 2.0平台之前调用onStart()方法。在Android的更高版本中,仅调用onStartCommand()

Reference