RECEIVE_SMS广播接收器不工作?

时间:2013-01-06 14:42:23

标签: android

我已经看过所有“常见”的错误,但我没有做过(不幸)。

我不能为我的生活找出原因(至少),我没有收到日志消息!

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.liamwli.spyware.usertrack"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="10" />

    <permission
        android:name="com.liamwli.spyware.usertrack.permission.PREFS"
        android:description="@string/permdisc"
        android:label="Prefs"
        android:logo="@drawable/ic_launcher"
        android:protectionLevel="signature" >
    </permission>

    <uses-permission android:name="com.liamwli.spyware.usertrack.permission.PREFS" />

    <permission
        android:name="com.liamwli.spyware.usertrack.permission.CONFIG"
        android:description="@string/permdiscconfig"
        android:label="Config"
        android:logo="@drawable/ic_launcher"
        android:protectionLevel="signature" >
    </permission>

    <uses-permission android:name="com.liamwli.spyware.usertrack.permission.CONFIG" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ProcessSMS"
            android:excludeFromRecents="true"
            android:label="@string/app_name" >
        </activity>
        <activity android:name=".Launcher" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ActivityConfig"
            android:excludeFromRecents="true"
            android:exported="true"
            android:label="@string/app_name"
            android:permission="com.liamwli.spyware.usertrack.permission.CONFIG" >
        </activity>
        <activity
            android:name=".Prefs"
            android:permission="com.liamwli.spyware.usertrack.permission.PREFS" >
            <intent-filter>
                <action android:name="com.liamwli.spyware.usertrack.PREFS" />

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

        <receiver
            android:name=".CodeReceive"
            android:enabled="true"
            android:excludeFromRecents="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SECRET_CODE" />

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

</manifest>

ReceiveSMS.java:

package com.liamwli.spyware.usertrack;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class ReceiveSMS extends BroadcastReceiver {

    SharedPreferences prefs;
    SharedPreferences.Editor edit;

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

        Log.d("User Track", "SMS Received!");

        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        edit = prefs.edit();

        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]);
                if (messages[i].getMessageBody().toString()
                        .equals(prefs.getString("cbp_pass", null))) {

                    Log.d("User Track", "Message Equals Password!");
                    Intent start = new Intent(context, ProcessSMS.class);
                    start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    start.putExtra("Sender",
                            messages[i].getOriginatingAddress());
                    context.startActivity(start);

                }

            }

        }
    }
}

我甚至没有收到'短信收到!'日志消息,告诉我这根本不起作用。

第二个问题是,我怎样才能让短信绕过短信应用程序并转到我的应用程序?

2 个答案:

答案 0 :(得分:3)

您没有android.permission.RECEIVE_SMS权限。添加

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

到您的清单。

答案 1 :(得分:0)

从API级别23开始,这对我有用。我们需要在运行时请求权限。希望这可以帮助任何有类似问题的人。 https://stackoverflow.com/a/35462909/4544780