1

我已經在清單中註冊了一個接收器。android BroadcastReceiver:Receiver的onReceive()方法沒有被調用

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.receiverdemo.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> 

     <receiver 
      android:name="Receiver" 
      android:exported="false" > 
      <intent-filter> 
       <action android:name="com.example.receiverdemo.RECEIVER" /> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 

,我從亞行殼爲

adb shell am broadcast -a com.example.receiverdemo.RECEIVER

上面的命令應該得到正確執行發送廣播和ReceiveronReceive()方法應後調用。

這是預期的行爲。

我可以看到emulatorgalaxy nexus這種行爲都運行的是Android 4.2,但是這是不按預期工作對我HTC one XHTC-desire-x都運行的是Android 4.1.1,該onReceive()方法不獲取調用,即使am broadcast命令是獲得正確執行。

我可以看到輸出

[email protected]:/ $ am broadcast -a com.example.receiverdemo.RECEIVER 
am broadcast -a com.example.receiverdemo.RECEIVER 
Broadcasting: Intent { act=com.example.receiverdemo.RECEIVER } 
Broadcast completed: result=0 

Receiver等級如下表所示,

public class Receiver extends BroadcastReceiver { 

    public static final String ACTION_RECEIVER = "com.example.receiverdemo.RECEIVER"; 

    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     Log.i("DEMO", "Working"); 
    } 
} 

我沒有得到什麼錯誤在這裏。它與API版本或ROM或某些開發人員設置或其他內容有關嗎?

回答

0

我植根了我的設備,並從具有root權限的adb shell執行該命令,並且按預期工作。

0

您也可以嘗試對此接收器發表評論android:permission="com.google.android.c2dm.permission.SEND"

相關問題