2015-08-08 35 views
0

我试图在5秒后使用报警管理器显示通知。我尝试了很多网站,但无法理解,请举一个简单的例子来解释如何使用闹钟管理器并将通知与它连接。Android管理器在5秒后没有发出通知

我是新手。

这是我用来设置闹钟的功能,5秒后我没有收到通知,而不是在模拟器和android手机中。

public void setAlarm(View view) 
{ 

    Intent alertIntent = new Intent(this, AlertReciver.class); 

    Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000; 

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT)); 

tv.setText("completed"); 
} 

这个类,使其工作

public class AlertReciver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    createNotification(context,"times Up", "5 SEcond has passed", "Alert"); 

} 

public void createNotification(Context context,String msg, String msgText, String msgAlert){ 

    PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ne) 
      .setContentTitle(msg) 
      .setTicker(msgAlert) 
      .setContentText(msgText); 

    mBuilder.setContentIntent(notificIntent); 

    mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND); 

    mBuilder.setAutoCancel(true); 

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 

    mNotificationManager.notify(1, mBuilder.build()); 


} 

}

回答

0

参见:https://developer.android.com/reference/android/app/AlarmManager.html

注:与API开始19(奇巧)报警交货精确:操作系统 将转变警报,以最大限度地减少唤醒和电池使用。有 是新的API来支持需要严格交付 保证的应用程序;请参阅setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。其中 targetSdkVersion早于API 19的应用程序将继续看到 之前的行为,在该行为中, 请求时所有报警均准确传送。

+0

这意味着我有安卓4.2.2意味着API 19所以它不会唤醒,所以我将使用16或更低,但我仍然有问题我usesed闪电还2.2.1 –

+0

我试图puttint setExact它仍然没有工作SRY。我需要更清楚的解释,我只是增加了我的最小api到19。 –