0

我的应用中有一个 BroadcastReceiver:

public class SMSReceiver extends BroadcastReceiver { 

   public void onCreate(){
        Log.i("APP","SMS Receiver started.");
   }

   @Override 
       public void onReceive(Context context, Intent intent) { 
           Log.i("APP", "SMS received.");
       }
   }

和接收器:

<receiver class="SMSReceiver" android:name=".SMSReceiver" >
    <intent-filter>
        <action android:name="smsreceiver" android:value="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

和:

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

我开始:

Intent SMS = new Intent(this, SMSReceiver.class);
sendBroadcast(SMS);

在主要活动中。

如何阅读邮件内容?谢谢。

4

1 回答 1

1

在你的 mainfest 中声明:

<receiver android:name=".SMSReceiver"> 
        <intent-filter android:priority="100"> 
            <action android:name=
                "android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter> 
</receiver>

当您在清单中声明它时,无需从意图启动您的接收器,当传入消息到达时会自动调用它。

这是广播接收器的代码:

将此代码放入您的 onRecieve 方法中

 if (!intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) 
                       return;          
                        bundle = intent.getExtras();

            if (bundle != null) {
                // ---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                // Set<String> Bundle.keySet();
                msgs = new SmsMessage[pdus.length];
                // DeleteSMSFromInbox(context, msgs);
                for (int i = 0; i < msgs.length; i++) {

                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    ClsIncomingsmsNo = msgs[i].getOriginatingAddress();
                    System.out.println("ClsIncomingsmsNo" + ClsIncomingsmsNo);
                    msg = msgs[i].getMessageBody().toString();

}

}

于 2012-05-01T13:48:13.527 回答