不为PHONE_STATE调用BroadcastReceiver.onReceive

Ale*_*x F 2 android broadcastreceiver

遵循本教程:http://www.vogella.de/articles/AndroidServices/article.html#receiver我创建了自己的项目.表现:

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

    <uses-sdk android:minSdkVersion="13" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <receiver android:name="MyPhoneReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"></action>
            </intent-filter>
        </receiver>

        <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

码:

public class MyPhoneReceiver extends BroadcastReceiver {
final String logTag = "BroadcastReceiverSample";

@Override
public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
        String state = extras.getString(TelephonyManager.EXTRA_STATE);

        Log.i(logTag, "Call state: " + state);

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.i(logTag, "Phone number: " + phoneNumber);
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

在Android模拟器上运行此示例,我看到它已成功安装.但是,永远不会调用onReceive函数.我使用以下方式拨打来电:

telnet localhost 5554
gsm call 12345678
Run Code Online (Sandbox Code Playgroud)

模拟器显示来电,但未调用onReceive.

MBy*_*ByD 6

不应该是:

<receiver android:name=".MyPhoneReceiver">
                        ^ note the dot
Run Code Online (Sandbox Code Playgroud)

此外,权限的位置是错误的,它应该是<manifest>不是的孩子<Application>.