0

I was working with broadcast receiver and background services. So, When one my activity goes onPause(), I start my service and service sends a broadcast. Now, my broadcast receiver is in the same class as my service. I am receiving my service call, but I am unable to receive the broadcast info. Here is the code I have been working on.. [EDITED]

private String notifier = "ninja.ibtehaz.thenewproject.Activities.activityNew"; 

@Override
protected void onHandleIntent(Intent intent) {

    boolean flag = ProjectApp.getInstance().isActivityVisible();
    Log.e("rainbow", "onHandleIntent");
    if (!flag) {
        //start emergency activity
        Log.e("rainbow", "starting activity");
        Intent broadcastIntent = new Intent(notifier);
        sendBroadcast(broadcastIntent);
    }
}

[EDITED] on activityNew class >

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("rainbow", "In Method: Service started");
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    registerReceiver(receiver, filter);
}

private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e("rainbow", "In Method: onReceive");

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.e("rainbow","Screen went OFF");
        }

        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.e("rainbow","Screen went ON");
        }


    }
};

and the manifest file : [EDITED]

<service android:name=".Activities.ServiceBackground">
</service> 
<receiver
    android:name=".Activities.ActivityEmergency"
    android:theme="@android:style/Theme.NoDisplay">
</receiver>

here is my logcat info :

E/rainbow: onHandleIntent
E/rainbow: starting activity

I am not getting anything after that..

I know this might not be the best practice, I just started working with these things. Thank you for your help. Cheers!

O_o
  • 1,103
  • 11
  • 36
  • try to replace Intent broadcastIntent = new Intent(notifier); with I ntent broadcastIntent = new Intent(activityNew.class); – D_Alpha Nov 09 '16 at 19:30
  • @DeepanshuHarbola, it did not work. It asks for two argument... – O_o Nov 09 '16 at 19:32
  • Intent broadcastIntent = new Intent(this,activityNew.class); – D_Alpha Nov 09 '16 at 20:01
  • An `IntentService` stops itself after `onHandleIntent()` finishes. `Context#sendBroadcast()` is asynchronous, and relatively slow, so your `IntentService` will most likely be dead by the time the broadcast could be delivered. Furthermore, you've not registered the Receiver for the action you're broadcasting. That is, you've not added the `notifier` action to the `IntentFilter`. Also, it's unclear why you're trying to have a component broadcast to itself, anyway. Just call a local method directly. – Mike M. Nov 09 '16 at 23:27

3 Answers3

1

You are registering a BroadcastReceiver with an IntentFilter which only contains Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON, but no ninja.ibtehaz.thenewproject.Activities.activityNew. It means this BroadcastReceiver can only be invoked when Intent.ACTION_SCREEN_OFF or Intent.ACTION_SCREEN_OFF is broadcast.

Now pay attention to your sendBroadcast code:

Intent broadcastIntent = new Intent(notifier);
sendBroadcast(broadcastIntent);

You are broadcasting an action "ninja.ibtehaz.thenewproject.Activities.activityNew", which can't match the IntentFilter bound with the BroadcastReceiver you have registered.

Try to use these codes:

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(notifier);
registerReceiver(receiver, filter);
huangjh
  • 46
  • 4
1

Broadcast Limitations : With limited exceptions, apps cannot use their manifest to register for implicit broadcasts. They can still register for these broadcasts at runtime, and they can use the manifest to register for explicit broadcasts targeted specifically at their app.

Android documentation: https://developer.android.com/about/versions/oreo/background

Try registering like below dynamically in Application Activity/Service instead of Manifest.

BroadcastReceiver receiver = new NotificationReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_UPDATE_NOW);
context.registerReceiver(receiver, filter);
  • I stopped working with Android development in 2016. However, thank you for your contribution. – O_o May 25 '19 at 05:09
0

You are registering your receiver inside the processing an intent method ;-) This would not work, definitely.

You must register your receiver earlier, ex. in onCreate() method.

Andrey Kopeyko
  • 1,556
  • 15
  • 14
  • I did changed my code. But I am still getting the same result. I have updated the question with new codes. Can you please take a look? @AndreyKopeyko... – O_o Nov 09 '16 at 19:03
  • It seems you forgot to subscribe to ACTION_SCREEN_ON intent via the Manifest. Take a look on this question and answer - http://stackoverflow.com/questions/16161062/broadcastreceiver-for-screen-on-off-not-working – Andrey Kopeyko Nov 09 '16 at 19:25