Android:如何声明静态广播接收器

时间:2014-02-16 03:12:01

标签: java android

我想知道如何通过AndroidManifest.xml注册静态广播接收器

我的班级BroadcastReceiver类

public class YourBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle == null)
            return;
        String phoneNumber = null;

        // Incoming call
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);
        if ((state != null)&&(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {
            phoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            // Here: do something with the number
        }
        // Outgoing call
        else if (state == null) {
            phoneNumber = bundle.getString(Intent.EXTRA_PHONE_NUMBER);
            // Here: do something with the number
        }
    }
}

我的AnrdoidMainfest.xml文件,我在该行<receiver android:name=".YourBroadcastReceiver">

中收到错误(预期的类或接口)
<?xml version="1.0" encoding="utf-8"?>
<!--suppress ALL -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gfun.Andp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gfun.Andp.main"
            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=".YourBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

关于我的项目,它有两个模块,第二个模块完全是由mainactivity运行的Asynctask线程,我希望这个广播接收器用于第二个模块。
我正在使用android studio 0.4.4 所以需要帮助

1 个答案:

答案 0 :(得分:0)

您应该为android:name标记提供完全限定的类名。请参阅sdk docs here