Почему мой класс ReceiveMessage.java не устанавливает тело SMS для моего текстового представления? - PullRequest
0 голосов
/ 05 июня 2018

У меня есть кнопка в деятельности.При нажатии на него отправляется SMS с заранее заданным телом и адресом телефона, а затем на мой мобильный телефон автоматически отправляется ответное SMS с кодом.Мне нужно получить код и установить его для моего просмотра текста.Как я могу решить это?У меня есть два действия: MainActivity и MessageReceiver.

    //MessageReceiver.java
    public class MessageReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        SmsMessage[] messages;
        String str = "";

        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            messages = new SmsMessage[pdus != null ? pdus.length : 0];
            for (int i = 0; i < messages.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) (pdus != null ? pdus[i] : null));
                str += messages[i].getOriginatingAddress();
                str += ":";
                str += messages[i].getMessageBody();
                str += "\n";
            }
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("SMS_RECIEVED_ACTION");
            broadcastIntent.putExtra("message", str);
            context.sendBroadcast(broadcastIntent);}}}

     //MainActivity.java
     private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            TextView chargeText = (TextView) findViewById(R.id.chargeText);
            Log.d("myTag", "is chargeText null? : " + (chargeText==null));
            Log.d("myTag", "The text is: " + (intent.getExtras().getString("message")));
            chargeText.setText(intent.getExtras().getString("message"));
        }
    };

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");
 button3 = (Button) findViewById(R.id.button);

 button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String mymsg = "CMDACC_1234";
                String thenumber = "09380638202";
                SendChargeMessage(thenumber, mymsg);
            }
        });}

    public void SendChargeMessage(String thenumber, String mymsg) {
        String SENT = "Message sent";
        String DELIVERED = "Message delivered";
        SmsManager smsManager = SmsManager.getDefault();
        Context curContext = this.getApplicationContext();
        PendingIntent sentPending = PendingIntent.getBroadcast(curContext,
                0, new Intent("SENT"), 0);
        curContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Sent.",
                                Toast.LENGTH_LONG).show();
                        break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
                        Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
                        Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
                                Toast.LENGTH_LONG).show();
                        break;
                }
            }
        }, new IntentFilter("SENT"));

        PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext,
                0, new Intent("DELIVERED"), 0);

        curContext.registerReceiver(
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode()) {
                            case Activity.RESULT_OK:
                                Toast.makeText(getBaseContext(), "Delivered.",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case Activity.RESULT_CANCELED:
                                Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
                                        Toast.LENGTH_LONG).show();
                                break;
                        }
                    }
                }, new IntentFilter("DELIVERED"));

        PackageManager pm = this.getPackageManager();

        if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
                !pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
            Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
        } else {
            smsManager.sendTextMessage("09380638202", null, "CMDACC_1234", sentPendin

g, deliveredPending);
                //chargeText.setText(SMSBody1);
            }
        }

///Edited

 @Override
protected void onResume(){
    super.onResume();
    IntentFilter intentFilter;
    intentFilter = new IntentFilter();
    intentFilter.addAction("SMS_RECEIVED_ACTION");

    BroadcastReceiver intentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            TextView chargeText = (TextView) findViewById(R.id.chargeText);
            Log.d("myTag", "is chargeText null? : " + (chargeText==null));

            Log.d("myTag", "The text is: " + (intent.getExtras().getString("message")));

            String text = intent.getExtras().getString("message").toString(); // let's try this with toString() so we are very explicit about it

            Log.d("myTag", "The converted text is: " + text);

            chargeText.setText(text);
        }
    };

    registerReceiver(intentReceiver , intentFilter);

} }

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dearzeinab.emaapplication">

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.example.dearzeinab.emaapplication.MessageReceiver" android:enabled="true">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

Ответы [ 2 ]

0 голосов
/ 05 июня 2018

