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!
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);
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);
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.
Related
Normally for a single IntentService you can define the broadcast receiver listener like this in an Activity's onCreate() method (for example)
broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//get stuff from the intent and do whatever you want
}
};
And you register the receiver like this (also in onCreate()):
LocalBroadcastManager.getInstance(this)
.registerReceiver(broadcastReceiver, new IntentFilter("my_intent_service"));
And then you start the IntentService with:
Intent intent = new Intent(this, MyIntentService.class);
startService(intent);
And in the IntentService you send messages back to the receiver with:
Intent broadcastIntent = new Intent("my_intent_service");
broadcastIntent.putExtra("whateverData", whateverData);
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
which triggers the onReceive method described earlier.
And then to unregister the receivers, in onDestroy method you can do:
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
In the Manifest file, you add for your service:
<service
android:name=".MyIntentService"
android:exported="false" />
I want to receive broadcasts from multiple IntentServices in the same Activity.
Do you need one receiver per IntentService? As in, if I make n IntentServices do I need to register n receivers, make n listener, and unregister n receivers in onDestroy?
The way to filter multiple actions with one BroadcastReceiver is to add them to the IntentFilter:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("my_intent_service1"); // Action1 to filter
intentFilter.addAction("my_intent_service2"); // Action2 to filter
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
And in your BroadcastReceiver:
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("my_intent_service1")) {
// Action 1
} else if (intent.getAction().equals("my_intent_service2")) {
// Action 2
}
}
};
yes you can use one broadcast receiver in one activity, that would handle multiple intents from different services.
I’d recommend to add multiple intent-filter to your broadcast receiver to make distinction from which Service you are getting a broadcast.
I'm registering many IntentFilter to a single BroadcastReceiverand unregistering all the IntentFilter when these are not required as in the code below.
Thing are working well at this point. But now I need to unregister a single IntentFilter instead of all.
How this can be achieved or any workaround or better approach?
private void registerBroadcastReceiver() {
Log.i(TAG, LogHelper.at() + "Begin");
LocalBroadcastManager
.getInstance(getActivity())
.registerReceiver(broadcastReceiver,
new IntentFilter(getString(R.string.onInitiateCustomerVerificationSUCCESS)));
LocalBroadcastManager
.getInstance(getActivity())
.registerReceiver(broadcastReceiver,
new IntentFilter(getString(R.string.onInitiateCustomerVerificationFAILURE)));
Log.i(TAG, LogHelper.at() + "End");
}
private void unRegisterBroadcastReceiver() {
Log.i(TAG, LogHelper.at() + "Begin");
LocalBroadcastManager
.getInstance(OkeKeyApplication.getAppContext())
.unregisterReceiver(broadcastReceiver);
Log.i(TAG, LogHelper.at() + "End");
}
Define the IntentFilter by adding actions. Then you can register the receiver only once.
IntentFilter filter = new IntentFilter();
filter.addAction(getString(R.string.onInitiateCustomerVerificationSUCCESS));
filter.addAction(getString(R.string.onInitiateCustomerVerificationFAILURE));
LocalBroadcastManager.getInstance(getActivity())
.registerReceiver(broadcastReceiver, filter);
To change the IntentFilter, I would just unregister it, and register it again with a new IntentFilter.
There is no such method but you can follow one of the following ways -
Register a different receiver for that particular IntentFilter and unregister it whenever you want . There will be no effect on other registered receivers.
Unregister the receiver and then at the same time register the same receiver again but only for the rest of the IntentFilters.
There are some question about Android4.2 Broadcast,
android.intent.action.TIME_TICK
android.intent.action.PACKAGE_INSTALL
This two protected broadcast are define in /frameworks/base/core/res/AndroidManifest.xml
Only find where register Receiver use Context.registerRecriver(),
Question:Where to send this Broadcast attached android.intent.action.TIME_TICK
Thanks
If you declare in manifest file it wont be enough.Manifest file is like a blue print.You need to declare the broadcast receiver there and after that u need to register the broadcast receiver in your code here i am attaching sample broadcast receiver code.
batteryLevelFilter= new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
this.registerReceiver(batteryLevelReceiver, batteryLevelFilter);
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//yourcode
}
};
You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().
And it only be sent by the system.
You have to register this intent programmatically : Sent every minute.
like this
IntentFilter if = new IntentFilter(Intent.ACTION_TIME_TICK);
registerReceiver(YourReceiver, if);
I've a Broadcast receiver:
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//Do something
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Intent start=new Intent(context,MainActivity.class);
context.startActivity(start);
}
}
}
And, in my activity, into onCreate():
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
ScreenReceiver mReceiver=new ScreenReceiver();
registerReceiver(mReceiver, filter);
The problem is that, when my activity is displayed, the receiver performs correctly the action, but when it is in background, sometimes nothing happens.
What could be the issue?
Most likely, when your app goes into the background Android kills it to free up resources. Try starting a foreground service attached to an ongoing notification from your Activity, and register the BroadcastReceiver in that.
I've got this app, in which users update certain variables in an Activity, and this Activity passes the new variables to a IntentService using a BroadcastReceiver. However, the BroadcastReceiver in the IntentService doesn't seem to be receiving the broadcasts. This is the code within the service to create the broadcast receiver
protected class UpdateReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
Log.d("receiver", "Got message: ");
//state=3;
//Toast.makeText(context, "got it", Toast.LENGTH_SHORT).show();
}
};
And here's the code to register the receiver, in the onHandle() function
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("updates");
UpdateReceiver lol = new UpdateReceiver();
DetectService.this.registerReceiver(lol, intentFilter);
Finally here's the code to send the broadcast, from the activity
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("updates");
HomePageActivity.this.sendBroadcast(broadcastIntent);
Log.d("sender", "send msg");
When I put the receiver in the same activity as the broadcasting part, it works, but not when I put it into the IntentService. Help please!
On another related note, I've tried using LocalBroadcastManager in this project since the broadcasts are all local but eclipse doesn't seem to be able to import the compatibility class. I've installed it using Android SDK manager already. Is there any thing I'm doing wrong here?
this Activity passes the new variables to a IntentService using a BroadcastReceiver.
That makes no sense. Use startService() to send a command to an IntentService. And an IntentService should not have a BroadcastReceiver, because the IntentService will be destroyed as soon as onHandleIntent() completes and therefore will never receive the broadcast.
I've tried using LocalBroadcastManager in this project since the broadcasts are all local but eclipse doesn't seem to be able to import the compatibility class.
:: shrug ::
Here is a sample project with Eclipse project files that uses LocalBroadcastManager. I encountered no particular Eclipse issues when creating the project.
protected class UpdateReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
Log.d("receiver", "Got message: ");
//state=3;
//Toast.makeText(context, "got it", Toast.LENGTH_SHORT).show();
}
};
In the onCreate() method or where relevant.
mReceiver = new UpdateReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("<your receivers intent goes here>");
this.registerReceiver(mReceiver, filter);
Now you should be able to send a broadcast and it be picked up.
Intent intent = new Intent("<your receivers intent goes here>");
// Add what you want to add to the intent right here.
<context-handle>.sendBroadcast(intent);