Android broadcast not received from widget - java

I Have a widget that upon button press I send broadcast to service. But I am not receving any broadcast in my onReceive().
My intention is basically on widget button click, it tellls service to perform some action.
But I am unable to receive any broadcast from widget in service.
Here is my widget code -
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews;
ComponentName componentName;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
componentName = new ComponentName(context, MyWidgetProvider.class);
remoteViews.setOnClickPendingIntent(R.id.play,getPendingSelfIntent(context, "PLAY",componentName));
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
#Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName componentName;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
componentName = new ComponentName(context, MyService.class);
if (intent.getAction().equals("PLAY")) {
Log.d("hi","PLAY0");
remoteViews.setOnClickPendingIntent(R.id.engine_lock_unlock,getPendingSelfIntent(context,"PLAY",componentName));
serviceIntent = new Intent(context,MyService.class);
serviceIntent.setAction("PLEASE_PLAY");
Log.d("hi","PLAY1");
context.sendBroadcast(serviceIntent);
}
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
protected PendingIntent getPendingSelfIntent(Context context, String action, ComponentName componentName) {
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(action);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, componentName);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
In MyService,
public class ServiceWidgetReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("hi","PLAY2");
if (intent.getAction().equals("PLEASE_PLAY")) {
}
}
}
My MAnifest looks like this -
<receiver android:name=".widget.MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/homescreen_widget_preview" />
</receiver>
<receiver android:name=".service.MyService$ServiceWidgetReceiver" />
I only get the logs -
PLAY0
PLAY1
Why I dont get my broadcast???

Seams that your receiver has no intent-filter provided. Try to add this in your mainfest file:
<receiver android:name=".service.MyService$ServiceWidgetReceiver">
<intent-filter>
<action android:name="PLEASE_PLAY" />
</intent-filter>
</receiver>

I assume that you have implement the action in intent-filter like Myon said, but the still no broadcast to the onReceive. you may try this:
protected PendingIntent getPendingSelfIntent(Context context, String action, ComponentName componentName) {
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(action);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, componentName);
//this line replace the original
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Actually, i have facing the similar problem yesterday that my widget work fine on my android 5.1 phone. However, I find it cannot perform click on android 7.0 sony phone. I still trying to find out whether is the sony phone os problem or android 7.0 or after problem because after the upgrade of the 7.0 or after, there is so many different issues appeared as they changed quite a lot in the API. Anyway, I used the PendingIntent.FLAG_UPDATE_CURRENT solved the problem I have on 7.0
I just think will directly register the receiver class to manifest like this be work. The service binded doesnt directly affect the receive, as the only thing affect the receiver is context. So, maybe just the receive class is enough.
<receiver android:name="packageName.ServiceWidgetReceiver">
<intent-filter>
<action android:name="PLEASE_PLAY" />
</intent-filter>
</receiver>

I found out the mistake I was doing. I had to registerreceiver and unregister it in Service itself rather in Manifest.
This worked for me.

Related

Activity is not launching from BrodcastReciever, when the application is not running

I am trying set set an alarm using pending intent from the activity,
Below code works fine when the application is running in foreground or background,
But the activity is not launching from OnReceive when the application is not running.
Creating pending intent and setting alarm in activity,
intent = new Intent(getApplicationContext(), AlaramReceiver.class);
alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000, pendingIntent);
Handling OnReceive event
public class AlaramReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
Intent intent1 = new Intent(context.getApplicationContext(), AlaramActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setClassName("mad.todo","mad.todo.notify.AlaramActivity");
context.startActivity(intent1);
}
}
receiver in AndroidManifest.xml
<receiver
android:name=".notify.AlaramReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>

onReceive not called for pendingIntent in android home screenwidget

I've been searching for a while on here, but can't find a solution to this. I have a home screen widget with a configuration screen that saves some parameters inside the preferences. The configuration part works flawlessly, but I cannot for the life of me get the setOnClickPendingIntent to register. No error messages.
AndroidManifest.xml
<receiver android:name=".TrunkWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="ActionTrunkButtonClicked" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/trunk_widget_info" />
</receiver>
My App widget provider:
public class TrunkWidget extends AppWidgetProvider {
private SharedPreferences pref;
private final String clickAction = "ActionTrunkButtonClicked";
// UPDATE FUNCTION
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.trunk_widget);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.trunk_widget);
// Register an onClickListener
Intent intent = new Intent(context, TrunkWidget.class);
intent.setAction(clickAction);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
//Set pendingIntent
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.my_btn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
String actionName = clickAction;
if (actionName.equals(action)) {
//DO STUFF
}
}
I'm pulling my hair on this...if anyone has any idea, I'd be grateful.
Thanks!
Nevermind. I moved this part :
// Register an onClickListener
Intent intent = new Intent(context, TrunkWidget.class);
intent.setAction(clickAction);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
//Set pendingIntent
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.my_btn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
From onUpdate() to updateAppWidget() and now it works. I'm leaving this here in case someone needs it.

AlarmManager not working when app is closed even receiver is set in manifest

Test: sdk 19
My manifest
<receiver android:name=".pushnotification.TimeAlarm"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I set up alarm manager
private void setAlarm(String portionName, String orderId, int time, int requestCode) {
Intent intent = new Intent(this, TimeAlarm.class);
intent.putExtra(TimeAlarm.PORTION_NAME, portionName);
intent.putExtra(MainScreenActivity.ORDER_ID, orderId);
intent.putExtra(ALARM_REQUEST_KEY, requestCode);
int id = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, pendingIntent);
}
This is my receiver
public class TimeAlarm extends BroadcastReceiver {
public static final String PORTION_NAME = "portionName";
private NotificationManager mNotifyMgr;
#Override
public void onReceive(Context context, Intent intent) {
int notificationId = (int) System.currentTimeMillis();
//Do something here
}
}
I have tried a lot of solutions and I can see nothing wrong with my code. But the receiver cannot receive alarm broadcast when the app is closed. When the app is paused, it works ok
You should also register for this intent
<action android:name="android.intent.action.QUICKBOOT_POWERON" />

Android IntentReceiver never receives custom intent from WidgetProvider

(This is my first SE question ever, but I've relied heavily on this site for it's fantastic community as I've bumbled around learning Java and Android!)
I'm creating an Android widget that (for the time being) has only one button that functions as a two-sided dice. Eventually this widget will support all major RPG dice sizes (d6, d8, d20, etc), but for now I'm just trying to get the intent/receiver system working.
Currently nothing happens when I hit the d2 button in my widget. So far as I can tell by debugging my custom intent is fired, but the onReceive in my IntentReceiver never catches it. Any assistance you can provide would be much appreciated!
Here is my WidgetProvider:
public class MyWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
remoteViews.setOnClickPendingIntent(R.id.btnReset, buildButtonPendingIntent(context));
Log.e(null, "Setup remote views & Onclick");
Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();
pushWidgetUpdate(context, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction("ca.sulli.rpgdicewidget.intent.action.D2");
Log.e(null, "Setting new ButtonPendingIntent");
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
Log.e(null, "Updating widget!");
ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
}
And here is my receiver:
public class MyWidgetIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e(null,"Checking intent by receiver");
if(intent.getAction().equals("ca.sulli.rpgdicewidget.intent.action.D2")){
Log.e(null,"Correct intent received!");
updateWidgetPictureAndButtonListener(context);
}
}
private void updateWidgetPictureAndButtonListener(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Log.e(null,"Updating text since the intent was received!");
remoteViews.setTextViewText(R.id.txtResult, "Intent Received! ");
remoteViews.setOnClickPendingIntent(R.id.btnD2, MyWidgetProvider.buildButtonPendingIntent(context));
MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
}
}
And for completions' sake, my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.sulli.rpgdicewidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="MyWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="ca.sulli.rpgdicewidget.intent.action.D2" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
</application>
</manifest>
And the appwidget-provider:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="200dp" android:minHeight="70dp" android:initialLayout="#layout/main" android:updatePeriodMillis="30000">
</appwidget-provider>
Many thanks for any help provided, and many thanks for the help this community has already provided for me by answering so many other questions!
I cannot see anywhere in your code where you are using the PendingIntent that you have created, you can use the alarmmanager to schedule a message, presumably immediately, using code like this:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
Broadcast messages are quite complex though, so you may want to weigh up if you want to use them, you could consider using a regular intent and context.sendBroadcast instead

Android AppWidgetProvider onReceive not called on button click

I have a widget running under android and I would like for it to update itself when the user clicks a button on the widget.
For some reason, the onReceive method is never called when a button is clicked after i install the widget.
I have a onUpdate method like this in my AppWidgetProvider class:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = 0; i < appWidgetIds.length; ++i) {
final RemoteViews rv =
new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);
final Intent nextIntent =
new Intent(context, TestAppWidgetProvider.class);
nextIntent.setAction(TestAppWidgetProvider.NEXT_ACTION);
nextIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
final PendingIntent refreshPendingIntent =
PendingIntent.getBroadcast(context, 0,
nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.next, refreshPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
}
And then in my on receive i have a method that looks like:
#Override
public void onReceive(Context ctx, Intent intent) {
super.onReceive(ctx, intent);
final String action = intent.getAction();
if (action.equals(PREV_ACTION)) {
Toast.makeText(ctx, "Previous clicked..", Toast.LENGTH_SHORT).show();
} else if (action.equals(NEXT_ACTION)) {
Toast.makeText(ctx, "Next was clicked..", Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(ctx, "Other action..", Toast.LENGTH_SHORT).show();
}
}
I have this in my manifest file too:
<receiver android:name="com.test.android.widget.TestAppWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter >
<action android:name="com.test.android.widget.PREV" />
</intent-filter>
<intent-filter >
<action android:name="com.test.android.widget.NEXT" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/appwidget_info" />
</receiver>
After I install the widget and click on the next button, the onReceive method is never called for some reason...
Fixed it, turned out to be trivial.
It was because onReceive wasn't even being called on widget creation, so the event listener wasn't setup.
Added some of the code that was in onReceive (setting the setOnClickPendingIntent etc) to a function which is called when the widget configure activity is ready to update the widget.

Categories

Resources