4

在清单中指定 BroadcastReceiver 时,我没有收到以下系统操作:

public class BRMemoryHandler extends BroadcastReceiver 
{
    public BRMemoryHandler()
    {
        Log.d("BRMH", "ctor");
    }

    @Override
    public void onReceive(Context arg0, Intent arg1) 
    {
        Log.d("BRMH", "onReceive()");
    }
}

<application
    android:name="Test"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar" >

    <receiver android:name=".BRMemoryHandler" >
        <intent-filter>
            <action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
            <action android:name="android.intent.action.ACTION_DEVICE_STORAGE_OK" />
        </intent-filter>
    </receiver>

但是,如果我在我的主要活动中以编程方式创建相同的广播接收器,我能够接收此操作:

void onCreate(Bundle b)
{
    super.onCreate(Bundle b);

    BRMemoryHandler tbr = new BRMemoryHandler();

    IntentFilter intF = new IntentFilter();
    intF.addAction(Intent.ACTION_HEADSET_PLUG);
    intF.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);

    registerReceiver(tbr, intF);
4

0 回答 0