接收短信Android应用

时间:2019-11-05 13:55:58

标签: android broadcastreceiver sms

我在设置将接收SMS消息的应用程序时遇到问题。问题是当我使用android模拟器消息测试应用程序时,但不是在物理设备上。并且仅当应用程序在ReceivedFragment中时,才会显示消息并显示日志消息。否则,没有任何消息的踪迹。

我尝试按照以下链接中的说明进行设置:https://google-developer-training.github.io/android-developer-phone-sms-course/Lesson%202/2_p_2_sending_sms_messages.html

我没有得到任何积极的结果,我可以说我被困住了……我不知道什么可能是错误的,以及产生这种应用程序行为的原因。如果有人可以帮助我,我将非常感激。

这里是负责处理我的消息的类

SMSReceiver类

public class SMSReceiver extends BroadcastReceiver {

    public static final String SMS_TEXT = "SMS_TEXT";
    public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == SMS_RECEIVED) {
            Bundle pdusBundle = intent.getExtras();
            Object[] pdus = (Object[]) pdusBundle.get("pdus");
            SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[0]);

            Intent broadcastSMS = new Intent();
            broadcastSMS.setAction("SMS_RECEIVED_ACTION");
            broadcastSMS.putExtra(SMS_TEXT, message.getMessageBody());
            context.sendBroadcast(broadcastSMS);
        }
    }
}

收到的片段

public class ReceivedFragment extends Fragment {


    public static ReceivedFragment newInstance() {
        return new ReceivedFragment();
    }

    private SMSReceiver broadcastIntent = new SMSReceiver();
    private SMSReceiver messageIntent;
    private TextView tvReceived;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View receivedView = inflater.inflate(R.layout.fragment_received, container, false);
        tvReceived = receivedView.findViewById(R.id.tv_received);

        messageIntent = new SMSReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                tvReceived.setText(intent.getStringExtra(SMSReceiver.SMS_TEXT));
            }
        };
        return receivedView;
    }

    @Override
    public void onStart() {
        super.onStart();

        IntentFilter telephonyIF = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        IntentFilter actionIF = new IntentFilter("SMS_RECEIVED_ACTION");

        getActivity().registerReceiver(broadcastIntent, telephonyIF);
        getActivity().registerReceiver(messageIntent, actionIF);
    }

    @Override
    public void onStop() {
        super.onStop();
        getActivity().unregisterReceiver(broadcastIntent);
        getActivity().unregisterReceiver(messageIntent);
    }
}


和Android清单文件:


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

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

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".AppService"
            android:enabled="true"
            android:exported="true" />

        <receiver
            android:name=".receivers.SMSReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter android:priority="100">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

如果有人指出这个应用程序在做什么错,我将不胜感激。预先谢谢你

0 个答案:

没有答案
相关问题