为什么我的AlarmManager没有调用Pendingintent

时间:2016-01-25 13:01:17

标签: android notifications broadcastreceiver alarmmanager notificationmanager

我尝试让我的应用程序使用AlarmManager在5秒后发送通知,但pendingIntent似乎没有被调用。 继承我的代码

在MainActivity中的onCreate:

Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
    Intent alertIntent = new Intent(this, Notification.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime , PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

Notification.class:

public class Notification extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
    NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(context);
            mBuilder.setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Title")
            .setTicker("Ticker")
            .setContentText("Text");
    mBuilder.setContentIntent(notificIntent);
    mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

}

}

Android Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wouter.lifetracker"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".Notification"
        >
        </receiver>

</application>

我搜索了几个小时,但我无法找出为什么没有发生

2 个答案:

答案 0 :(得分:1)

参见:https://developer.android.com/reference/android/app/AlarmManager.html#set(int,long,android.app.PendingIntent)

  

注意:从API 19开始,传递给此方法的触发时间是   视为不精确:此时间之前不会发出警报,   但可能会推迟并在一段时间后交付。操作系统将使用   这个政策是为了在整个过程中“批处理”警报   系统,最大限度地减少设备需要“唤醒”的次数   并尽量减少电池使用。一般来说,警报安排在附近   只要警报安排在远期,将来不会推迟   将来

答案 1 :(得分:1)

只有5秒钟?

试试这个:

new Handler().postDelayed(new Runnable() {
                public void run() {
                showNotification(); //your notification code called after 5 seconds
                }
            }, 5 * 1000); // 5 seconds...

或尝试:

long alertTime = System.currentTimeMillis() + (5 * 1000); // <-- try this
Intent alertIntent = new Intent(this, Notification.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime , PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));