如何在android中接收新的传入消息?

时间:2014-06-04 13:27:27

标签: java android

  • 我经常尝试和搜索并实施但是我没有得到结果。一旦新的SMS消息到来,我试图通过使用广播接收器在吐司上显示。但是,不给出结果。

  • 我正在尝试使用此代码,

  • BroadcastReceiver类

    public class MainActivity extends BroadcastReceiver{
    @SuppressLint("NewApi")
    @Override
    
        public void onReceive(Context context, Intent intent) {
            Bundle myBundle = intent.getExtras();
            SmsMessage [] messages = null;
            String strMessage = "";
            if (myBundle != null)
            {
                Object [] pdus = (Object[]) myBundle.get("pdus");
                messages = new SmsMessage[pdus.length];
                for (int i = 0; i < messages.length; i++)
                {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    strMessage += "SMS From: " + messages[i].getOriginatingAddress();
                    strMessage += " : ";
                    strMessage += messages[i].getMessageBody();
                    strMessage += "\n";
                }
                Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
            }
        }
    }
    
  • 的manifest.xml

     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.websoftexmessagetracker"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <receiver android:name=".MainActivity" >
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
        </application>
    </manifest>
    

请帮我解决这个问题。我没有得到再次需要的东西,以便它可以工作.. 提前致谢.. 请帮助我...

1 个答案:

答案 0 :(得分:0)

您是否将此代码添加到清单?

注意:在清单文件中添加BroadcastReceiver -

<receiver android:name=".listener.SmsListener">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

添加此权限:

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

更新:

将您的代码更改为:

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    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]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                    }
                }catch(Exception e){
//                            Log.d("Exception caught",e.getMessage());
                }
            }
        }
    }

reference