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
| package com.bernard.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.pm.PackageManager;
public class IncomingSms extends BroadcastReceiver {
private static final String ARGUMENT_NAME = "APP_INVENTOR_START";
Button buttonSend;
EditText textPhoneNo;
EditText textSMS;
final SmsManager sms = SmsManager.getDefault();
@SuppressWarnings("deprecation")
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
PackageManager pm= context.getPackageManager();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
System.out.println("IncomingNumber :" + senderNum);
Toast.makeText(context, senderNum + ": " + message, Toast.LENGTH_SHORT).show();
Intent launchIntent = pm.getLaunchIntentForPackage(("appinventor.ai_bernard_pre.IncommingCall"));
launchIntent.putExtra(ARGUMENT_NAME, "SMS " + senderNum);
context.startActivity(launchIntent);
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
public void send(View v) {
//System.out.println("SEND");
//EditText tonEdit = (EditText) findViewById(R.id.editTextPhoneNo);
//String tonTexte = tonEdit.getText().toString();
}
} |
Partager