Reputation: 135
I faced kind of issue during making some appwidget. I wanted to use AlarmManager to refresh my widget every 15 minutes. Unfortunately my implementation of BroadcastReceiver doesn't receive anything so Toast message doesn't show.
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
<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
@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
public class WidgetUpdateService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "WidgetUpdateService", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 1
Views: 55
Reputation: 1561
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.
getBroadcast : Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().
getService : Retrieve a PendingIntent that will start a service, like calling Context.startService().
Upvotes: 1