报警管理器不显示通知 [英] Alarm Manager Not Showing Notifications

查看:319
本文介绍了报警管理器不显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和关于通知报警管理工作以示通知。我对API 23。
在code我贴在下面的工作是完美的,但我不能filgure为什么不通知我?

任何一个可以帮助我它是什么错误?
谢谢你。

MainActivity

 公共类MainActivity扩展AppCompatActivity {按钮B;
私人GoogleApiClient客户端;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    B =(按钮)this.findViewById(R.id.btn);
    工具条工具栏=(栏)findViewById(R.id.toolbar);
    setSupportActionBar(工具栏);
    台历挂历= Calendar.getInstance();    calendar.set(的Calendar.MONTH,11);
    calendar.set(Calendar.YEAR,2015年);
    calendar.set(Calendar.DAY_OF_MONTH,30);    calendar.set(Calendar.HOUR_OF_DAY,15);
    calendar.set(Calendar.MINUTE,34);
    calendar.set(Calendar.SECOND,0);
    calendar.set(Calendar.AM_PM,Calendar.PM);    意图myIntent =新意图(MainActivity.this,AlarmBroadcastReceiver.class);
    的PendingIntent的PendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);    AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC,calendar.getTimeInMillis(),的PendingIntent);}

AlarmBroadcastReceiver

 公共类AlarmBroadcastReceiver扩展广播接收器{@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    意图startIntent =新意图(背景下,AlarmService.class);
    context.startService(startIntent);
}
}

AlarmService

 公共类AlarmService延伸服务{
@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){    generarNotificacion();    返回Service.START_NOT_STICKY;
}@覆盖
公众的IBinder onBind(意向意图){
    返回null;
}公共无效generarNotificacion(){    意图resultIntent =新意图(this.getApplicationContext(),MainActivity.class);    android.support.v4.app.NotificationCompat.Builder mBuilder =
            新NotificationCompat.Builder(本)
                    .setSmallIcon(R.drawable.heart1)
                    .setContentTitle(getResources()。的getString(R.string.app_name))
                    .setContentText(getResources()的getString(R.string.text));    的PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    这个,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );    mBuilder.setContentIntent(resultPendingIntent);    //设置一个ID为通知
    INT mNotificationId = 001;    //获取NotificationManager服务的一个实例
    NotificationManager mNotifyMgr =
            (NotificationManager)getSystemService(NOTIFICATION_SERVICE);    //构建通知和问题了。
    mNotifyMgr.notify(mNotificationId,mBuilder.build());
}}

Android清单

 <使用许可权-SDK-23机器人:名字=android.permission.WAKE_LOCK/>
<使用许可权的android:NAME =android.permission.WAKE_LOCK/><服务机器人:名字=。AlarmService
        机器人:启用=真/>    <接收机器人:名字=。AlarmBroadcastReceiver/>


解决方案

看样子,你要为如今的报警,不久的未来。然而,几个月是从零开始,所以值 11 实际上是12月合约。如果你想要月,对应的值是 10 。一个更好的选择,虽然是使用类常量: Calendar.NOVEMBER

I am new to android and working on Notification Alarm Manager to show notification. I am on API 23. The code I posted below is working perfectly but I cannot filgure why it is not notifying me?

Can Any one help me what is the error in it? Thanks.

MainActivity

public class MainActivity extends AppCompatActivity {

Button b;
private GoogleApiClient client;

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

    b = (Button) this.findViewById(R.id.btn);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.MONTH, 11);
    calendar.set(Calendar.YEAR, 2015);
    calendar.set(Calendar.DAY_OF_MONTH, 30);

    calendar.set(Calendar.HOUR_OF_DAY, 15);
    calendar.set(Calendar.MINUTE, 34);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.AM_PM,Calendar.PM);

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

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

}

AlarmBroadcastReceiver

public class AlarmBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent startIntent = new Intent(context, AlarmService.class);
    context.startService(startIntent);
}
}

AlarmService

public class AlarmService extends Service{
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    generarNotificacion();

    return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void generarNotificacion(){

    Intent resultIntent = new Intent(this.getApplicationContext(), MainActivity.class);

    android.support.v4.app.NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.heart1)
                    .setContentTitle(getResources().getString(R.string.app_name))
                    .setContentText(getResources().getString(R.string.text));

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    mBuilder.setContentIntent(resultPendingIntent);

    // Sets an ID for the notification
    int mNotificationId = 001;

    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

}

Android Manifest

<uses-permission-sdk-23 android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<service android:name=".AlarmService"
        android:enabled="true" />

    <receiver android:name=".AlarmBroadcastReceiver"/>

解决方案

It appears that you're trying to set an alarm for today, shortly in the future. However, months are zero-based, so a value of 11 is actually December. If you want November, the corresponding value is 10. A better option, though, is to use the class constant: Calendar.NOVEMBER.

这篇关于报警管理器不显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