2

我是 android 的新手,我试图在收到新消息时获取短信的编号,我为此使用广播接收器。但是当收到一条新消息时,我的“广播接收器”课程无法正常工作。谁能帮我找出问题所在。在下面给出我的代码..

public class MainActivity extends Activity
{
     @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_layout);
    }


     public class MyBroadcastReciever extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String smsnumber = "";         
        if (bundle != null)
        {
            //---retrieve the sender number SMS received---
            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]);                
                smsnumber =  msgs[i].getOriginatingAddress();         
            }
        } 
    }
}

}

我已经在清单中给出了接收标签。

 <receiver android:name="com.aucupa.iack.MyBroadcastReciever" >
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

也获得了接收短信的使用权限..

<uses-permission android:name="android.permission.RECEIVE_SMS" />

但是当一条短信没有来自 MyBroadcastReciever 类的响应时。当我调试控件时,MyBroadcastReciever 类没有响应。请帮我找出问题所在

最后我从你的帮助中得到了答案,在这里给出我的答案..

 public class MainActivity extends Activity
{

  MyBroadcastReciever broadcastreciever = new MyBroadcastReciever();
  static final String SOME_ACTION = "android.provider.Telephony.SMS_RECEIVED";
  IntentFilter intentFilter = new IntentFilter(SOME_ACTION);

  Intent i = new Intent(SOME_ACTION);
 @Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);

sendBroadcast(i);
context.registerReceiver(broadcastreciever, intentFilter);
}


 public class MyBroadcastReciever extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent) 
{
   if (intent.getAction() == "android.provider.Telephony.SMS_RECEIVED"){
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String smsnumber = "";         
    if (bundle != null)
    {
        //---retrieve the sender number SMS received---
        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]);                
            smsnumber =  msgs[i].getOriginatingAddress();         
        }
    } 
 }
}
}

}
4

5 回答 5

2

在扩展 BroadcastReceiver 的 MyBroadcastReciever 中,在 onReceive() 中尝试此代码

 if (intent.getAction() == android.provider.Telephony.SMS_RECEIVED) {
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                       // your rest of code

                }
            }
于 2013-03-25T04:56:30.527 回答
1

我认为问题在于注册/注销BroadcastReceiver以及删除清单声明。

定义ReceiveMessagesActivity需要监听来自Service.

然后,声明类变量,例如...

 ReceiveMessages myReceiver = null;
 Boolean myReceiverIsRegistered = false;

onCreate()使用中myReceiver = new ReceiveMessages();

然后在onResume()...

if (!myReceiverIsRegistered) {
    registerReceiver(myRecever, new IntentFilter("com.mycompany.myapp.SOME_MESSAGE"));
    myReceiverIsRegistered = true;
}

……在onPause()……

if (myReceiverIsRegistered) {
    unregisterReceiver(myReceiver);
    myReceiverIsRegistered = false;
}

Service创建和广播Intent...

Intent i = new Intent("com.mycompany.myapp.SOME_MESSAGE");
sendBroadcast(i);

就是这样。使“动作”对您的包/应用程序是唯一的,即,com.mycompany...如我的示例所示。这有助于避免其他应用程序或系统组件可能尝试处理它的情况。

于 2013-03-25T06:50:06.887 回答
0

尝试清理和重建您的代码。有时我们需要在更新代码时进行清理。

您必须通过两种方式注册。

静态和动态

更多信息你可以看到这个链接

或者

Uri uriSMSURI = Uri.parse("content://sms/");
    Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null,null, null);
    cur.moveToFirst();
       id = cur.getString(cur.getColumnIndex("thread_id"));        
       protocol = cur.getString(cur.getColumnIndex("protocol"));
       Long date=cur.getLong(cur.getColumnIndex("date"));
       type = cur.getInt(cur.getColumnIndex("type"));
       String address=cur.getString(cur.getColumnIndex("address"));

       System.out.println("READING LOG...."+"\n thread_id:"+id+"\n protocol:"+protocol+"\n type:"+type+"\n address"+address);
           if(protocol==null && (type==2 || type==4 || type==6)){
             Log.i("======TEST====", "MESSAGE SENT.......number:"+address);
           }else if(protocol!=null && type==1){               
                Log.i("======TEST====", "MESSAGE RECEIVE.......number:"+address);
           }else{
            //something message
           }

试试这个。

于 2013-03-23T07:56:09.437 回答
0

在清单中使用此前提

   <uses-permission android:name="android.permission.RECEIVE_SMS" />
   <uses-permission android:name="android.permission.SEND_SMS" />

     <receiver android:name="net.app.sms.MessageReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

尝试这个...

已编辑

     <receiver android:name="Your PackageName.ClassName" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
于 2013-03-23T06:41:48.643 回答
0

要接收定义的 BroadcastReceiver 的意图,您可以像这样在 AndroidManifest.xml 中删除接收器:

<receiver android:name="full qualified name of your receiver">
    <action android:name="the action interests you"/>
</receiver>

或使用 Context.registerReceiver 注册它:

Context.registerReceiver(receiver, intentFilter);

否则,系统无法知道您的接收器存在。对于一些特殊的敏感意图(如短信),您还需要在 AndroidManifest.xml 中删除权限。

于 2013-03-23T07:04:36.203 回答