发送短信给自己并接收它

时间:2016-05-22 12:16:25

标签: android broadcastreceiver sms

我正在构建一个Android应用程序,我想向当前用户发送短信,然后使用BroadcastReceiver读取此短信的内容。目前短信已成功发送,但我的BroadcastReceiver无法正常工作。 我确实在manifest.xml中输入了正确的权限:

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

以下是我片段的代码:

public class GetOtpFragment extends Fragment {

    private SMSReceiver mSMSreceiver;
    private VerificationActivity mActivity;
    private EditText mPhoneNumber;
    private Button mVerifyButton;

    public GetOtpFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (VerificationActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_get_otp, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle b = getArguments();
        mPhoneNumber = (EditText) view.findViewById(R.id.phone_number);
        mVerifyButton = (Button) view.findViewById(R.id.verify_phone_number_button);

        // I register my receiver
        IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        mSMSreceiver = new SMSReceiver();
        mActivity.registerReceiver(mSMSreceiver, intentFilter);

        // verify phone number
        mVerifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // this works
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(mPhoneNumber.getText().toString(), null, "test", null, null);
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        mActivity.unregisterReceiver(mSMSreceiver);
    }
}

我的BroadcastReceiver:

public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // never called
        Toast.makeText(context, "SMS received", Toast.LENGTH_LONG);
        Log.d("---- smsreceiver ---", "in smsreceiver");

    }
}

就像我说的那样,短信真的被发送了,我可以在短信应用中找到它,但是没有通知。问题是我的接收器从未被呼叫,没有日志,也没有吐司,我不知道为什么。是因为sendTextMessage没有创建任何通知吗?

1 个答案:

答案 0 :(得分:-1)

尝试注册清单上的意图,而不是以编程方式注册,看看您是否能够接收广播。

<receiver
    android:name="com.example.SMSReceiver"
    android:permission="android.permission.BROADCAST_SMS">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

如果这不起作用,可能是因为某些其他应用程序在您执行之前捕获广播并使用它(因为SMS_RECEIVED广播是有序广播),如果是这种情况则尝试添加{{1像这样的intent-filter:

android:priority="2147483000"

如果它确实有效,您可能会遇到 <intent-filter android:priority="2147483000"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> 的问题,请确保您的活动保持有效,直到收到短信为止。