0

In AndroidManifest.xml I have this:

<receiver android:name=".MyBroadcast"  android:exported="true"/>

My broadcast file:

package com.myapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class MyBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intent1 = new Intent(context, Radio.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent1);
    }
}

I am trying to run application after close it to play music in background.

Skyterix
  • 107
  • 1
  • 9

3 Answers3

3

That's because you never specify what intent you're actually listening for.

You need to register the broadcast receiver to listen for specific broadcasts (events), either in the manifest using the intent-filter tag or dynamically at runtime. See this question for more discussion about the difference.

Here's an example of how to do this in the manifest (from the linked question):

<receiver android:name="TestReceiver">
        <intent-filter>
            <action android:name="android.media.AUDIO_BECOMING_NOISY"/>
        </intent-filter>
    </receiver>

This means that the broadcast receiver is listening for the AUDIO_BECOMING_NOISY intent. (You'll want to replace this with a more appropriate intent that reflects when you want this to run).

There's a very useful list of Intents that you can listen for here. You can select a broadcast from there (or from one of the libraries) or, if you're listening for an event that occurs within your application, you can raise the broadcast yourself.

Also, make sure that the event in question is actually being raised. If the broadcast you're listening for never happens, the broadcast receiver will never actually be triggered.

For related reading, see the Observer Pattern (which is the design pattern that Android Broadcast Receivers implement).

Community
  • 1
  • 1
1

onReceive() method is only called when the event you have registered for occurs. You have not declared that event that will trigger the onReceive() method. So, the Broadcast Receiver doesn't know what it should listen for.

You should read more about the Broadcast Receivers and Activity Lifecycle methods from Android Docs.

This is similar to asking person X(anyone) to get ________ from the market for you. He is in the market looking for ________ but he does not know what it is. So, obviously, he can't get it for you. You need to tell the receiver what to look for.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ray an
  • 1,132
  • 3
  • 17
  • 42
  • I'm trying to make a broadcast that will launch Radio.class after the application closes. (I'm new to java) – Skyterix Apr 19 '17 at 21:24
  • `onReceive()` method is only called when the event you have registered for occurs. You have not declared that event that will trigger the `onReceive()` method. So, the Broadcast Receiver doesn't know what it should listen for. – ray an Apr 19 '17 at 21:58
  • @rayan You should edit your answer with your most recent comment (or delete the original and post the comment as the answer) - your comment *is*, in fact, an answer to the question. (In fact, it's the correct answer). – EJoshuaS - Stand with Ukraine Apr 19 '17 at 22:01
  • And what do u mean by "*after the application closes*". Does it mean when your application process dies or just when the application is in the background. – ray an Apr 19 '17 at 22:04
1

onReceive() method is only called when the event you have registered for occurs. You have not declared that event that will trigger the onReceive() method. So, the Broadcast Receiver doesn't know what it should listen for.

You should read more about the Broadcast Receivers and Activity Lifecycle methods from Android Docs.

I don't think you need to use Broadcast Receivers. You can use Activity lifecycle methods to do whatever you want when your application closes.

ray an
  • 1,132
  • 3
  • 17
  • 42