2014-02-16 43 views
0

我想知道我們如何通過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> 

關於我的項目(類或接口預期),它有兩個模塊和第二個模塊完全是Asynctask線程運行的主要活動,我想這個廣播接收器爲第二個模塊。
正在使用過Android Studio 0.4.4 所以需要幫助

回答

0

你應該到android:name標籤只能得到一個完全合格的類名。請參閱sdk文檔here

+0

雅全名com.gfun.Andp.YourBroadcastReceiver我也試過,不工作相同的錯誤。我認爲android工作室可能有不同的模式來註冊廣播接收器,我在互聯網上搜索,但所有關於廣播接收器的教程都在eclipse studio中 – user3315024