Broadcast receiver for nought and Oreo + devices not working





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















public class ScreenReceiver extends BroadcastReceiver {

private boolean screenOff;

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}

}


<receiver
android:name=".ScreenReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.DREAMING_STARTED" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android." />
</intent-filter>
</receiver>



Not getting any callback on naught and oreo devices,tried on marshmallow devices its working fine .but on oreo devices its not working and also for battery connected and network change receiver not working .











share|improve this question

























  • Starting from Nougat, background optimizations have been made. Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.

    – chetna
    Sep 17 '18 at 9:25











  • I know about the behaviour changes ,but I need it to get all the action callbacks . is there anyway to do this .

    – Danial clarc
    Sep 17 '18 at 9:51








  • 1





    after searching i find this one is correct approach for this github.com/devggaurav/…

    – Danial clarc
    Sep 18 '18 at 6:47


















1















public class ScreenReceiver extends BroadcastReceiver {

private boolean screenOff;

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}

}


<receiver
android:name=".ScreenReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.DREAMING_STARTED" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android." />
</intent-filter>
</receiver>



Not getting any callback on naught and oreo devices,tried on marshmallow devices its working fine .but on oreo devices its not working and also for battery connected and network change receiver not working .











share|improve this question

























  • Starting from Nougat, background optimizations have been made. Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.

    – chetna
    Sep 17 '18 at 9:25











  • I know about the behaviour changes ,but I need it to get all the action callbacks . is there anyway to do this .

    – Danial clarc
    Sep 17 '18 at 9:51








  • 1





    after searching i find this one is correct approach for this github.com/devggaurav/…

    – Danial clarc
    Sep 18 '18 at 6:47














1












1








1


0






public class ScreenReceiver extends BroadcastReceiver {

private boolean screenOff;

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}

}


<receiver
android:name=".ScreenReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.DREAMING_STARTED" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android." />
</intent-filter>
</receiver>



Not getting any callback on naught and oreo devices,tried on marshmallow devices its working fine .but on oreo devices its not working and also for battery connected and network change receiver not working .











share|improve this question
















public class ScreenReceiver extends BroadcastReceiver {

private boolean screenOff;

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}

}


<receiver
android:name=".ScreenReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.DREAMING_STARTED" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android." />
</intent-filter>
</receiver>



Not getting any callback on naught and oreo devices,tried on marshmallow devices its working fine .but on oreo devices its not working and also for battery connected and network change receiver not working .








java android broadcastreceiver android-8.0-oreo android-7.0-nougat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 15:36







Danial clarc

















asked Sep 17 '18 at 9:16









Danial clarcDanial clarc

256




256













  • Starting from Nougat, background optimizations have been made. Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.

    – chetna
    Sep 17 '18 at 9:25











  • I know about the behaviour changes ,but I need it to get all the action callbacks . is there anyway to do this .

    – Danial clarc
    Sep 17 '18 at 9:51








  • 1





    after searching i find this one is correct approach for this github.com/devggaurav/…

    – Danial clarc
    Sep 18 '18 at 6:47



















  • Starting from Nougat, background optimizations have been made. Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.

    – chetna
    Sep 17 '18 at 9:25











  • I know about the behaviour changes ,but I need it to get all the action callbacks . is there anyway to do this .

    – Danial clarc
    Sep 17 '18 at 9:51








  • 1





    after searching i find this one is correct approach for this github.com/devggaurav/…

    – Danial clarc
    Sep 18 '18 at 6:47

















Starting from Nougat, background optimizations have been made. Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.

– chetna
Sep 17 '18 at 9:25





Starting from Nougat, background optimizations have been made. Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.

– chetna
Sep 17 '18 at 9:25













I know about the behaviour changes ,but I need it to get all the action callbacks . is there anyway to do this .

– Danial clarc
Sep 17 '18 at 9:51







I know about the behaviour changes ,but I need it to get all the action callbacks . is there anyway to do this .

– Danial clarc
Sep 17 '18 at 9:51






1




1





