Android broadcast receiver not working

2019-01-24 06:38发布

I try to get a broadcast receiver working. Should be as simple as possible, I have my manifest like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mytest.intentRec" android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name"
        android:debuggable="true">
        <activity android:name=".mainAct" 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="com.mytest.intentRec.MyIntentRec"
                  android:enabled="true" >
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="7" />
</manifest> 

As you can see I have a main activity mainAct, this does nothing but sending the broadcast once started:

public class mainAct extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.sendBroadcast(new Intent());
    }
}

and I have a class MyIntentRec, which is as simple as it could:

public class MyIntentRec extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("IntentRec", "got it");
    }
}

What I expect is that when I start my app that a broadcast is sent and being picked up and that a log entry is written. I don't see that log entry and I don't see any error. I'm suspecting to have either an error in the manifest or in sending the broadcast. I just created an empty intent there, does it need to be some intent with certain properties?

4条回答
甜甜的少女心
2楼-- · 2019-01-24 07:11

Try specifying what actions your receiver should catch in the manifest. You can do this as such:

<receiver android:name="com.mytest.intentRec.MyIntentRec">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>
查看更多
等我变得足够好
3楼-- · 2019-01-24 07:15

Please setClass for your Intent,

EX:

public class mainAct extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent i=new Intent("any string");
        i.setClass(this, MyIntentRec.class);
        this.sendBroadcast(i);
    }
}

That is what it means " The absence of any filters means that it can be invoked only by Intent objects that specify its exact class name."

[Old answer]
You should register what kind of actions you need in the manifest.
Ex:

    <receiver android:name="com.mytest.intentRec.MyIntentRec" android:enabled="true" >
        <intent-filter>
            <action  android:name="your.intent" />
        </intent-filter>
    </receiver>

send it,

this.sendBroadcast(new Intent("your.intent"));
查看更多
Lonely孤独者°
4楼-- · 2019-01-24 07:16

You didn't define any Intent Filters in the manifest for your BroadcastReceiver. Specify one for a custom Action type. You also have to define this custom Action type in the Intent you brodcast upon startup.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-24 07:21

it is insufficient to make just new Intent();. You have to specify it with some action. Also, you have to specify in your manifest the intent filter for this particular action. Please read more here and here.

查看更多
登录 后发表回答