我正在尝试做一个应用程序,如果它在其中发现任何想要的 header ,它将丢弃 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);
}
}
还有我的 list 文件(经过编辑以显示为默认短信应用程序):
<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>
希望它可以完成,这只是我在这里遗漏的一个小问题...... 谢谢!
最佳答案
“完成后,我希望能够将短信传递给所有其他可以收听的应用程序(”
您必须是系统应用程序才能执行此操作。查看 AOSP com/android/internal/telephony/InboundSmsHandler.java,了解 Android 是如何做到的。
关于android - 在 Android 上获取特定的短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29207117/