after searching i find this one is correct approach for this github.com/devggaurav/…

– Danial clarc
Sep 18 '18 at 6:47





after searching i find this one is correct approach for this github.com/devggaurav/…

– Danial clarc
Sep 18 '18 at 6:47












1 Answer
1






active

oldest

votes


















2














You can not register broadcast receiver in manifest.xml from Oreo.
You can see
Android 8.0 Behavior Changes




Apps cannot use their manifests to register for most implicit
broadcasts (that is, broadcasts that are not targeted specifically at
the app).




Solution



Register your receiver in your related Activity instead. Like this.



public class MainActivity extends AppCompatActivity {
BroadcastReceiver receiver;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction("android.intent.action.LOCKED_BOOT_COMPLETED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// todo
}
};
registerReceiver(receiver, filter);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (receiver != null)
unregisterReceiver(receiver);
}
}


You can add action as string same as manifest, if you don't find relevant constant string.






share|improve this answer


























  • I know but what's the alternative ??

    – Danial clarc
    Sep 17 '18 at 9:50











  • See edited answer.

    – Khemraj
    Sep 17 '18 at 9:57











  • It will be activity specific ?

    – Danial clarc
    Sep 17 '18 at 10:00











  • Where do you want to register it? You can register BroadcastReceiver anywhere context is available, like Services, Fragments and Dialogs etc.

    – Khemraj
    Sep 17 '18 at 10:02








  • 1





    I tried posting it here .but people deleted it .lol they are asking to put all the code here .

    – Danial clarc
    Sep 24 '18 at 11:24












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52364398%2fbroadcast-receiver-for-nought-and-oreo-devices-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can not register broadcast receiver in manifest.xml from Oreo.
You can see
Android 8.0 Behavior Changes




Apps cannot use their manifests to register for most implicit
broadcasts (that is, broadcasts that are not targeted specifically at
the app).




Solution



Register your receiver in your related Activity instead. Like this.



public class MainActivity extends AppCompatActivity {
BroadcastReceiver receiver;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction("android.intent.action.LOCKED_BOOT_COMPLETED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// todo
}
};
registerReceiver(receiver, filter);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (receiver != null)
unregisterReceiver(receiver);
}
}


You can add action as string same as manifest, if you don't find relevant constant string.






share|improve this answer


























  • I know but what's the alternative ??

    – Danial clarc
    Sep 17 '18 at 9:50











  • See edited answer.

    – Khemraj
    Sep 17 '18 at 9:57











  • It will be activity specific ?

    – Danial clarc
    Sep 17 '18 at 10:00











  • Where do you want to register it? You can register BroadcastReceiver anywhere context is available, like Services, Fragments and Dialogs etc.

    – Khemraj
    Sep 17 '18 at 10:02








  • 1





    I tried posting it here .but people deleted it .lol they are asking to put all the code here .

    – Danial clarc
    Sep 24 '18 at 11:24
















2














You can not register broadcast receiver in manifest.xml from Oreo.
You can see
Android 8.0 Behavior Changes




Apps cannot use their manifests to register for most implicit
broadcasts (that is, broadcasts that are not targeted specifically at
the app).




Solution



Register your receiver in your related Activity instead. Like this.



public class MainActivity extends AppCompatActivity {
BroadcastReceiver receiver;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction("android.intent.action.LOCKED_BOOT_COMPLETED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// todo
}
};
registerReceiver(receiver, filter);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (receiver != null)
unregisterReceiver(receiver);
}
}


You can add action as string same as manifest, if you don't find relevant constant string.






share|improve this answer


























  • I know but what's the alternative ??

    – Danial clarc
    Sep 17 '18 at 9:50











  • See edited answer.

    – Khemraj
    Sep 17 '18 at 9:57











  • It will be activity specific ?

    – Danial clarc
    Sep 17 '18 at 10:00











  • Where do you want to register it? You can register BroadcastReceiver anywhere context is available, like Services, Fragments and Dialogs etc.

    – Khemraj
    Sep 17 '18 at 10:02








  • 1





    I tried posting it here .but people deleted it .lol they are asking to put all the code here .

    – Danial clarc
    Sep 24 '18 at 11:24














