• Aucun résultat trouvé

Tutoriel Android Communication entre services BroadCast Receiver Romain Raveaux

N/A
N/A
Protected

Academic year: 2022

Partager "Tutoriel Android Communication entre services BroadCast Receiver Romain Raveaux"

Copied!
15
0
0

Texte intégral

(1)

Tutoriel  Android  

Communication  entre  services     BroadCast  Receiver  

Romain  Raveaux  

 

Préambule  ...  1  

Prérequis  ...  2  

Architecture  ...  3  

Envoi  d’un  message  au  ServiceS1  ...  5  

Mise  en  œuvre  ...  6  

Modification  du  Manifest  ...  7  

Les  classes  du  tutorial  ...  8  

Classe  ServiceS1  ...  9  

Classe  ServiceS2  ...  11  

Classe  TutoActivity  ...  13  

Manifest  de  l’application  ...  15    

 

Préambule  

Les  services  ne  peuvent  pas  communiquer  par  le  biais  de  l’interface  «  IBinder  ».  La   communication  par  IBinder  ne  permet  que  la  communication  entre  une  Activity  et  un   service.  La  solution  pour  résoudre  le  problème  est  d’utiliser  les  BroadCast  Receiver    

   

     

Service  S1   Service  S2  

message  

(2)

Prérequis  

Savoir  démarrer  et  arrêter  un  service.  

Savoir  créer  une  application  Android.  

   

(3)

Architecture  

On  crée  un  service  «  ServiceS1  »  avec  deux  variables  :  Une  donnée  à  envoyer  et  une  variable   pour  recevoir  un  message.  

public class ServiceS1 extends Service { String datatosend ="ping";

String receiveddata;

@Override

public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null;

} /**

* @param args */

public static void main(String[] args) { // TODO Auto-generated method stub }

A l’intérieur de la classe « ServiceS1 », on crée une classe BroadCastReceiver.

public class S1Receiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent arg1) { // TODO Auto-generated method stub

Toast.makeText(context, "reçu !!!"

,Toast.LENGTH_LONG ).show();

Log.d(this.getClass().getName(), "reçu !!!");

receiveddata = arg1.getExtras().getString("id");

Toast.makeText(context, receiveddata ,Toast.LENGTH_LONG ).show();

} }

}    

(4)

 

On  crée  le  constructeur  du  service  «  ServiceS1 ». La variable messageReceiver  

est  une  instance  de  S1Receiver. Il faut enregistrer le S1Receiver pour

avertir l’application que le service ServiceS1 possède un BroadCast Receiver qui pourra être appelé par d’autres services.

MESSAGE_S1 est l’identifiant du BroadCast Receiver S1Receiver.

S1Receiver messageReceiver;

public static String MESSAGE_S1 = "MESSAGE_S1";

public ServiceS1(){

super() ;

messageReceiver = new S1Receiver();

registerReceiver(messageReceiver, new IntentFilter(MESSAGE_S1));

}

   

(5)

Envoi  d’un  message  au  ServiceS1  

Pour  illustrer  la  communication  entre  deux  services,  nous  avons  besoin  de  deux  services.  

On crée un autre service « ServiceS2 ».

public class ServiceS2 extends Service { String datatosend ="pong";

String receiveddata;

@Override

public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null;

} /**

* @param args */

public static void main(String[] args) { // TODO Auto-generated method stub }

}      

Lors  du  démarrage  du  service  S2,  ce  dernier  enverra  un  message  au  service  S1.  

Le  service  S2  envoie  par  le  biais  de  la  méthode  sendBroadcast  un  Intent  contenant  la   variable  datatosend.

 

Intent sender = new Intent(ServiceS1.MESSAGE_S1);

sender.putExtra("id", datatosend);

sendBroadcast(sender);

   

   

(6)

 

Mise  en  œuvre  

Les  services  s’exécutent  en  tâche  de  fond  de  manière  parallèle.  

             

 

       

 

   

       

   

Pour  expérimenter  notre  communication  entre  services  nous  avons  besoin  d’une  activité   avec  un  bouton  permettant  de  démarrer  les  services.  

                                     

Démarrage  de  l’activité  

Démarrage  du   service  S1  

Démarrage  du   service  S2  

Envoi  du  message  a  S1   Mise  en  place  du  

BroadCast  Receiver  

Réception  du   message  

temps  

(7)

Voici  le  code  de  la  méthode  «  onCreate  »  de  l’activité.  Lors  de  l’appuie  sur  un  bouton,  on   démarre  le  service  S1  et  le  service  S2.  

   

   

@Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.tutolayout);

start = (Button) this.findViewById(R.id.button1);

start.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub Intent starter = new

Intent(TutoActivity.this,ServiceS1.class);

startService(starter);

starter = new

Intent(TutoActivity.this,ServiceS2.class);

startService(starter);

} });

}    

Modification  du  Manifest  

Dans  le  manifest  de  l’application,  il  faut  rajouter  la  déclaration  des  services  entre  les  balises  

«  application  ».  

<service android:name=".services.ServiceS1" />

<service android:name=".services.ServiceS2" />

   

(8)

Les  classes  du  tutorial  

 

   

