简体   繁体   English

Android管理器5秒后未发出通知

[英]Android manager not giving notification after 5 sec

I am trying to use alarm manager to show notification after 5 sec. 我正在尝试使用警报管理器在5秒后显示通知。 I have tried many sites but was not able to understand please give a simple example for explaining how to use alarm manager and connect notification with it. 我已经尝试了许多站点,但无法理解,请举一个简单的示例,说明如何使用警报管理器并将通知与之连接。

I am newbie. 我是新手。

this is function i used to set alarm and I am not getting notification after 5 secs not in emulator nor in android mobile. 这是我用来设置警报的功能,并且5秒钟后在模拟器或Android Mobile中都没有收到通知。

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");
}

and this class to make it work 和这个使它起作用的课程

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());


}

} }

See: https://developer.android.com/reference/android/app/AlarmManager.html 请参阅: https//developer.android.com/reference/android/app/AlarmManager.html

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. 注意:从API 19(KITKAT)开始,警报传递是不准确的:操作系统将转移警报,以最大程度地减少唤醒和电池消耗。 There are new APIs to support applications which need strict delivery guarantees; 有一些新的API支持需要严格交付保证的应用程序。 see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). 请参见setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。 Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested. targetSdkVersion早于API 19的应用程序将继续看到以前的行为,在该行为中,所有警报均在请求时准确传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM