簡體   English   中英

onReceive永遠不會被調用

[英]onReceive never getting called

下面是我的Java代碼和我的XML代碼。 有人可以告訴我為什么我的onReceieve方法永遠不會被調用。

Java的:

public class PopUPSMS extends Activity {

    String RECEIVE_SMS = "RECEIVE_SMS";

    private static final String LOG_TAG = "PopUPSMS";

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

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.i(LOG_TAG, "onCreate");

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.i(LOG_TAG, "onReceive");
            }
        }, new IntentFilter(RECEIVE_SMS));
    }
}

XML:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.smith.johnathan.phone"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".PopUPSMS"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-sdk android:minSdkVersion="3" />
</manifest> 
 registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(LOG_TAG, "onReceive");
        }
    }, new IntentFilter(RECEIVE_SMS));

您已使用字符串“RECEIVE_SMS”注冊了廣播接收器。 IntentFilter應該是一種動作形式。 隨你的聲明:

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

您將擁有:

registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.i(LOG_TAG, "onReceive");
            }
        }, new IntentFilter(ACTION));

您可以查看XML聲明的Api演示清單

您不能將接收器用作內部類。 創建一個單獨的。

您是否需要指定要在清單中接收短信?

<application android:icon="@drawable/icon" android:label="@string/app_name">

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

</application>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM