不使用Android中的SmsManager接收短信

时间:2015-02-22 17:50:42

标签: android

我有接收短信的广播接收器类

package package name;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;

import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction().equals(ACTION)){ 
            Bundle bundle = intent.getExtras();
            if (bundle != null){

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


                   // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, 
                                 "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();

                } // end for loop
              } // bundle is null


    }    
        }
}

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="data.mobile.balance"
    android:versionCode="3"
    android:versionName="1.03" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="PACKAGE NAME.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

       <receiver android:name=".IncomingSms"> 
        <intent-filter android:priority="1000">
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="PACKAGE NAME.android.action.broadcast"/> 
             <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter> 
    </receiver>

    </application>

</manifest>

我花了一整天的时间来运行这段代码,但仍然没有成功。我的代码出了什么问题?

我是否必须在主要活动中注册广播接收器?

2 个答案:

答案 0 :(得分:0)

在5.0.1上测试更新

我做了一个简单的演示&amp;它就像魅力一样。

Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver
        android:name=".SmsReciever"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name=
                "android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

Receiver:

public class SmsReciever extends BroadcastReceiver {
public SmsReciever() {
}

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context,"sms Received",Toast.LENGTH_SHORT).show();

    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String messageReceived = "";
    if (bundle != null)
    {
        //---retrieve the SMS message 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]);
            messageReceived += msgs[i].getMessageBody().toString();
            messageReceived += "\n";
        }
        //---display the new SMS message---
        Toast.makeText(context, messageReceived, Toast.LENGTH_SHORT).show();
        // Get the Sender Phone Number
        String senderPhoneNumber=msgs[0].getOriginatingAddress ();

    }
}

}

答案 1 :(得分:0)

我不确定它是否与您的问题有关,但您是否尝试删除/减少接收器的“优先级”值?根据谷歌文档,它必须是一个大于-1000且小于1000的整数...