簡體   English   中英

Android:如何聲明靜態廣播接收器

[英]Android : How to Declare static broadcast receiver

我想知道我們如何通過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,因此需要幫助

您應該給android:name標簽指定完全限定的類名。 請在此處參考SDK文檔

暫無
暫無

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

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