(9)

Classe  ServiceS1  

package  com.example.carteauxtresors.services;  

 

import  com.example.carteauxtresors.services.Ordonnanceur.MyReceiver;  

 

import  android.app.Service;  

import  android.content.BroadcastReceiver;  

import  android.content.Context;  

import  android.content.Intent;  

import  android.content.IntentFilter;  

import  android.os.IBinder;  

import  android.util.Log;  

import  android.widget.Toast;  

 

public  class  ServiceS1  extends  Service  {    

  String  datatosend  ="ping";  

  String  receiveddata;  

  S1Receiver  messageReceiver;  

  public  static  String  MESSAGE_S1  =  "MESSAGE_S1";  

 

  public  ServiceS1(){  

    super();  

 

  }    

  @Override    

  public  int  onStartCommand(Intent  intent,  int  flags,  int  startId)  {         Log.d(this.getClass().getName(),  "onStart");    

    Log.d("LocalService",  "Received  start  id  "  +  startId  +  ":  "  +  intent);  

 

    messageReceiver  =  new  S1Receiver();  

    registerReceiver(messageReceiver,  new  IntentFilter(MESSAGE_S1));  

 

    return  START_NOT_STICKY;    

  }    

  @Override  

  public  IBinder  onBind(Intent  arg0)  {  

    //  TODO  Auto-­‐generated  method  stub       return  null;  

  }    

  /**  

   *  @param  args  

(10)

    //  TODO  Auto-­‐generated  method  stub    

  }    

 

  public  class  S1Receiver  extends  BroadcastReceiver  {  

      @Override  

    public  void  onReceive(Context  context,  Intent  arg1)  {         //  TODO  Auto-­‐generated  method  stub  

      Toast.makeText(context,  "S1  reçu  !!!"  ,Toast.LENGTH_LONG  ).show();  

      Log.d(this.getClass().getName(),  "S1  reçu  !!!");    

      receiveddata  =  arg1.getExtras().getString("id");  

      Toast.makeText(context,  receiveddata  ,Toast.LENGTH_LONG  ).show();  

    }  

    }    

 

  @Override    

  public  void  onDestroy()  {    

    Log.d(this.getClass().getName(),  "onDestroy");    

    unregisterReceiver(messageReceiver);  

  }      

}    

   

(11)

Classe  ServiceS2  

package com.example.carteauxtresors.services;

import android.app.Service;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.IBinder;

import android.util.Log;

import android.widget.Toast;

public class ServiceS2 extends Service { String datatosend ="pong";

String receiveddata;

public ServiceS2(){

super();

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.d(this.getClass().getName(), "onStart");

Log.d("LocalService", "Received start id " + startId + ":

" + intent);

Intent sender = new Intent(ServiceS1.MESSAGE_S1);

sender.putExtra("id", datatosend);

sendBroadcast(sender);

return START_NOT_STICKY;

}

@Override

public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null;

} /**

* @param args */

public static void main(String[] args) {

(12)

}

@Override

public void onDestroy() {

Log.d(this.getClass().getName(), "onDestroy");

} }  

   

(13)

Classe  TutoActivity  

package com.example.carteauxtresors;

import com.example.carteauxtresors.services.Ordonnanceur;

import com.example.carteauxtresors.services.ServiceS1;

import com.example.carteauxtresors.services.ServiceS2;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class TutoActivity extends Activity { /**

* @param args */

public static void main(String[] args) { // TODO Auto-generated method stub }

private Button start;

@Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.tutolayout);

start = (Button) this.findViewById(R.id.button1);

start.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub Intent starter = new

Intent(TutoActivity.this,ServiceS1.class);

startService(starter);

starter = new

Intent(TutoActivity.this,ServiceS2.class);

(14)

} });

} }

   

   

(15)

Manifest  de  l’application    

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.carteauxtresors"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="15" />

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".TutoActivity"

android:label="@string/title_activity_main" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category

android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service android:name=".services.ServiceS1" />

<service android:name=".services.ServiceS2" />

</application>

</manifest>  

   

Références

Documents relatifs

The resulting impact of CPHY implementation in receivers in terms of complexity and power consumption highly depends on the way the proposed generic CPHY is

The simulation results were focused on the 8PSK and 16APSK modulations applied in satellite broadcast systems, to demonstrate the complexity and performance trade-off for the

A subscribed user can obtain the encryption keys through point to point links. The policy of transmitting encrypted data ensures that only those users, who have

We consider in this part a BB cellular network like LTE, transmitting linear services to a number of users in the service area.. 6.2.2.1 Deployment

The radio receiver has external cables to control microcontroller like for example the Arduino. It is suitable to control inter alia self-made

Anytime the logic is enabled (Logic Settings switch # 7 is on and switch # 8 is true, or with the B Logic Interface option installed and that input is active), the Logic Active

Common the other side of each switch (Switch Common on the control panel illustration) to Logic Ground.. The Logic Active Tally output controls the lamps for the Cough and

When active low logic (pull to ground) is used by the peripheral device, Ready (+) and Audio Reset (+) connect to the logic supply voltage on the peripheral.. Ready (-) and Audio