简体   繁体   English

AlarmManager没有触发BroadcastReceiver

[英]BroadcastReceiver is not being fired by AlarmManager

Here is my code below, what is the problem? 以下是我的代码,问题是什么? If i look at log, there is no start of AlarmReceiver class. 如果我查看日志,则没有AlarmReceiver类的启动。 So no notification is sent to user. 因此,不会向用户发送通知。 I tried every minute in loop and still nothing happened. 我在循环中尝试了每一分钟但仍未发生任何事情。 What am i doing wrong? 我究竟做错了什么?

Thanks in Advance!! 提前致谢!!

public void setRepeatingAlarm(int hour, int min)
{
    Intent myIntent = new Intent(this , AlarmReceiver.class);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, min);
    calendar.set(Calendar.SECOND, 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
}

//Receiver Class AlarmReceiver //接收器类AlarmReceiver

public void onReceive(Context context, Intent intent)
{

    taskDb = new TaskDb(context);

    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);

    String temp ="";
    for(String s: taskDb.selectTodayTasks())
    {
        temp +="-" + s + "\n";
    }

    if(!temp.equals("")) {
        Notification n = new Notification.Builder(context)
                .setContentTitle(context.getString(R.string.today_tasks))
                .setContentText(temp)
                .setLights(Color.CYAN,500,500)
                .setContentIntent(pIntent)
                .setPriority(Notification.PRIORITY_MAX)
                .setAutoCancel(true).build();

        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(MainActivity.NOTIFICATION_SERVICE);

        notificationManager.notify(0, n);
    }

}

//android manifest // android清单

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".main.MainActivity"
        android:windowSoftInputMode="stateHidden"
        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=".background.AlarmReceiver"  android:enabled="true">
    </receiver>
</application>

Change PendingIntent.getService to PendingIntent.getBroadcast : 更改PendingIntent.getServicePendingIntent.getBroadcast

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);

Because PendingIntent.getService is used to Retrieve a PendingIntent that will start a service,... but PendingIntent.getBroadcast for Retrieve a PendingIntent that will perform a broadcast,... 因为PendingIntent.getService用于检索将启动服务 Retrieve a PendingIntent that will perform a broadcast,...但是PendingIntent.getBroadcast用于Retrieve a PendingIntent that will perform a broadcast,...

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

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