[Bundle] Communication depuis service vers activité principale (passage de variables)
.Bonjour !
Je débute en programmation android et java.
Actuellement, je mets en place une application AppWidget, pour afficher des données dans plusieurs textViews.
Cela consiste , en gros, à récupérer des données envoyées par un service, de manière périodique.
Ces données sont alors envoyées à l'aide de la fonction sendBroadcast( intent ) dans la méthode onHandleIntent( Intent intent ) du service étendu de la classe IntentService.
(J’espère que je ne suis pas trop confus.)
Donc, le problème est que je ne parviens pas a afficher les données dans les champs textView.
Tous les champs textViews deviennent vide... Rien ne s'affiche!
De plus, j'ignore si la manière est correcte pour récupérer des données ( bundle ) depuis un service, dans la méthode onReceive (...) ...
Y a t'il une erreur dans le code ?
Ce pourrait-il que j'ai oublié quelque chose dans le AndroidManifest.xml ?
Dans l'espoir que vous puissiez m'aider, je vous montre une partie du code du service et de l'activité principale.
Merci beaucoup, pour vos réponses...
Le service mon_service.class :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
public class mon_service extends IntentService {
public static final String DATA_WMMD_ARTISTE = "Artiste";
public static final String DATA_WMMD_TITRE = "Titre";
public static final String DATA_WMMD_COUVERTURE = "Couverture";
private static final String ACTION_DATA_WMMD_BROADCAST = "PACKAGE.mon_service.ACTION_DATA_WMMD_BROADCAST";
public mon_service() {
super("mon_service");
}
protected onHandleIntent ( Intent intent ) {
// ... //
artiste = entry.artiste;
titre = entry.titre;
couverture = DonnerCouverture(entry.couverture);
Intent broadcastIntent = new Intent(this,PACKAGE.widgetAppProvider.class);
broadcastIntent.setAction(ACTION_DATA_WMMD_BROADCAST);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(DATA_WMMD_ARTISTE, artiste);
broadcastIntent.putExtra(DATA_WMMD_TITRE, titre);
broadcastIntent.putExtra(DATA_WMMD_COUVERTURE, couverture);
sendBroadcast(broadcastIntent);
// ... //
}
} |
Dans mon activité principale ( widgetAppProvider.class ), on met a jours les champs apres la récupération des données, dans la méthode onReceive ( final Context context, Intent intent ).
Voici une partie du code dans onReceive(...)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
@Override
public void onReceive (final Context context, Intent intent) {
final String action = intent.getAction();
if ( MON_ACTION.equals(action)){
IntentService(context);
Bundle bundle = intent.getExtras();
if (bundle != null) {
String donnee = bundle.getString("Artiste");
String donnee2 = bundle.getString("Couverture");
String donnee3 = bundle.getString("Titre");
Log.w("donnée reçu", " est "+donnee);
ComponentName componentNameInit = new ComponentName(context, widgetAppProvider.class);
AppWidgetManager Manager = AppWidgetManager.getInstance(context);
int[] widgetsIds = Manager.getAppWidgetIds(componentNameInit);
for (int WidgetID : widgetsIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
remoteViews.setTextViewText(R.id.textView1, donnee);
remoteViews.setTextViewText(R.id.textView2, donnee2);
remoteViews.setTextViewText(R.id.textView3, donnee3);
Manager.updateAppWidget(WidgetID, remoteViews);
}
}
}
}
// .... //
}
// methode IntentService(Context) :
private void IntentService (Context context) {
Intent intent = new Intent(context, mon_service.class);
context.startService(intent);
} |
AndroidManifest.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="PACKAGE"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- AppWidget -->
<receiver
android:name="widgetAppProvider">
<intent-filter>
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action
android:name="android.intent.action.MAIN"/>
<action
android:name="PACKAGE.ACTION" />
<!-- ( ... ) -->
<action
android:name="PACKAGE.ACTION_DATA_WMMD_BROADCAST"/>
<action
android:name="PACKAGE.mon_service.ACTION_DATA_WMMD_BROADCAST"/>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_meta_info" />
</receiver>
<!-- Services -->
<service
android:name="PACKAGE.mon_service" />
</application>
</manifest> |