I am trying to get my BroadcastReceiver, created inside of a service, to receive Screen_On and Screen_Off events. It is not working, here is what I am trying right now.
//OnCreate Method for the service
#Override
public void onCreate() {
super.onCreate();
myBroadcast = new MyBroadcastReceiver();
IntentFilter screenOn_Off = new IntentFilter();
screenOn_Off.addAction(Intent.ACTION_SCREEN_OFF);
screenOn_Off.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(myBroadcast, screenOn_Off);
}
//Inner class for the broadcast receiver, also in the service.
public class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Broadcast received", "Broadcast received");
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
startTimers();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
screenLocked();
}
}
};
//Declaration of receiver in manifest.
<receiver
android:name="com.bla.bla.bla$MyBroadcastReceiver"
android:enabled="true">
</receiver>
I purposely did not include the intent filters for the screen_on and off because I read that those must be declared programmatically, including them made not difference.
Related
I am attempting to send a broadcast from an IntentService, I am having no luck.
MainActivity
private void registerBroadcastReceiver() {
Context context = this;
IntentFilter filter = new IntentFilter();
filter.addAction("io.a.spike");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("******* received broadcast *******");
}
};
registerReceiver(receiver, filter);
}
IntentService
#Override
protected void onHandleIntent(Intent intent) {
System.out.println("send broadcast");
Intent notificationIntent = new Intent();
notificationIntent.setClass(this, MainActivity.class);
notificationIntent.setAction("io.a.spike");
sendBroadcast(notificationIntent);
}
I am not getting to the onReceive method within my registerBroadcastReceiver method. Any guidance would be most appreciated.
Thank you.
For getting your onReceive() called, you will have to add these -
add your BroadcastReceiver to into manifest, in this way -
<receiver android:name="YourReceiver"> <intent-filter> <action android:name="YourAction" /> </intent-filter> </receiver>
call registerReceiver() in your activity.
Also, don't forget to call unegisterReceiver() to avoid leaks.
Also, refer Android BroadcastReceiver without intent filters
You can use LocalBroadcastManager
Create instance
LocalBroadcastManager localBroadcastManager =
LocalBroadcastManager.getInstance(this);
Register Receiver
localBroadcastManager.registerReceiver(new YourReceiver(),new
IntentFilter("YourAction"));
Send Broadcast
localBroadcastManager.sendBroadcast(intent);
UnRegister Receiver
localBroadcastManager.unregisterReceiver(mybroadcast);
In my android app i am calling a service from broadcast receiver.But while debugging i noticed that broadcast receiver is not working. And there is nothing in main activity like layout or buttons etc
Broadcast receiver class
public class Myreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i=new Intent(context,MyService.class);
context.startService(i);
}
}
And In manifest
<receiver android:name=".Myreceiver" >
</receiver>
<service android:name=".MyService" />
</application>
You have registered Broadcast Receiver in your activity
Myreceiver myreceiver = new Myreceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver((BroadcastReceiver) myreceiver, intentFilter);
I have a foreground service uploading images and broadcasting status using intent with action "myAction".
My app needs to react on the broadcast, like send a server request after receiving success message or pop up a notification after receiving failure message. I'm following this https://developer.android.com/training/run-background-service/report-status.html
Sending an broadcast Intent doesn't start or resume an Activity. The
BroadcastReceiver for an Activity receives and processes Intent
objects even when your app is in the background, but doesn't force
your app to the foreground. If you want to notify the user about an
event that happened in the background while your app was not visible,
use a Notification. Never start an Activity in response to an incoming
broadcast Intent.
I first try to register my broadcast receiver statically in Manifest. It doesn't work. Then, I follow this instruction, Keep broadcast receiver running after application is closed, to start a service to register my broadcast receiver. Both don't work(no log shows) when I swipe out or close the app.To be specific, I can see "HAHAHAHA" and "HEHEHEHE" logs come out when is in the foreground. It doesn't come out once I swipe it out from app lists or click back to exit the app.
Here is my code. Where do I miss?
In my manifest
<application...>
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="false">
<intent-filter android:priority="0">
<action android:name="myAction"/>
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:exported="false"/>
</application>
MyBroadcastReceive.java
public class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.d("HAHAHAHA", "Broadcast received.");
}
}
MyService.java
public class MyService extends Service {
BroadcastReceiver mReceiver;
// use this as an inner class like here or as a top-level class
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// do something
Log.d("HEHEHEHE", "Broadcast received.");
}
}
#Override
public void onCreate() {
// get an instance of the receiver in your service
IntentFilter filter = new IntentFilter();
filter.addAction("myAction");
mReceiver = new MyReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
Log.d("HEHEHEHE", "onDestroy");
super.onDestroy();
}
}
MainActivity.java
onStart() {
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
Broadcast is called like below:
LocalBroadcastManager.getInstance(service).sendBroadcast(intent);
I first try to register my broadcast receiver statically in Manifest. It doesn't work.
You cannot use manifest-registered receivers with LocalBroadcastManager. Only receivers registered via LocalBroadcastManager.getInstance().registerReceiver() will respond to local broadcasts.
My app needs to react on the broadcast, like send a server request after receiving success message or pop up a notification after receiving failure message.
Get rid of the broadcast and do that work in the service.
I want to implement a minimal screen activity logger app. So, the application should run on the background (no user interaction) and it will log the screen on and off activities. I have started these codes, but it seems that I need to register my ScreenBroadcastReceiver broadcastreceiver. If I do it with the below code in main activity, it works. However, I do not want to register it in main because the user cannot launch the activity every time. So, where should i register my BroadcastReceiver so that the application works without user interaction?
oncreate in main activity
//I need to find another place to put these code, Where ???
//IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
//filter.addAction(Intent.ACTION_SCREEN_OFF);
//BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
//registerReceiver(screenOnReceiver, filter);
This is ScreenBroadcastReceiver, it will be triggered when the screen is on.
public class ScreenBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.w("OnReceive", "SCREEN IS ON");
}
}
}
This is the BootReceiver to run the program on the background itself.
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, ScreenListenerService.class);
context.startService(service);
//IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
//filter.addAction(Intent.ACTION_SCREEN_OFF);
//BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
//context.registerReceiver(screenOnReceiver, filter);
}
}
This is the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".ScreenListenerService"></service>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Update:
I added a service, but it does not work. Did i forget to add something? or what ?
public class ScreenListenerService extends Service {
public void OnCreate(){
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
registerReceiver(screenOnReceiver, filter);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
use Service for your purpose and do the same as you did in the activity.
I want to broadcast intent with custom-data and only the receiver that have this custom data should receive this intent, how this could be done ?
this how i broadcast the intent :
Intent intent = new Intent();
intent.setAction("com.example");
context.sendBroadcast(intent);
and i define the broadcast receiver as following :
<receiver android:name="com.test.myReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.example"></action>
</intent-filter>
</receiver>
Setup your myReceiver class basically how anmustangs posted.
public class myReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent intent) {
//Do something
}
}
You shouldn't have to check the Intent action because you've already filtered for it based on what you've included in your manifest, at least in this simple case. You could have other actions and filters in which case you'd need to have some kind of check on the received intent.
If you want the filter to filter on data, the sdk documentation on intent filters covers that. I'm not great with data types so any example I would give would be relatively poor. In any event, here is a link to the manifest intent filter page:
http://developer.android.com/guide/topics/manifest/intent-filter-element.html
And the specific page for the data element;
http://developer.android.com/guide/topics/manifest/data-element.html
If you already define your broadcast receiver class, then the next step is to register it on the Activity/Service which you'd like to receive the broadcast intent. An example to register:
IntentFilter iFilter = new IntentFilter();
iFilter.addAction("com.example"); //add your custom intent to register
//below is your class which extends BroadcastReceiver, contains action
//which will be triggered when current class received the broadcast intent
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver, iFilter); //register intent & receiver
If you destroy the activity/service, for best practice don't forget to unregister the broadcast receiver on the same activity/service:
#Override
protected void onDestroy() {
if(myReceiver!=null)
unregisterReceiver(myReceiver)
super.onDestroy();
}
MyReceiver class:
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent intent) {
if(intent.getAction().equals("com.example")){
//DO SOMETHING HERE
}
}
}