如何将此类的Intent传递给活动?

时间:2018-05-24 17:12:12

标签: android

在我的应用中,我希望收到特定电话号码的验证代码。 接收后,意图需要传递给其他活动。指导我如何做到这一点,并告诉我问题出在哪里。如果可以,请教我如何通过简单的代码接收短信并读取特定电话号码的短信。 或引入具有此类活动的源代码的应用程序。

public class MySMSBroadCastReceiver extends BroadcastReceiver {
    String specificPhoneNumber = "+989033001001";//phNum .Sender

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

        // Get Bundle object contained in the SMS intent passed in
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] smsm = null;
        String sms_str = "";

        if (bundle != null) {

            // Get the SMS message
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus"); //pdus
            smsm = new SmsMessage[pdus.length];
            for (int i = 0; i < smsm.length; i++) {
                smsm[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                sms_str += smsm[i].getMessageBody().toString();
                String Sender = smsm[i].getOriginatingAddress();
                if (specificPhoneNumber.equals(Sender)) {
                    Uri uri = Uri.parse("content://sms/inbox");
                    ContentResolver contentResolver = context.getContentResolver();
                    String where = "address="+Sender;
                    Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,
                        null);
                    while (cursor.moveToNext()) {
                        long thread_id = cursor.getLong(1);
                        where = "thread_id="+thread_id;
                        Uri thread = Uri.parse("content://sms/inbox");
                        context.getContentResolver().delete(thread, where, null);
                    }

                    if (sms_str == "333") {
                        Toast.makeText(context,"тест", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(context, sms_str, Toast.LENGTH_LONG).show();
                    }

                    Intent l = new Intent(context,SmsReport.class);
                    l.putExtra("msg",sms_str); //Transfer Data Between Activities Using Intent
                    l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    abortBroadcast();
                }
            }
        }
    }
}

0 个答案:

没有答案