2010-12-01 40 views
5

Le code ci-dessous doit décrire une application dans laquelle, une fois le bouton du widget cliqué, elle envoie une intention qui devrait être reçue par TestReceiver. Cependant, en exécutant mon code ci-dessous, l'onReceive de TestReceiver n'est jamais appelé.Android Widget Clic et le récepteur de diffusion ne fonctionne pas

Quelqu'un pourrait-il me faire savoir ce que je fais mal?

code 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); 



    } 


} 

}

Code de récepteur:

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); 
     } 
    } 

    } 

Manifest:

<?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> 
+0

La question stupide d'abord - est-ce que cela fonctionne si vous créez simplement un Intent régulier() et appelez startActivity() quelque part dans votre application? Assurez-vous simplement que le récepteur est correctement configuré. – EboMike

+0

J'ai ajouté context.sendBroadcast (intention); à la fonction onUpdate dans le widget. Débogage maintenant, il semble que son appel le récepteur sur cette déclaration et maintenant quand je clique sur le bouton. Je pense que je commençais à être confus parce que l'appel de Toast que je faisais ne faisait rien. – Kratz

+0

Oui, car vous n'avez pas ajouté '.show()' :) – EboMike

Répondre

7

Il fonctionne probablement, mais vous avez oublié d'ajouter à .show() la fin de votre Toast :)

0

== tests pour l'égalité de référence (s'il s'agit du même objet).

.equals() teste l'égalité des valeurs (qu'elles soient logiquement "égales").

Les valeurs de chaîne sont comparées en utilisant '==' non 'égal'

Cette "if(intent.getAction()==TEST_INTENT)" changer cette "if(intent.getAction().equals(TEST_INTENT))"

et bien sûr Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();

Tout le code:

package *********; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 


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 holaaa", Toast.LENGTH_SHORT).show(); 

     if(intent.getAction() == TEST_INTENT) 
      // if(intent.getAction().equals(TEST_INTENT)) 
     { 
      System.out.println("GOT THE INTENT"); 

      Toast.makeText(context, "Test Goooo", Toast.LENGTH_SHORT).show(); 
     } 
    } 
}