拦截短信

时间:2013-12-23 10:46:48

标签: android

所以我有一个应用程序,它应该在收到来自特定电话号码的特定消息时拨打电话。 广播接收器适用于Android 4.0.3上的三星Galaxy Tab2,但如果不打电话,Go SMS Pro不会再收到这些消息。 在Samsung Galaxy S2(Android 4.0.3)上运行应用程序时,广播接收器似乎没有拦截所有消息并且Go SMS Pro正常工作。 我曾试图使用“abortBroadcast”和“clearAbortBroadcast”,但它们没有效果。

以下是我的广播接收器的代码:

public class SMSReceiver extends BroadcastReceiver{
/** The Action fired by the Android-System when a SMS was received.
 * We are using the Default Package-Visibility */

private static final String SMS_EXTRA_NAME = "pdus";


@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Message received", Toast.LENGTH_LONG).show();
    StringBuilder sb = new StringBuilder();
    sb.append("");
    if(intent.getAction().equals(Consts.ACTION)) {

        Bundle bundle = intent.getExtras();
        if(bundle != null)
        {
            //Get received SMS array
            Object[] smsExtra = (Object[]) bundle.get(SMS_EXTRA_NAME);

            for ( int i=0; i < smsExtra.length; i++)
            {
                SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
                String body = sms.getMessageBody().toString();
                String address = sms.getOriginatingAddress();
                if(address.equals(Consts.COPCI_NUMBER)) {
                    sb.append(body);
                }
            }
        }
        if(sb.toString().equals(Consts.TRIGGER_MESSAGE))
        {
            Intent dial = new Intent(Intent.ACTION_CALL);
            dial.setData(Uri.parse("tel: " + Consts.POLI_NUMBER));
            dial.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(dial);

        }
        else {
        }

    }
}

}

这是AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.Parcare"
      android:versionCode="1"
      android:versionName="1.0" >

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver android:name="com.example.Parcare2.SMSReceiver" android:priority="100">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

0 个答案:

没有答案