如何阻止Android的传入消息? [英] How to block an incoming message in android?

查看:118
本文介绍了如何阻止Android的传入消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发机器人)也是一个应用程序,它阻止传入sms..i设置优先级,但它不是阻止传入sms..i已经使用this.abortBroadcast(但没有result..help我出去球员

 进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.telephony.gsm.SmsMessage;

进口android.widget.Toast;

@燮pressWarnings(德precation)
公共类SmsReceiver扩展的BroadcastReceiver
{

@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
this.abortBroadcast();

    捆绑包= intent.getExtras();
    SmsMessage []封邮件= NULL;
    字符串str =;

    如果(捆绑!= NULL)
    {


        [对象]的PDU =(对象[])bundle.get(的PDU);
        封邮件=新SmsMessage [pdus.length]
        的for(int i = 0; I< msgs.length;我++){
            封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
            STR + =短信来自+封邮件[I] .getOriginatingAddress();
            STR + =;
            STR + =封邮件[I] .getMessageBody()的toString()。
            STR + =\ N的;

}


}
}
}
 

和清单文件是这样的

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  包=BVB.EDU
  安卓版code =1
  机器人:VERSIONNAME =1.0>
<使用-SDK安卓的minSdkVersion =7/>

<应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
    <活动机器人:短信NAME =
              机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;

< /用途>

<接收机器人:SMSReceiverNAME =>
     <意图过滤器的Andr​​oid版本:优先=99999>
         <作用机器人:名称=android.provider.Telephony.SMS_RECEIVED/>
     &所述; /意图滤光器>
 < /接收器>

<使用-权限的Andr​​oid:名称=android.permission.SEND_SMS>
< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS>
< /使用-许可>

< /舱单>
 

解决方案

添加 abortBroadcast(); 如果(!包= NULL) {} 块。应该阻止它去其他应用程序。而且我注意到,你的广播接收器的名称是 SmsReceiver ,但在清单,你给它。SMSReceiver(案例敏感)。

I am trying to develop an app in android which blocks an incoming sms..i have set the priority but its not blocking the incoming sms..i have used this.abortBroadcast() also but no result..help me out guys

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
{

@Override
public 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";        

}


}
}
}

and the manifest file is like this

<?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>

解决方案

Add abortBroadcast(); in the if(bundle!=null){} block. that should stop it going to other apps. And I noticed that your Broadcast Receiver's name is SmsReceiver, but in Manifest, you gave it ".SMSReceiver" (case sensitive).

这篇关于如何阻止Android的传入消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