简体   繁体   English

AlarmManager 设置重复但 BroadcastReceiver 未收到警报

[英]AlarmManager sets repetition but BroadcastReceiver doesn't receive alarm

I faced kind of issue during making some appwidget .我在制作一些appwidget 时遇到了一些问题。 I wanted to use AlarmManager to refresh my widget every 15 minutes .我想使用 AlarmManager每 15 分钟刷新一次我的小部件。 Unfortunately my implementation of BroadcastReceiver doesn't receive anything so Toast message doesn't show.不幸的是,我的BroadcastReceiver 实现没有收到任何消息,所以没有显示 Toast 消息。

I went thought many similar issues and solutions delivered here and in Google.我去想了很多类似的问题和解决方案在这里和谷歌提供。 Everything what I wrote seems to fit exactly according to tutorials, people answers, and other docs.根据教程、人们的回答和其他文档,我写的所有内容似乎都完全符合。 I must have made little bug or I misunderstood something, but I really can't see what is wrong here.我一定是犯了小错误,或者我误解了一些东西,但我真的看不出这里有什么问题。

Would you please be so kind to look in the code I pasted below?请您看一下我粘贴在下面的代码好吗?

AndroidManifest.xml AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pl.test.mywidget"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


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

        <activity
            android:name=".view.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <receiver android:name=".view.LmWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" />
        </receiver>

        <receiver android:name=".service.WidgetUpdateService"/>

        <receiver android:name=".service.DeviceBootReceiver"
                  android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

    </application>
</manifest>

MyWidgetProvider.java我的WidgetProvider.java

@Override
public void onDisabled(Context context) {
    final Intent intent = new Intent(context, WidgetUpdateService.class);
    final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
    final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pending);
    super.onDisabled(context);
}

@Override
public void onEnabled(Context context) {
    super.onEnabled(context);
    final Intent intent = new Intent(context, WidgetUpdateService.class);
    final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
    final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    int widgetRefreshInterval = 1000 * 60 * 1;  //1 minute just for tests
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, widgetRefreshInterval, pending);
    //alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, widgetRefreshInterval, pending);
}

WidgetUpdateService.java WidgetUpdateService.java

public class WidgetUpdateService extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "WidgetUpdateService", Toast.LENGTH_SHORT).show();
    }

}

Change Your Pending Intent to.将您的待定意图更改为。

PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent.FLAG_UPDATE_CURRENT : Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. PendingIntent.FLAG_UPDATE_CURRENT :表示如果描述的 PendingIntent 已经存在,则保留它,但用这个新 Intent 中的内容替换它的额外数据。

getBroadcast : Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast(). getBroadcast :检索将执行广播的 PendingIntent,例如调用 Context.sendBroadcast()。

getService : Retrieve a PendingIntent that will start a service, like calling Context.startService(). getService :检索将启动服务的 PendingIntent,例如调用 Context.startService()。

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

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