Используйте код ниже в вашем приемнике

 @Override
    public void onReceive(Context context, Intent intent) {

        final Bundle bundle = intent.getExtras();
        try {
            if (bundle != null) {
                Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (Object aPdusObj : pdusObj) {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
                    String senderAddress = currentMessage.getDisplayOriginatingAddress();
                    String message = currentMessage.getDisplayMessageBody();

                    Log.d(TAG, "Received SMS: " + message + ", Sender: " + senderAddress);

                    // if the SMS is not from our gateway, ignore the message
                    if (!senderAddress.toLowerCase().contains(GlobalApplication.SMS_ORIGIN.toLowerCase())) {
                        return;
                    }

                    // verification code from sms
                    String verificationCode = getVerificationCode(message);

                    Log.d(TAG, "OTP received: " + verificationCode);

                    mListener.otpReceived(verificationCode);

                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }

    /**
     * Getting the OTP from sms message body
     * ':' is the separator of OTP from the message
     *
     * @param message
     * @return
     */
    private String getVerificationCode(String message) {
        String str_msg = message.replace("Dear Customer,","");
        str_msg = str_msg.replace("is your one time password (OTP). Please enter the OTP to proceed.","");
        str_msg = str_msg.replace("Thank you,","");
        str_msg = str_msg.replace("ChannelPaisa","");
        str_msg = str_msg.replace("\n","");

        str_msg= str_msg.trim().toString();

        return str_msg;
    }
0 голосов
/ 05 июня 2018

Вам необходимо зарегистрировать свой приемник.

Примерно так:

getActivity().registerReceiver(intentReceiver, new IntentFilter("SMS_RECIEVED_ACTION"));

Только тогда это будет работать.

https://developer.android.com/reference/android/content/BroadcastReceiver

https://developer.android.com/guide/components/broadcasts

Вот как это должно выглядеть в вашем коде:

//MainActivity.java
    private BroadcastReceiver intentReceiver; // do not set it up here. We'll set it and register it onCreate(), but we'll keep it here so it stays on scope of every other function
    private IntentFilter intentFilter; // same for the filter

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // We'll set up the receiver here, after the activity starts
        intentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                TextView chargeText = (TextView) findViewById(R.id.chargeText);
                Log.d("myTag", "is chargeText null? : " + (chargeText==null));

                Log.d("myTag", "The text is: " + (intent.getExtras().getString("message")));

                String text = intent.getExtras().getString("message").toString(); // let's try this with toString() so we are very explicit about it

                Log.d("myTag", "The converted text is: " + text);

                chargeText.setText(text);
            }
        };

        // then, we'll create the filter
        intentFilter = new IntentFilter();

        intentFilter.addAction("SMS_RECEIVED_ACTION");
        button3 = (Button) findViewById(R.id.button);

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String mymsg = "CMDACC_1234";
                String thenumber = "09380638202";
                SendChargeMessage(thenumber, mymsg);
            }
        });

        registerReceiver(intentReceiver , intentFilter); // registering
    }

    public void SendChargeMessage(String thenumber, String mymsg) {
        String SENT = "Message sent";
        String DELIVERED = "Message delivered";
        SmsManager smsManager = SmsManager.getDefault();
        Context curContext = this.getApplicationContext();
        PendingIntent sentPending = PendingIntent.getBroadcast(curContext, // Are you sure you want to create a new intent here?
                0, new Intent("SENT"), 0);
        curContext.registerReceiver(new BroadcastReceiver() { // and are you sure this is supposed to be a new receiver as well? Are you registering it like we did with iontentReceiver?
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Sent.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
                                Toast.LENGTH_LONG).show();
                        break;
                }
            }
        }, new IntentFilter("SENT"));

        PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext, 
                0, new Intent("DELIVERED"), 0);

        curContext.registerReceiver( 
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode()) {
                            case Activity.RESULT_OK:
                                Toast.makeText(getBaseContext(), "Delivered.",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case Activity.RESULT_CANCELED:
                                Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
                                        Toast.LENGTH_LONG).show();
                                break;
                        }
                    }
                }, new IntentFilter("DELIVERED"));

        PackageManager pm = this.getPackageManager();

        if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
                !pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
            Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
        } else {
            smsManager.sendTextMessage("09380638202", null, "CMDACC_1234", sentPending, deliveredPending);
            //chargeText.setText(SMSBody1);
        }
    }

Что я видел в вашем коде:

  • Вы регистрировали получателяперед onStart ().Этот приемник был в области применения для каждого метода.Но он не был зарегистрирован.

  • Вы создавали новый получатель в onResume () с тем же именем, что и предыдущий (broadcastReceiver).Вы правильно регистрировали этот, но он не входил в сферу действия каждой функции.

  • Это означает, что вы пытались получить доступ к незарегистрированному получателю и регистрировали получателя с тем же именем.но за рамками.

Если это не сработает, по крайней мере, вы будете на шаг ближе к тому, чтобы заставить его работать;)

Если вы хотите, чтобы мое мнение, вы могли бы:

  • Изучить область и как она работает в объектно-ориентированном программировании;

  • Изучить получателей, намерения и способы их регистрации.

Надеюсь, это поможет.Удачи, приятель.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...