2014-02-14 74 views
0

我來這裏,因爲我真的不明白爲什麼我沒有得到BroadcastReceiver 我有一個名爲NotifService的服務創建通知。 我創建了一個接收器下面這段代碼:無法獲得BroadcastReceiver

public class MyReceiver extends BroadcastReceiver { 

Intent myIntent; 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.d("2erlog","2erlog>>>>>>>>>>>>>>>>>>>>>>>>"); 
    myIntent = new Intent(context, NotifService.class); 
    context.startService(myIntent); 
    Log.d("3erlog","3erlog>>>>>>>>>>>>>>>>>>>>>>>>"); 
    } 
} 

在這裏你可以看到我的清單:

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

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="18" /> 

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <service android:name="NotifService"/> 

    <activity 
     android:name="com.example.notifplugunplug.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <!-- Declaring broadcast receiver for BOOT_COMPLETED event --> 
    <receiver android:name=".MyReceiver" android:enabled="true"  android:exported="false"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 
    </receiver> 



    </application> 
</manifest> 

我知道,服務永遠不啓動,因爲我從來沒有看到日誌,我創建,我不看到通知從服務中出現。

謝謝你們!

+3

你有沒有運行該應用程序?即啓動了MainActivity? – NickT

+0

是的主要活動是用來啓動服務來測試,如果問題是我的廣播接收器或其他地方。活動開始良好(但我把評論的活動,以便從broadcastReceiver做到這一點)! – Eydolol

+1

你的應用程序安裝在哪裏?內部存儲器或SD卡?如果是SD卡,則在安裝SD卡之前將發送'BOOT_COMPLETED'action,並且您的應用程序不會處於活動狀態。 – Squonk

回答

2

「android.intent.action.BOOT_COMPLETED」是全局廣播。
由於您已將您的BroadcastReceiver定義爲android:exported =「false」,因此它只會獲得本地廣播。
將其更改爲android:exported =「true」。

1

放置在不同的包 你的包com.example.notifplugunplug 您的廣播reciever然後右鍵點擊它,並在其下新建一個包 com.example.notifplugunplug.myreciever 然後把你的廣播reciever其下

+0

它不會改變任何東西:x – Eydolol