0

我尝试了这篇文章中提到的这段代码:http://stackoverflow.com/questions/7089313/android-listen-for-incoming-sms-messages

但似乎问题在于,当我在手机中安装该程序时,它无法正常工作。我希望这个应用程序在后台运行。当我检查我的手机运行进程时,它不存在。

顺便说一句,下面的代码是我有这个应用程序的在线代码。这个对吗?

谢谢

RJ

public class SmsListener extends BroadcastReceiver{

private SharedPreferences preferences;

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

注意:在您的清单文件中添加 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" />
4

0 回答 0