Android Notification在启动时启动,而不是在设定的时间启动

时间:2016-04-08 21:16:21

标签: android alarmmanager android-notifications

我目前正在尝试创建一个重复闹钟,每天在教程后同时发生,但我似乎无法让它正常工作。它熄灭但是一旦应用程序启动而不是设定的时间,我不确定原因是什么。我还确保服务是在清单中声明的​​。知道我哪里出错了,或者这是正确的方法吗?

主要活动

public class MainActivity extends AppCompatActivity {
private PendingIntent pendingIntent;

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

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    int i = preferences.getInt("numberofLaunces", 1);

    if (i < 2) {
        alarmMethod();
        i++;
        editor.putInt("numberoflaunches", i);
        editor.commit();
    }

    if (savedInstanceState == null) {
        return;
    }

}



private void alarmMethod() {
    Intent myIntent = new Intent(this, NotifyService.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 32);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.AM_PM, Calendar.AM);
    calendar.set(Calendar.DAY_OF_MONTH, 0);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);

    Toast.makeText(MainActivity.this, "start Alarm", Toast.LENGTH_LONG).show();
}
}

Notifyservice

public class NotifyService extends Service {
@Override
public IBinder onBind(Intent Intent) {
    return null;
}

@Override
public void onCreate(){
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1,0);

    Notification mNotify = new Notification.Builder(this)
            .setContentTitle("placeholder tital")
            .setContentText("Placeholder text")
            .setSmallIcon(R.drawable.ic_cat)
            .setContentIntent(pIntent)
            .setSound(sound)
            .addAction(0,"hello", pIntent)
            .build();

    mNM.notify(1, mNotify);



}
}

2 个答案:

答案 0 :(得分:1)

来自开发人员[网站](http://developer.android.com/reference/android/app/AlarmManager.html#set(int,long,android.app.PendingIntent))

  

警报是一个Intent广播,它通过registerReceiver(BroadcastReceiver,IntentFilter)或通过AndroidManifest.xml文件中的标签注册到广播接收器。

您应该定义广播待定意图而不是服务。 改变这个

Intent myIntent = new Intent(this, NotifyService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

到此

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

然后实现MyAlarmBroadcastReceiver在onReceive()内扩展BroadcastReceiver类和fire通知。

答案 1 :(得分:0)

问题很简单:如果警报发出的时间(setRepeating()中的第二个参数)是过去的,它会立即触发。您指定的时间是该月的第0天(?)日的0:32 am。 这总是在过去(可能除了每月32分钟,但我不知道这个月的第0天是什么)。

在凌晨0:32时间每隔24小时发出一次警报,请使用以下内容:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 32);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, new Date().after(calendar.getTime()) ? 1 : 0); //if the current time is after 0:32 am, one day is added

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);