I'm trying to add to my broadcast receiver an if, so I will start a different activity if a call has been answered, to the regular activity I start usually, if the screen has just been turned on.
Now as you can see down here I have the class screenJump starting when the user wakes up phone.
I would like to start a phoneActivity I wrote when the user wakes up phone, but only when a call has been answered.
This is my service now.
public class MyService extends Service {
BroadcastReceiver bd;
public MyService() {
}
class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startupIntent = new Intent(context, ScreenJump.class);
startupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startupIntent);
}
public ScreenReceiver()
{
}
}
#Override
public void onCreate()
{
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
bd = new ScreenReceiver();
registerReceiver(bd, filter);
}
}
Related
I have a broadcast receiver that gets triggered on geofencing events and either "clocks in" or "clocks out" with the server. If my application's "Attendance" activity is already open I would like it to display the clocking status change but I don't want the Broadcast Receiver to start the activity if it's not open - in other words display the change "live" while the activity is open only.
The way I imagine doing this is with the Broadcast Receiver sending an Intent to the activity but name "startActivity()" doesn't sound encouraging unless there are any special flags I can pass to prevent starting an Activity that isn't already open - I can't seem to find any.
The other option would be to constantly poll the value while the activity is open but it doesn't seem optimal so I would only use it if there wasn't another way and I can't think of a reason why it couldn't be possible with Intents.
There are several different ways to accomplish the same task. One is registering a listener like the following example:
MainActivity
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Receiver.setOnReceiveListener(new Receiver.OnReceiveListener() {
public void onReceive(Context Context, Intent intent)
{
//Do something.
}
});
}
#Override
protected void onDestroy()
{
super.onDestroy();
Receiver.setOnReceiveListener(null);
}
}
Receiver
public class Receiver extends BroadcastReceiver
{
private static OnReceiveListener static_listener;
public static abstract interface OnReceiveListener
{
public void onReceive(Context context, Intent intent);
}
public static void setOnReceiveListener(OnReceiveListener listener)
{
static_listener = listener;
}
#Override
public final void onReceive(Context context, Intent intent)
{
if(static_listener != null) {
static_listener.onReceive(context, intent);
}
}
}
Just have your BroadcastReceiver send a broadcast Intent. Your Activity should register a listener from this broadcast Intent and if it gets triggered, it can update the UI.
Here's an example:
Declare a private member variable in your Activity:
private BroadcastReceiver receiver;
In Activity.onCreate(), register the BroadcastReceiver:
IntentFilter filter = new IntentFilter();
filter.addAction("my.package.name.CLOCK_STATUS_CHANGE");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Here you can update the UI ...
}
};
registerReceiver(receiver, filter);
And in onDestroy() you can unregister it (probably not necessary, but cleaner):
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
In your BroadcastReceiver that detects the geofencing event, you should create and send a broadcast Intent:
Intent broadcastIntent = new Intent("my.package.name.CLOCK_STATUS_CHANGE");
sendBroadcast(broadcastIntent);
I'm writing a service that must accept and react on ACTION_BATTERY_LOW broadcast. I'm using next code:
public class MyService extends Service {
...
private final BroadcastReceiver batteryBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "batteryBroadcastReceiver.onReceive()->intent="+intent.toString());
if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW))
Log.d(LOG_TAG, "intent.getAction() == Intent.ACTION_BATTERY_LOW!");
}
};
public void onCreate() {
super.onCreate();
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
registerReceiver(batteryBroadcastReceiver,intentFilter);
}
public void onDestroy() {
super.onDestroy();
unregisterReceiver(batteryBroadcastReceiver);
}
}
When battery charge level goes to low (~15%) Android sends an intent with action ACTION_BATTERY_LOW and then sends it again every 10 seconds which I'm receiving in MyServive. Why does it happen? What can I do or what I'm doing wrong? Tested on real device.
The period to send Intent.ACTION_BATTERY_LOW is up to the OS and the manufacturer. It's informed periodically so you have updated information through time and you can make better decisions.
I don't know what do you want to accomplish but if you are getting the action repeated you can monitor also Intent.ACTION_BATTERY_OKAY and have a flag indicating whether the action for the low battery has been made. That flag changes its value depending on the action the broadcastReceiver receives, e.g.
public class MyService extends Service {
...
private final BroadcastReceiver batteryBroadcastReceiver = new BroadcastReceiver() {
private bool mBatteryLowActionHasBeenMade = false;
#Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "batteryBroadcastReceiver.onReceive()->intent="+intent.toString());
if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW) && !this.mBatteryLowActionHasBeenMade ) {
Log.d(LOG_TAG, "intent.getAction() == Intent.ACTION_BATTERY_LOW!");
this.mBatteryLowActionHasBeenMade = true;
}
if(intent.getAction().equals(Intent.ACTION_BATTERY_OKAY)) {
this.mBatteryLowActionHasBeenMade = false;
}
}
};
public void onCreate() {
super.onCreate();
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
registerReceiver(batteryBroadcastReceiver,intentFilter);
}
public void onDestroy() {
super.onDestroy();
unregisterReceiver(batteryBroadcastReceiver);
}
}
If that doesn't fit your requirements try monitoring the battery level with Intent.ACTION_BATTERY_CHANGED
I have application that use audio device of the phone, I want my activity to be finish when any call come to my device
This is the code
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context mContext, Intent intent) {
if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
// Send local broadcast to set pin code in the dialog
Intent broadcast = new Intent();
broadcast.setAction(ThurayaConstants.INCOMING_CALL);
mContext.sendBroadcast(broadcast);
}
}
}
}
I have added this class to listen to global receiver in the manifest file to detect when any call come to my device.
and in my fragment I register the following receiver
private BroadcastReceiver mIncomingCallReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
getActivity().finish();
}
};
The problem that my activity is not closed , can any one help here ???
i have an Activity and a Sticky Service. The Activity needs to show some values in it´s UserInterface depending on the values of the Sticky Service. So whenever the Service changes his data, the Activity needs to update it´s UserInterface.
But how should the Service notify the Activity to change it´s values??
Please remind that the Activity sometimes isn´t alive, only the Service is Sticky.
Use LocalBroadcasts
in your service class:
public class LocalMessage extends IntentService{
private Intent broadcast;
public static final String BROADCAST = "LocalMessage.BROADCAST";
public LocalMessage(String name) {
super(name);
broadcast = new Intent(name);
}
#Override
protected void onHandleIntent(Intent intent) {
if (broadcast != null) LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast);
}
}
and here is method inside service to broadcast
private void sendLocalMessage(){
(new LocalMessage(LocalMessage.BROADCAST)).onHandleIntent(null);
}
In your activity:
private void registerBroadcastReciever() {
IntentFilter filter = new IntentFilter(YourService.LocalMessage.BROADCAST);
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(receiver, filter);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//doSmth
}
};
in your activity onDestroy() method unregister receiver;
I have an Android application which uses C2DM services (aka push).
I have a separate class which implements the registration process and which receives the data (and extends BroadcastReceiver).
I want to communicate this data to the activity which currently is in the foreground. The activity currently in the foreground may differ depending on user action.
What's the best way to communicate in between the receiver and the current activity?
Thanks.
I solved this problem by sending out a new broadcast from the C2DMReceiver class, which looked something like this.
The C2DMReceiver class:
public class C2DMReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
handleRegistration(context, intent);
} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(context, intent);
}
}
private void handleRegistration(Context context, Intent intent) {
// handle registration
}
private void handleMessage(Context context, Intent intent) {
Intent i = new Intent("push");
i.putExtras(intent);
// context.sendOrderedBroadcast(i, null);
context.sendBroadcast(i);
}
}
Another class I called PushReceiver. This is the class that will extend BroadcastReceiver and receive the broadcast sent by C2DMReceiver.
public class PushReceiver extends BroadcastReceiver {
public PushReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
// do stuff
abortBroadcast();
}
public static class PushFilter extends IntentFilter {
private static final int DEFAULT_PUSH_PRIORITY = 1;
public PushFilter() {
this(DEFAULT_PUSH_PRIORITY);
}
public PushFilter(int priority) {
super("push");
setPriority(priority);
}
}
}
And the activity class, in this case called MyActivity. This should work well if you are using a base activity class that all other activities extend. That way every activity registers the receiver. By doing the register/unregister in onResume/onPause, you should be able to guarantee that only the current activity receives the broadcast. If not, you can send an ordered broadcast from C2DMReceiver and use priority in the PushFilter.
public class MyActivity extends Activity {
private PushReceiver pushReceiver;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// your onCreate method
pushReceiver = new PushReceiver();
}
public void onResume() {
super.onResume();
// your onResume method
registerReceiver(pushReceiver, new PushReceiver.PushFilter());
}
public void onPause() {
super.onPause();
// your onPause method
unregisterReceiver(pushReceiver);
}
}
In my case, I wrote the PushReceiver constructor to take a View and then "did stuff" with the view in the onReceive method. Without knowing more about what your trying to do, I can't elaborate on this, but hopefully this can provide a decent template to work from.