2












2








2







You can not register broadcast receiver in manifest.xml from Oreo.
You can see
Android 8.0 Behavior Changes




Apps cannot use their manifests to register for most implicit
broadcasts (that is, broadcasts that are not targeted specifically at
the app).




Solution



Register your receiver in your related Activity instead. Like this.



public class MainActivity extends AppCompatActivity {
BroadcastReceiver receiver;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction("android.intent.action.LOCKED_BOOT_COMPLETED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// todo
}
};
registerReceiver(receiver, filter);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (receiver != null)
unregisterReceiver(receiver);
}
}


You can add action as string same as manifest, if you don't find relevant constant string.






share|improve this answer















You can not register broadcast receiver in manifest.xml from Oreo.
You can see
Android 8.0 Behavior Changes




Apps cannot use their manifests to register for most implicit
broadcasts (that is, broadcasts that are not targeted specifically at
the app).




Solution



Register your receiver in your related Activity instead. Like this.



public class MainActivity extends AppCompatActivity {
BroadcastReceiver receiver;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction("android.intent.action.LOCKED_BOOT_COMPLETED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// todo
}
};
registerReceiver(receiver, filter);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (receiver != null)
unregisterReceiver(receiver);
}
}


You can add action as string same as manifest, if you don't find relevant constant string.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 17 '18 at 9:57

























answered Sep 17 '18 at 9:46









KhemrajKhemraj

17k66690




17k66690













  • I know but what's the alternative ??

    – Danial clarc
    Sep 17 '18 at 9:50











  • See edited answer.

    – Khemraj
    Sep 17 '18 at 9:57











  • It will be activity specific ?

    – Danial clarc
    Sep 17 '18 at 10:00











  • Where do you want to register it? You can register BroadcastReceiver anywhere context is available, like Services, Fragments and Dialogs etc.

    – Khemraj
    Sep 17 '18 at 10:02








  • 1





    I tried posting it here .but people deleted it .lol they are asking to put all the code here .

    – Danial clarc
    Sep 24 '18 at 11:24



















  • I know but what's the alternative ??

    – Danial clarc
    Sep 17 '18 at 9:50











  • See edited answer.

    – Khemraj
    Sep 17 '18 at 9:57











  • It will be activity specific ?

    – Danial clarc
    Sep 17 '18 at 10:00











  • Where do you want to register it? You can register BroadcastReceiver anywhere context is available, like Services, Fragments and Dialogs etc.

    – Khemraj
    Sep 17 '18 at 10:02








  • 1





    I tried posting it here .but people deleted it .lol they are asking to put all the code here .

    – Danial clarc
    Sep 24 '18 at 11:24

















I know but what's the alternative ??

– Danial clarc
Sep 17 '18 at 9:50





I know but what's the alternative ??

– Danial clarc
Sep 17 '18 at 9:50













See edited answer.

– Khemraj
Sep 17 '18 at 9:57





See edited answer.

– Khemraj
Sep 17 '18 at 9:57













It will be activity specific ?

– Danial clarc
Sep 17 '18 at 10:00





It will be activity specific ?

– Danial clarc
Sep 17 '18 at 10:00













Where do you want to register it? You can register BroadcastReceiver anywhere context is available, like Services, Fragments and Dialogs etc.

– Khemraj
Sep 17 '18 at 10:02







Where do you want to register it? You can register BroadcastReceiver anywhere context is available, like Services, Fragments and Dialogs etc.

– Khemraj
Sep 17 '18 at 10:02






1




1





I tried posting it here .but people deleted it .lol they are asking to put all the code here .

– Danial clarc
Sep 24 '18 at 11:24





I tried posting it here .but people deleted it .lol they are asking to put all the code here .

– Danial clarc
Sep 24 '18 at 11:24




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52364398%2fbroadcast-receiver-for-nought-and-oreo-devices-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out