Android闹钟管理器不会触发我的活动

时间:2017-01-24 14:29:25

标签: java android notifications alarmmanager

我在向我的应用添加通知时遇到了一些问题。例如,我希望每天早上10点收到通知。这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private PendingIntent alarmIntent;
private AlarmManager alarmMgr;

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

    setAlarm();
}

private void setAlarm() {
    alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 10);
    calendar.set(Calendar.MINUTE, 00);

    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            1000 * 3, alarmIntent);
}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_stat_name)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");
    Intent resultIntent = new Intent(context, MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
}
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.turnu_a.todolist">

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".taskView.newTaskActivity" />
    <activity android:name=".taskView.showTaskDetailsActivity" />
    <activity android:name=".taskView.updateTaskActivity" />
    <activity android:name=".settings.settingsActivity"></activity>
    <receiver android:name=".Alarm.AlarmReceiver"></receiver>
</application>

问题是,即使我在一两分钟内更改了触发器的发生时间(为了测试我的代码),但绝对没有任何反应。我试图在&#34; setAlarm()&#34;中创建通知。方法和它工作,这意味着问题不是来自通知的创建,但alarmManager没有被触发。 我很感激任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

所以...我删除了这一行:

calendar.setTimeInMillis(System.currentTimeMillis());

来自“setAlarm()”方法,现在可以正常工作。

我很高兴它有效,但有点惊讶问题来自这里(虽然事后看来,我不认为这条线是非常有用的。)

无论如何,问题已经解决了,感谢所有试图提供帮助的人。