Android broadcast receiver not working

和自甴很熟 提交于 2019-11-29 01:52:53
Jett Hsieh

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"));

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.

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.

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!