警报管理器不显示通知

时间:2015-11-30 10:51:12

标签: android broadcastreceiver alarmmanager android-notifications

我是Android的新手,正在使用Notification Alarm Manager来显示通知。我在API 23上。 我在下面发布的代码工作得很好,但我不能说明为什么它没有通知我?

任何人都可以帮我解决它的错误吗? 感谢。

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清单

<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"/>

1 个答案:

答案 0 :(得分:0)

您似乎在未来很快就试图为今天设置闹钟。但是,月份从零开始,因此11的值实际上是12月。如果您想要11月,则相应的值为10。但是,更好的选择是使用类常量:Calendar.NOVEMBER