
我正在尝试在android中开发一个阻止传入短信的应用程序.我已经设置了优先级,但它没有阻止传入的短信.我也使用过this.abortbroadcast(),但没有结果.
import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;import androID.os.Bundle;import androID.telephony.gsm.SmsMessage;import androID.Widget.Toast;@SuppressWarnings("deprecation")public class SmsReceiver extends broadcastReceiver{@OverrIDepublic voID onReceive(Context context, Intent intent) {this.abortbroadcast(); Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = ""; if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; for (int i=0; i<msgs.length; i++){ msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); str += "SMS from " + msgs[i].getoriginatingAddress(); str += " :"; str += msgs[i].getMessageBody().toString(); str += "\n"; }}}}并且清单文件是这样的
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="BVB.EDU" androID:versionCode="1" androID:versionname="1.0"><uses-sdk androID:minSdkVersion="7" /><application androID:icon="@drawable/icon" androID:label="@string/app_name"> <activity androID:name=".SMS" androID:label="@string/app_name"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity></application><receiver androID:name=".SMSReceiver"> <intent-filter androID:priority="99999"> <action androID:name="androID.provIDer.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver><uses-permission androID:name="androID.permission.SEND_SMS"></uses-permission><uses-permission androID:name="androID.permission.RECEIVE_SMS"></uses-permission></manifest>解决方法:
您的清单中的问题是关闭< application>在您的接收器之前标记,但不应该这样做,您必须在接收器后关闭您的应用程序标签.
您的类名是SmsReceiver,在您声明为SMSReceiver的清单中,
所以你不会为你的接收器获得广播.
使用必须在清单中更改您的类名,如下所示
<receiver androID:name=".SmsReceiver"> <intent-filter androID:priority="99999"> <action androID:name="androID.provIDer.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver></application>在你的接收器中,最好检查意图对象是否有消息然后你可以中止它.
但要小心它会中止所有消息.如果你想根据一些特定的字符串中止消息,你可以对消息进行一些 *** 作,然后你可以中止它.
如果要中止所有消息,可以在对该消息进行任何 *** 作之前直接调用abortbroadcast().
Bundle extras = intent.getExtras(); if ( extras != null ) { // do you manipulation on String then if you can abort. if(somecondition){ abortbroadcast(); } }这是我的清单工作正常,一旦与您的代码比较
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.mypackage" androID:versionCode="1" androID:versionname="1.0"> <uses-sdk androID:minSdkVersion="8" /> <uses-permission androID:name="androID.permission.RECEIVE_SMS"/> <uses-permission androID:name="androID.permission.WRITE_SMS"/> <uses-permission androID:name="androID.permission.READ_SMS"/> <application androID:icon="@drawable/icon" androID:enabled="true" androID:label="@string/app_name"> <service androID:name="com.mypackage.service.MyService" androID:exported="true"> </service> <receiver androID:name="com.mypackage.receiver.MyReceiver"> <intent-filter androID:priority="100"><action androID:name="androID.provIDer.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application></manifest>和java代码
public voID onReceive( Context context, Intent intent ) { if(intent != null){ String action = intent.getAction(); if(action.equals("androID.provIDer.Telephony.SMS_RECEIVED")) { Bundle extras = intent.getExtras(); if ( extras != null ){ //read sms } } } } 总结 以上是内存溢出为你收集整理的如何在android中阻止传入的消息?全部内容,希望文章能够帮你解决如何在android中阻止传入的消息?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)