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
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 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 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); } } } } } }
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
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>
Voilà, merci à tous ceux qui pourront m'aider...
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>
Partager