El siguiente código debe describir una aplicación donde, una vez que se hace clic en el botón del widget, envía un intento que debe ser recibido por TestReceiver. Sin embargo, al ejecutar mi código a continuación, nunca se llama a la recepción de TestReceiver.Android Widget Click y Broadcast Receiver Not Working
¿Podría alguien decirme qué estoy haciendo mal?
código Widget
public class Widget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
//Intent intent = new Intent(context.getApplicationContext(), TestReceiver.class);
Intent intent = new Intent();
intent.setAction(TestReceiver.TEST_INTENT);
intent.setClassName(TestReceiver.class.getPackage().getName(), TestReceiver.class.getName());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views;
views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setOnClickPendingIntent(R.id.btnTest, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Código del receptor:
public class TestReceiver extends BroadcastReceiver {
public static final String TEST_INTENT= "MyTestIntent";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Test", Toast.LENGTH_SHORT);
if(intent.getAction()==TEST_INTENT)
{
System.out.println("GOT THE INTENT");
Toast.makeText(context, "Test", Toast.LENGTH_SHORT);
}
}
}
Manifiesto:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.intenttest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".TestReceiver" android:label="@string/app_name">
<intent-filter>
<action android:name="MyTestIntent">
</action>
</intent-filter>
</receiver>
<receiver android:label="@string/app_name" android:name="Widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Pregunta estúpida primero: ¿funciona si solo creas un Intent() regular y llamas a startActivity() en algún lugar de tu aplicación? Solo asegúrate de que el receptor esté configurado correctamente. – EboMike
He añadido context.sendBroadcast (intento); a la función onUpdate en el widget. Depurarlo ahora, parece que está llamando al receptor en esa declaración y ahora cuando estoy haciendo clic en el botón. Creo que me estaba confundiendo porque la llamada a Toast que estaba haciendo no hace nada. – Kratz
Sí, porque no agregó '.show()' :) – EboMike