在Android上获取特定短信
我正在尝试做一个应用程序,将丢弃短信,如果它发现任何想要的标题在它在Android上获取特定短信,android,sms,broadcast,Android,Sms,Broadcast,我正在尝试做一个应用程序,将丢弃短信,如果它发现任何想要的标题在它 <application> <!-- BroadcastReceiver that listens for incoming SMS messages --> <receiver android:name=".IncomingSms" android:permission="android.permission.BROADCAST_SMS" >
<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>
我试着在KitKat改变后在网络上寻找如何做到这一点,并发现我必须是一个默认的应用程序才能首先获得短信
<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>
完成后,我希望能够将短信传递给所有其他可以收听短信的应用程序(具体来说,使用Galaxy,我希望短信能够进入短信应用程序)
<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>
不管我怎么努力,我都无法做到这一点
<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>
即使我没有发出“abortBroadcast()”,消息也不会传递给其他应用程序
<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>
我试着自己发一条广播信息,让手机“相信”它收到了短信。再说一次,运气不好。(也许我做错了)
<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>
代码如下:
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>
public class IncomingSms扩展广播接收器{
final smsmsmanager sms=smsmsmanager.getDefault();
对象[]myPdusObj;
弦乐团;
意图密特;
@凌驾
公共void onReceive(上下文、意图){
final Bundle=intent.getExtras();
myInt=意图;
试一试{
if(bundle!=null){
最终对象[]pdusObj=(对象[])bundle.get(“pdus”);
myPdusObj=pdusObj;
对于(int i=0;i
和我的清单文件(已编辑以显示为默认sms应用程序):
<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>
希望它可以做到,这只是一个小问题,我在这里错过。。。
谢谢!“完成后,我希望能够将短信传递给所有其他可以收听短信的应用程序(”
<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是如何做到的。因此,可以通过以下方式实现阻止其他应用程序接收广播的可能性:
<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>
您好,谢谢您的回答,我已经达到了一个步骤,我得到错误说:“您必须是系统应用程序来做这件事”,但最终意识到我不能成为一个系统应用程序。有没有办法绕过它,或者用其他方法解决我的问题?谢谢。如果你想在Play Store上发布你的应用程序,就不要。我不想在Play Store中使用它,它只供我使用。但从我所看到的情况来看,我需要一个根设备,才能使应用程序成为一个系统应用程序,这就是我为什么这么做的原因试图找到另一种方法。哦,如果这只是为了你自己,你应该考虑自己构建操作系统。如果你愿意,你可以使用AOSP或Cyanogen。AOSP是使用默认密钥构建的,你可以使用默认密钥对你的应用程序进行签名,也可以将你的源代码包含在构建中。使用默认密钥对你的应用程序进行重新签名会更快。然后是y如果你把它放在手机的/system目录中,它就会起作用。谢谢你的回答大卫,我正试图找到一个解决办法,以避免仅仅为了我想要实现的短信操作而构建我的操作系统。希望有另一个解决方案。“通过这种方式,可以防止广播到达其他应用程序。”
SMS\u DELIVER
广播仅发送到默认的SMS应用程序。其他应用程序将永远不会接收到它,因此abortBroadcast()
调用是无意义的。”但要求你的应用程序处理“正常”消息(或只传递它们…)“-默认SMS应用程序负责将所有传入消息写入提供商。它无法“传递消息”。收到的SMS_
操作仍在广播中,任何侦听该操作的应用程序都将获得该操作,因为该操作无法再中止,但除默认应用程序外,没有其他应用程序具有对提供商的写入权限。