Android broadcast receiver not working

前端 未结 4 846
庸人自扰
庸人自扰 2020-12-16 00:55

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


&l         


        
相关标签:
4条回答
  • 2020-12-16 01:39

    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>
    
    0 讨论(0)
  • 2020-12-16 01:45

    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.

    0 讨论(0)
  • 2020-12-16 01:52

    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"));
    
    0 讨论(0)
  • 2020-12-16 01:55

    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.

    0 讨论(0)
提交回复
热议问题