如何让Android应用程序读取短信,如果其中包含字符串,则显示祝酒词

如何让Android应用程序读取短信,如果其中包含字符串,则显示祝酒词,android,sms,Android,Sms,你好,我正在尝试制作一个应用程序,它将始终监听包含文本的短信,如果它只是给出一个视觉指示器,我希望侦听器一直在后台运行这是我所有的代码 package me.andrew.findmyphone; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.tele

你好,我正在尝试制作一个应用程序,它将始终监听包含文本的短信,如果它只是给出一个视觉指示器,我希望侦听器一直在后台运行这是我所有的代码

package me.andrew.findmyphone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

/**
 * Created by Andrew on 15/12/2015.
 */
public class SMSReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context,Intent intent){
        Bundle bundle = intent.getExtras();

        if(bundle != null){
            Object[] pdus = (Object[]) bundle.get("pdu");

            for(int i = 0; i < pdus.length; i++){
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[i]);
                String message = sms.getMessageBody();
                String status = "";
                if(message.equalsIgnoreCase("!andrew")){
                    status = "true";
                }else{
                    status = "false";
                }
                Toast.makeText(context,status+" the msg is a trigger msg",Toast.LENGTH_LONG).show();
            }
        }
    }

}
package me.andrew.findmyphone;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.sms消息;
导入android.widget.Toast;
/**
*由Andrew于2015年12月15日创建。
*/
公共类SMSReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
Bundle=intent.getExtras();
if(bundle!=null){
Object[]pdus=(Object[])bundle.get(“pdu”);
对于(int i=0;i
目前我想做的是,如果消息中包含“!andrew”来显示通知,则该类是主ActivityClass中的SMSReceiver类,它只是默认代码,android清单如下所示

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
        </activity>
        <receiver android:name=".SMSReceiver">
            <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
        </receiver>
        <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>


如果您在您的
中创建了一个
,并为
“android.provider.Telephony.SMS_RECEIVED”
创建了一个
,则每当接收到传入的SMS时,接收器就会启动。你不需要任何东西在后台持续运行。您还可以删除
标记中的两个
元素。您只需要外部的一个。中的问题在清单中有一个示例。@MikeM.Thank现在将尝试此操作。是否有任何方法可以启动设备上的接收器启动?我不知道您为什么要这样做。您的接收器用于处理传入的短信,只要您正确地实现了所有功能,并且用户在安装后至少运行了一次应用程序,系统就会在收到短信时启动接收器。在启动时运行该接收器实际上不会起任何作用,因为它不会附加SMS。如果确实希望在引导时接收广播,则需要对
BOOT\u COMPLETED
操作进行接收器筛选,并且需要
receive\u BOOT\u COMPLETED
权限。@MikeM。好的,谢谢,迈克,我当时的印象是,应用程序必须运行才能处理这些问题。下一个问题是,当它收到短信时,如果你在你的
中创建一个
,并为
“android.provider.Telephony.SMS_RECEIVED”
创建一个
,接收器将在收到短信时启动。你不需要任何东西在后台持续运行。您还可以删除
标记中的两个
元素。您只需要外部的一个。中的问题在清单中有一个示例。@MikeM.Thank现在将尝试此操作。是否有任何方法可以启动设备上的接收器启动?我不知道您为什么要这样做。您的接收器用于处理传入的短信,只要您正确地实现了所有功能,并且用户在安装后至少运行了一次应用程序,系统就会在收到短信时启动接收器。在启动时运行该接收器实际上不会起任何作用,因为它不会附加SMS。如果确实希望在引导时接收广播,则需要对
BOOT\u COMPLETED
操作进行接收器筛选,并且需要
receive\u BOOT\u COMPLETED
权限。@MikeM。好的,谢谢,迈克,我当时的印象是,应用程序必须运行才能处理它们。下一个问题是,当它收到短信时,它会崩溃