访问android中的短信电话号码

时间:2013-03-23 06:34:37

标签: android android-intent android-broadcast

我是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>

鉴于获得sms的使用许可..

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

但是当一个sms没有来自MyBroadcastReciever类的响应时。当我调试控件时,不会进入MyBroadcastReciever类。 PLZ帮我找出问题

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

 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();         
        }
    } 
 }
}
}

}

5 个答案:

答案 0 :(得分:2)

在MyBroadcastReciever中扩展BroadcastReceiver,在onReceive()里面试试这段代码

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

                }
            }

答案 1 :(得分:1)

我认为问题在于注册/取消注册BroadcastReceiver并删除了清单声明。

ReceiveMessages定义为Activity中需要侦听来自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...,如我的示例所示。这有助于避免其他应用程序或系统组件可能尝试处理它的情况。

答案 2 :(得分: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>

试试这个......

<强> EDITED

     <receiver android:name="Your PackageName.ClassName" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

答案 3 :(得分:0)

要接收定义的BroadcastReceiver的意图,您可以在AndroidManifest.xml中对接收器进行delcare,如下所示:

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

或使用Context.registerReceiver注册它:

Context.registerReceiver(receiver, intentFilter);

否则,系统无法知道您的接收器是否存在。对于某些特殊的敏感意图(如SMS),您还需要在AndroidManifest.xml中使用delcare权限。

答案 4 :(得分:0)

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

你必须双向注册。

静态和动态

您可以看到更多信息link

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
           }

尝试这个。