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;
}
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
add a comment |
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
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
add a comment |
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
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
java
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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 registerBroadcastReceiveranywhere 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
|
show 4 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 registerBroadcastReceiveranywhere 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
|
show 4 more comments
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.
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 registerBroadcastReceiveranywhere 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
|
show 4 more comments
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.
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.
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 registerBroadcastReceiveranywhere 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
|
show 4 more comments
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 registerBroadcastReceiveranywhere 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
|
show 4 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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