首页 > 解决方案 > Android SDK解析短信

问题描述

我需要解析短信。我找到了一个库https://github.com/adorsys/sms-parser-android。这很舒服,因为我可以分割消息并显示电话号码。但是,它不起作用。我写 BEGIN /* message */ END 并且应用程序告诉我“No Number”和“No message”。我从模拟器向模拟器发送消息。Android 向我显示号码 (555) 521-5556。我试着写“5215556”、“521-5556”、“5555215556”、“5215556”。该应用程序不起作用。我能做些什么?我在 Gradle 中设置了库。我不碰Manifest,示例中的作者如何。

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.example.kostya.testsms">

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

        <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>

        </application>

    </manifest>

    public class MainActivity extends AppCompatActivity {
    @NonNull
    private TextView smsSenderTextView;
    @NonNull
    private TextView smsMessageTextView;
    @NonNull
    private LocalBroadcastManager localBroadcastManager;

    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("AAA", "test");
            if (intent.getAction().equals(SmsReceiver.INTENT_ACTION_SMS)) {

                String receivedSender = intent.getStringExtra(SmsReceiver.KEY_SMS_SENDER);
                String receivedMessage = intent.getStringExtra(SmsReceiver.KEY_SMS_MESSAGE);
                smsSenderTextView.setText(getString(R.string.text_sms_sender_number,
                        receivedSender != null ? receivedSender : "NO NUMBER"));
                smsMessageTextView.setText(getString(R.string.text_sms_message,
                        receivedMessage != null ? receivedMessage : "NO MESSAGE"));
            }
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        // If request is cancelled, the result arrays are empty.
        if (requestCode == SmsTool.REQUEST_CODE_ASK_PERMISSIONS
                && (grantResults.length <= 0
                || grantResults[0] != PackageManager.PERMISSION_GRANTED)) {
            Toast.makeText(this, getString(R.string.warning_permission_not_granted),
                    Toast.LENGTH_LONG).show();
            startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                    Uri.parse("package:" + getApplicationContext().getPackageName())));
        }
    }

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

        SmsConfig.INSTANCE.initializeSmsConfig(
                "BEGIN",
                "END",
                "5555215556");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SmsTool.requestSMSPermission(this);
        }

        initViews();
    }

    @Override
    protected void onPause() {
        unRegisterReceiver();
        super.onPause();
    }

    @Override
    protected void onResume() {
        registerReceiver();
        super.onResume();
    }

    private void initViews() {
        smsSenderTextView = findViewById(R.id.sms_sender_text_view);
        smsMessageTextView = findViewById(R.id.sms_message_text_view);

        smsSenderTextView.setText(getString(R.string.text_sms_sender_number, ""));
        smsMessageTextView.setText(getString(R.string.text_sms_message, ""));
    }

    private void registerReceiver() {
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(SmsReceiver.INTENT_ACTION_SMS);
        localBroadcastManager.registerReceiver(broadcastReceiver, intentFilter);
    }

    private void unRegisterReceiver() {
        localBroadcastManager.unregisterReceiver(broadcastReceiver);
    }
}

它是屏幕模拟器... 在此处输入图像描述

我给了许可...

标签: androidparsingsms

解决方案


我是该库的作者之一:我使用了标准示例,交换了开始和结束关键字以匹配您使用的关键字

SmsConfig.INSTANCE.initializeSmsConfig(
            "BEGIN",
            "END",
            "5555215556");

我使用了你发送的相同信息 在此处输入图像描述

它有效: 在此处输入图像描述

您是否正确更新了开始和结束关键字?您的模拟器中的电话号码和您指定的电话号码是否匹配?


推荐阅读