2012-03-16 71 views
1

我可以使用下面的方式啓動廣播接收器嗎?有人可以讓它運行,但我不能。我不知道爲什麼。數據包管理器無法啓動廣播接收器

PackageManager pm = getApplicationContext().getPackageManager(); 
ComponentName componentName = new ComponentName("brd2.demo", "brd2.demo.BroadcastReceiver"); 
pm.setComponentEnabledSetting(componentName, 
         PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
         PackageManager.DONT_KILL_APP); 

這是我的Android清單:

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

    <uses-sdk android:minSdkVersion="7" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name="brd2.demo.BroadcastReceiverDemo2Activity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <receiver android:name="brd2.demo.CallBroadcastReceiver"> 
      <intent-filter > 
       <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
      </intent-filter> 
      <intent-filter > 
       <action android:name="android.intent.action.PHONE_STATE" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

請幫幫我。

回答

1

如果您正在照顧在接收器中添加適當的操作。

如果您的接收器類是 擴展廣播接收器,那麼系統將小心地打電話給您的廣播接收器。

您不需要手動啓動廣播接收器(直到並且除非您正在創建自定義廣播)。

在你的情況,如果CallBroadcastReceiver正在擴大BroadcastReceiver,如果你有在onReceive方法寫入適當的邏輯,那麼這個類會自動獲得當你做出新的呼叫,或者如果電話鈴響調用。

希望得到這個幫助。