Ouvrir une URL à la réception d'un SMS
Bonjour,
Je me suis lancé il y a peu dans le dev Android, j'ai acheté un bouquin et parcouru pas mal de tutos dans le but de faire ouvrir une page dans le navigateur du GSM à la réception d'un SMS contenant un code particulier.
Dans mes essais, je voudrais que le navigateur s'ouvre sur la page www.android.com dés qu'il reçoit un SMS commençant par"#". Tout mes essais sont infructueux, je sollicite donc votre aide. Voici mon fichier Manifest:
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
| <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms_web"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sms_web.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=".SMSReceive">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest> |
et mon fichier MainActivity.java:
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
| package com.example.sms_web;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class MainActivity 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;
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.startsWith("#")){
// action a effectuer à la réception du SMS: ici affichage du mot POKE
// lorsque le SMS commence par ##androPoke##
Toast.makeText(context, "POKE", Toast.LENGTH_SHORT).show();
Uri webpage = Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
context.startActivity(webIntent);
}
Toast.makeText(context, body, Toast.LENGTH_SHORT).show();
}
}
}
}
} |
J'utilise Eclipse, merci par avance à ceux qui pourront m'aider
Bonne soirée
Ludo