0

SMS を受信するたびに呼び出されるレシーバーがあります

public class SMSReceiver extends BroadcastReceiver {

    private SharedPreferences prefs;
    private String prefName = "MyPref";
    private static final String NUMBER_KEY = "number";

    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        String Sender = null;
        if (bundle != null) {
            // ---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                Sender = msgs[i].getOriginatingAddress();
            }
            prefs = context
                    .getSharedPreferences(prefName, Context.MODE_PRIVATE);
            String phoneNumber = (String) prefs.getString(NUMBER_KEY, "");
            // If the sender of the SMS just received is the same as one chosen
            // earlier
            if (Sender.equals(phoneNumber)) {
                Toast.makeText(context, "text message received",
                        Toast.LENGTH_LONG).show();
                // ---Launch the minderActivity even when the app is not in the
                // foreground---
                Intent minderActivityIntent = new Intent(context, Minder.class);
                minderActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(minderActivityIntent);
                // ---send a broadcast intent to update the SMS received in the
                // activity---
                Intent broadcastIntent = new Intent();
                broadcastIntent.setAction("SMS_RECEIVED_ACTION");
                broadcastIntent.putExtra("Sender", Sender);
                context.sendBroadcast(broadcastIntent);
            }
        }
    }
}

ボタンを使用して「minder」と呼ばれるアクティビティに受信者を登録します

registerReceiver(intentReceiver, intentFilter);

この「マインド」アクティビティでは、BroadcastReceiver

private BroadcastReceiver intentReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // ---gather up all the necessary user input---
        prefs = getSharedPreferences(prefName, MODE_PRIVATE);
        final Button btn2 = (Button) findViewById(R.id.btnContacts);
        String phoneNumber = (String) prefs.getString(NUMBER_KEY, "");
        String messageChosen = (String) prefs.getString(MESSAGE_KEY, "");
        String delay = (String) prefs.getString(DELAY_KEY, "");
        String Sender = (String) intent.getExtras().getString("Sender");
        if (Sender.equals(phoneNumber)) {
            sendSMS(phoneNumber, messageChosen, delay);
        }
    }
};

はすべて でpermissions定義されていmanifestます。

残念ながら、受信者をテストすると(つまり、受信者が登録された後、「phoneNumber」として定義された番号からSMSを電話に送信します)、アプリはアクションを実行しません。上記のコードに何が欠けているのでしょうか?

4

0 に答える 0