【问题标题】:Fetching a specific SMS on Android在 Android 上获取特定短信
【发布时间】:2015-03-23 09:46:07
【问题描述】:

我正在尝试做一个应用程序,如果它在其中找到任何想要的标头,它将丢弃 SMS 消息。

我已经尝试在整个网络上查看 KitKat 更改后的操作方法,发现我必须成为默认应用才能首先获取 SMS。

完成后,我希望能够将短信传递给所有其他可以收听它的应用程序(具体来说,使用 Galaxy 我希望短信能够到达消息应用程序)。

无论我多么努力,我都无法做到这一点。

即使我不发出“abortBroadcast()”,消息也不会传递给其他应用程序。

我试图自己发出一条广播消息,以使手机“相信”它收到了一条短信。再次,没有运气。 (可能是我做错了)

代码如下:

public class IncomingSms extends BroadcastReceiver {

final SmsManager sms = SmsManager.getDefault();

Object[] myPdusObj;
String minStr;
Intent myInt;

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

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            myPdusObj = pdusObj;
            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();

                Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);

                if (parseMessage(message) == true) {
                    this.abortBroadcast();
                    // Save the message/other action
                }
                else {
                     // Do nothing, let the message pass to others
                }
            } 
        } 

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);

    }
}

我的清单文件(已编辑以显示为默认短信应用程序):

<application>
    <!-- BroadcastReceiver that listens for incoming SMS messages -->
    <receiver android:name=".IncomingSms"
        android:permission="android.permission.BROADCAST_SMS"
        >
        <intent-filter
            android:priority="1">
            <action android:name="android.provider.Telephony.SMS_DELIVER" />
        </intent-filter>
    </receiver>

    <!-- BroadcastReceiver that listens for incoming MMS messages -->
    <receiver android:name=".IncomingMms"
        android:permission="android.permission.BROADCAST_WAP_PUSH">
        <intent-filter
            android:priority="1">
            <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
            <data android:mimeType="application/vnd.wap.mms-message" />
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter
            android:priority="1">
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.LAUNCHER" />
            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>
    </activity>


    <!-- Service that delivers messages from the phone "quick response" -->
    <service android:name=".HeadlessSmsSendService"
        android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>
    </service>
</application>



</manifest>

希望它可以完成,这只是我在这里遗漏的一个小问题...... 谢谢!

【问题讨论】:

    标签: android sms broadcast


    【解决方案1】:

    “完成后,我希望能够将短信传递给所有其他可以收听它的应用程序(”

    您必须是系统应用才能执行此操作。查看 AOSP com/android/internal/telephony/InboundSmsHandler.java 以了解 Android 是如何做到的。

    【讨论】:

    • 您好,感谢您的回答,我已经到了一个步骤,我收到错误消息“您必须是系统应用程序才能执行此操作”,但意识到我不能成为系统应用程序时走到了死胡同.有什么办法可以绕过它,或者解决我的问题的另一种方法?谢谢。
    • 如果您想在 Play 商店中分发您的应用,则不要这样做。
    • 我不想在 Play 商店中使用它,它仅供我使用。但从我所见,我需要一个有根设备才能使应用程序成为系统应用程序,这就是为什么我试图找到另一种方法来做到这一点。
    • 哦,如果只是为了你自己,你应该考虑自己构建操作系统。如果需要,您可以使用 AOSP 或 Cyanogen。 AOSP 是使用默认密钥构建的,您可以使用该密钥对应用程序进行签名,也可以将源代码包含在构建中。使用默认密钥重新签署您的应用程序会更快。然后你把它贴在手机的/system目录下就可以了。
    • 感谢您的回答大卫,我试图找到一种解决方法,以免仅仅为了我想要实现的 SMS 操作而构建我的操作系统。希望有另一种解决方案这个。
    【解决方案2】:

    因此,总而言之,可以实现阻止其他应用程序接收广播的可能性:

    1. 作为系统应用程序 - 大多数开发人员不可能
    2. 让用户选择你作为默认应用,这样可以防止广播到达其他应用,但要求你的应用处理“正常”消息(或只是传递它们......)

    【讨论】:

    • “这样可以防止广播到达其他应用程序” - SMS_DELIVER 广播仅发送到默认短信应用程序。没有其他应用程序会收到它,因此abortBroadcast() 调用毫无意义。 “但要求您的应用程序处理‘正常’消息(或者只是传递它们......)” - 默认 SMS 应用程序负责将所有传入消息写入 Provider。它不能“传递它们”。 SMS_RECEIVED 动作仍然是广播的,任何监听它的应用程序都会得到它,因为它不能再被中止,但是除了默认的应用程序之外没有任何应用程序对提供者有写访问权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多