IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Compteur vaut 1 si un mot précis est contenu dans un message reçu ou envoyé


Sujet :

Android

  1. #1
    Futur Membre du Club
    Inscrit en
    Février 2013
    Messages
    33
    Détails du profil
    Informations forums :
    Inscription : Février 2013
    Messages : 33
    Points : 9
    Points
    9
    Par défaut Compteur vaut 1 si un mot précis est contenu dans un message reçu ou envoyé
    Bonjour,

    Je débute en la matière et je tente de créer une application dans laquelle on rentre un mot ou une phrase. Puis, lorsque ce mot est contenu dans un message reçu, on met un compteur à un dans l'application.

    J'ai testé énormément de choses mais je n'y arrive toujours pas.

    Voici mon code :
    MainActivity.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package com.example.fapp;
     
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
     
    public class PokeReceiver extends BroadcastReceiver{
        String msg ="";
        public PokeReceiver(String msg){
            this.msg=msg;
        }
     
        @Override
        public void onReceive(Context context, Intent intent) {
     
            if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
     
                // récupérer SMS
                Bundle bundle = intent.getExtras();       
                SmsMessage[] msgs = null;
     
                if (bundle != null) {
     
                    // récupérer le SMS
                    Object[] pdus = (Object[]) bundle.get("pdus");
     
                    msgs = new SmsMessage[pdus.length];    
                    String body ="";
                    for (int i=0; i<msgs.length; i++){
     
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);               
                        body =  msgs[i].getMessageBody().toString(); 
     
                        if (body.contains(msg)) {
     
                            // action a effectuer à la réception du SMS: 
                            // lorsque le SMS commence par bonjour
                            Intent in = new Intent(context, MainActivity.class);
                            in.putExtra("valeur","1");
                            context.startActivity(in);
                        }
                    }
                }
            }
        }
    }
    Voici PokeReceiver.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package com.example.fapp;
     
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
     
    public class PokeReceiver extends BroadcastReceiver {
     
        String msg ="";
     
        public PokeReceiver(String msg){
     
            this.msg=msg;
        }
     
        @Override
        public void onReceive(Context context, Intent intent) {
     
            if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
     
                // récupérer SMS
                Bundle bundle = intent.getExtras();       
                SmsMessage[] msgs = null;
     
                if (bundle != null) {
     
                    // récupérer le SMS
                    Object[] pdus = (Object[]) bundle.get("pdus");
     
                    msgs = new SmsMessage[pdus.length];    
                    String body ="";
                    for (int i=0; i<msgs.length; i++){
     
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);               
                        body =  msgs[i].getMessageBody().toString(); 
     
                        if (body.contains(msg)) {
     
                            // action a effectuer à la réception du SMS: 
                            // lorsque le SMS commence par bonjour
                            Intent in = new Intent(context, MainActivity.class);
                            in.putExtra("valeur","1");
                            context.startActivity(in);
                        }
                    }
                }
            }
        }
    }
    Mon ActivityMain.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context="com.example.fapp.MainActivity" >
     
        <TextView
            android:id="@+id/saisir"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:paddingTop="10dip"
            android:text="@string/saisirmotouphrase" />
     
        <EditText
            android:id="@+id/saisie"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/saisir"
            android:layout_centerHorizontal="true"
            android:inputType="text"
            android:lines="2"
            android:textSize="15sp" />
     
        <Button
            android:id="@+id/ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/saisie"
            android:layout_centerHorizontal="true"
            android:gravity="right"
            android:text="@string/ok" />
     
        <TextView
            android:id="@+id/recu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:paddingRight="15dip"
            android:text="@string/phrasenombrerecu" />
     
        <TextView
            android:id="@+id/nbrecu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/recu"
            android:layout_alignRight="@+id/recu"
            android:layout_alignTop="@+id/recu"
            android:text="@string/initnbrecu" />
     
        <TextView
            android:id="@+id/envoye"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/recu"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:paddingRight="15dip"
            android:text="@string/phrasenombreenvoye" />
     
        <TextView
            android:id="@+id/nbenvoye"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/envoye"
            android:layout_alignRight="@+id/envoye"
            android:layout_alignTop="@+id/envoye"
            android:text="@string/initnbenvoye" />
     
    </RelativeLayout>
    Et enfin mon Manifest :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.fapp"
        android:versionCode="1"
        android:versionName="1.0" >
     
        <uses-permission android:name="android.permission.SEND_SMS" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
     
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
     
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.fapp.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
     
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
     
            <receiver
                android:name=".PokeReceiver"
                android:enabled="true"
                android:priority="-1" >
                <intent-filter android:priority="0" >
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
        </application>
     
    </manifest>
    Voilà, merci à tous ceux qui pourront m'aider...

  2. #2
    Futur Membre du Club
    Inscrit en
    Février 2013
    Messages
    33
    Détails du profil
    Informations forums :
    Inscription : Février 2013
    Messages : 33
    Points : 9
    Points
    9
    Par défaut
    Je redonne le principe de cette appli d'entraînement :

    On lance l'appli donc une activité, dans un TextView on écrit un mot ou une phrase nommé SAISIE. Lorsque l'on clique sur Ok, l'activité lance un service à qui elle a transmis SAISIE, puis créer un broadcast receiver pour communiqué avec le service. Le service lui aussi crée un broadcast receiver pour attendre la reception d'un SMS, lorsque c'est le cas, il va lire le SMS et vérifié la présence de SAISIE. Il envoie enfin à l'activité un "1" pour lui dire que le mot est présent. L'activité met alors à jour son TextView.

    Depuis mon dernier poste mon code à changé, l'activité et le service se lance comme il faut, mais je n'arrive pas à lancer deux AVD pour simuler l'envoie d'un SMS.

    MainActivity.java

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    package com.example.fapp;
     
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
     
    public class MainActivity extends ActionBarActivity {
    	/* Récupération des éléments du GUI */
    	private Button ok = null;
    	private RelativeLayout rlayout = null;
    	private EditText saisie = null;
    	private TextView saisir = null;
    	private TextView nbenvoye = null;
    	private TextView nbrecu = null;
    	private TextView envoye = null;
    	private TextView recu = null;
    	private String receivedatafromservice ="";
    	private MsgReceiver msgreceiver;
     
    	public static String ESTOUPAS = "ESTOUPAS";
     
    	public class MsgReceiver extends BroadcastReceiver{
    		@Override
    		public void onReceive(Context context, Intent intent) {
    			receivedatafromservice = intent.getExtras().getString("recu");
    			nbrecu.setText(receivedatafromservice);
    		}
    	}
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		ok = (Button) findViewById(R.id.ok);
    		rlayout = (RelativeLayout) findViewById(R.id.rlayout);
    		saisir = (TextView) findViewById(R.id.saisir);
    		nbenvoye = (TextView) findViewById(R.id.nbenvoye);
    		nbrecu = (TextView) findViewById(R.id.nbrecu);
    		envoye = (TextView) findViewById(R.id.envoye);
    		recu = (TextView) findViewById(R.id.recu);
    		saisie = (EditText) findViewById(R.id.saisie);
     
     
    		ok.setOnClickListener(new View.OnClickListener() {
    			@Override
    			public void onClick(View vue) {
     
    				Intent starter=new Intent(MainActivity.this,ServiceS.class);
    				System.out.println(saisie.getText().toString());
    				starter.putExtra("saisie", saisie.getText().toString());
    				msgreceiver = new MsgReceiver();
    				registerReceiver(msgreceiver, new IntentFilter("ESTOUPAS"));
    				startService(starter);
    			}
    		});
     
     
    	}
     
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
     
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
     
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		// Handle action bar item clicks here. The action bar will
    		// automatically handle clicks on the Home/Up button, so long
    		// as you specify a parent activity in AndroidManifest.xml.
    		int id = item.getItemId();
    		if (id == R.id.action_settings) {
    			return true;
    		}
    		return super.onOptionsItemSelected(item);
    	}
     
    	/**
             * A placeholder fragment containing a simple view.
             */
    	public static class PlaceholderFragment extends Fragment {
     
    		public PlaceholderFragment() {
    		}
     
    		@Override
    		public View onCreateView(LayoutInflater inflater, ViewGroup container,
    				Bundle savedInstanceState) {
    			View rootView = inflater.inflate(R.layout.fragment_main, container,
    					false);
    			return rootView;
    		}
    	}
     
    }
    Le Service.java

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    package com.example.fapp;
     
    import android.app.Service;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.telephony.SmsMessage;
    import android.util.Log;
     
    public class ServiceS extends Service {
     
    	private String msg = "";
    	private PokeReceiver  messageReceiver;
     
    	@Override
    	public int onStartCommand(Intent intent, int flags, int startID){
    		Log.d(this.getClass().getName(), "onStart");
    		String saisie = intent.getExtras().getString("saisie");
    		Log.d(this.getClass().getName(), "mot a compter "+saisie);
    		msg = saisie;
    		messageReceiver = new PokeReceiver();
    		registerReceiver(messageReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
    		return START_NOT_STICKY;
    	}
     
     
    	public class PokeReceiver extends BroadcastReceiver{
    		@Override
    		public void onReceive(Context context, Intent intent) {
     
    			if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
    				// récupérer SMS
    				Bundle bundle = intent.getExtras();       
    				SmsMessage[] msgs = null;
    				Log.d(this.getClass().getName(), "TEXTO INTER PAR SERVICES");
    				if (bundle != null)
    				{
    					// récupérer le SMS
    					Object[] pdus = (Object[]) bundle.get("pdus");
     
    					msgs = new SmsMessage[pdus.length];    
    					String body ="";
    					for (int i=0; i<msgs.length; i++){
    						msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);               
    						body =  msgs[i].getMessageBody().toString(); 
    						if(body.contains(msg)){
    							// action a effectuer à la réception du SMS: 
    							// lorsque le SMS commence par bonjour
    							Log.d(this.getClass().getName(), "Le message contient le mot cherché");
     
    							Intent in = new Intent(MainActivity.ESTOUPAS);
    							in.putExtra("recu","1");
    							sendBroadcast(in);
     
    						}
    					}
     
    				}
     
    			}
    		}
    	}
     
    	@Override
    	public IBinder onBind(Intent intent) {
    		// TODO Auto-generated method stub
    		return null;
    	}
     
     
    	@Override
    	public void onDestroy(){
    		Log.d(this.getClass().getName(), "onDestroy");
     
    	}
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    Le Manifest
     
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.fapp"
        android:versionCode="1"
        android:versionName="1.0" >
     
        <uses-permission android:name="android.permission.SEND_SMS" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
     
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
     
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.fapp.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
     
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
     
            <receiver
                android:name=".ServiceS$PokeReceiver"
                android:enabled="true"
                android:priority="-1" >
                <intent-filter android:priority="100" >
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
     
            <service android:name=".ServiceS" />
        </application>
     
    </manifest>

    Et j'ai des doutes sur le manifest au niveau des receiver.

    Merci d'avance

Discussions similaires

  1. [XL-2007] ouverture d'un formulaire lorsqu'un mot précis est sélectionné dans une liste
    Par blackstrange dans le forum Macros et VBA Excel
    Réponses: 127
    Dernier message: 22/06/2012, 15h40
  2. [String] Recherche si un mot est contenu dans une phrase
    Par Marvelll dans le forum Débuter avec Java
    Réponses: 8
    Dernier message: 05/02/2010, 10h19
  3. [XL-2000] Verrouiller une cellule quand un mot clé est saisi dans une autre
    Par Paloma dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 26/06/2009, 11h36
  4. [RegEx] Rechercher si mot est contenu dans un autre
    Par Seb33300 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 24/08/2007, 23h52
  5. Réponses: 1
    Dernier message: 22/05/2006, 07h16

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo