I am doing an android application. In my project I am checking phonestates using broadcast receiver.Using the folowing code I am checking my phonestate.
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bun=intent.getExtras();
if(bun!=null){
String state=bun.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
String phoneno=bun.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "phone no. is"+phoneno, Toast.LENGTH_LONG).show();
}
}
}
Now i want control this application from activity. I mean using an button i have start the broadcast and using an off button i have to close the broadcast run.the second problem is i want run the application when the application is in closed state too. So i think I have call a service from activity and that service make that broadcast receiver to run in the background. And if i want to stop my application's processing open app and click the button to off. Help me friends..
You can ,
When click on
Registered your broadcast
IntentFilter filter=new IntentFilter(TelephonyManager.EXTRA_STATE);
registerReceiver(new BroadcastReceiverClass.class, filter);
when click off
unregistered your broadcast
unregisterReceiver(new BroadcastReceiverClass());
Related
I have two buttons -start and stop service buttons- in the fragment. In the initial state start button is enabled and stop button is disabled. When i click "start button",buttons states are changed. -It means start button is disabled and stop button is enabled. In the service i check gps state. If the gps is disabled, i call stopself method to destroy service. After calling stopself method, I wanna change buttons states in the fragment. But i didn't find any solution. How can i do this?
You can use below option.
In your fragment.
//In your on Create method
IntentFilter updateState = new IntentFilter();
updateState.addAction("UPDATE_BUTTON_STATE");
registerReceiver(updateStatueReceiver, updateState);
private BroadcastReceiver updateStatueReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("UPDATE_BUTTON_STATE")) {
// Here you can update your button state
}
}
};
//unregister Receiver in on Destroy method
unregisterReceiver(updateStatueReceiver);
In your Service Class
You can send broadcast using this code
Intent broadCastIntent = new Intent();
broadCastIntent.setAction("UPDATE_BUTTON_STATE");
sendBroadcast(broadCastIntent);
This question already has answers here:
Checking if an Android application is running in the background
(35 answers)
Closed 5 years ago.
My app consists of just one activity (launchMode=singleTask) with a viewpager in it. In the app, the user schedules alarms through the AlarmManager. When these alarms are triggered, a broadcast receiver creates notifications in the status bar (wether the app is open or not).
I'd like to update a RecyclerView in the activity whenever a notification is issued AND THE ACTIVITY IS IN THE FOREGROUND. If the activity is in the background or not 'active' at all (doesn't exist), there's no need to call it to the front or open it just to update the RecyclerView.
How can I check if my activity exists before calling, say, "MyActivity.updateMyRecyclerView()"? (I have an idea on how to check if it's in the foreground or not, I think that wouldn't be a problem.)
You can use broadcast receiver for your activity. You can send a broadcast and if your activity is 'alive' and is registered to receive the broadcast you can trigger the recyclerView update from there.
Send the broadcast when you are creating the notification like this
Intent intent = new Intent("key_to_identify_the_broadcast");
Bundle bundle = new Bundle();
bundle.putBoolean("updateRecyclerView",true);
intent.putExtra("bundle_key_for_intent", bundle);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
and in your Activity where you want to receive this intent, you can use a broadcast receiver
private final BroadcastReceiver mHandleMessageReceiver = new
BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle =
intent.getExtras().getBundle("bundle_key_for_intent");
if(bundle!=null){
boolean shouldRefresh = bundle.getBoolean("updateRecyclerView");
if(shouldRefresh){
//Refresh your recyclerView
}
}
}
};
You need to register and unregister the receiver to work
In your onResume method you can register this receiver to receive the broadcast
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter filter = new IntentFilter("key_to_identify_the_broadcast");
LocalBroadcastManager.getInstance(this)
.registerReceiver(mHandleMessageReceiver,filter);
}
You also need to unregister it before your activity gets paused
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
try {
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(mHandleMessageReceiver);
} catch (Exception e) {
Log.e("UnRegister Error", "> " + e.getMessage());
}
}
I want to know the time left before screen off.
I can easily get the timeout defined by system, but the counter resets with each touch the user do. so I need to reset the counter on my side as well.
I don't want to have an activity in foreground but to use (probably) a broadcastReceiver.
You can't do this but you can detect when screen off and screen on - Use actions:
Intent.ACTION_SCREEN_ON
Intent.ACTION_SCREEN_OFF
But do not put it in AndroidManifest.xml you must register this broadcast, example:
public class ScreenReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if(action.equals(Intent.ACTION_SCREEN_ON)){
// screen on
} else {
// screen off
}
}
}
To register broadcast (do not call it in this broadcast (ScreenReceiver) - call it in Service, Activity or Application):
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new ScreenReceiver(), filter);
I guess the best solution is register broadcast in service because when Activity calls onDestroy() broadcast may be automatically unregistered.
I am to create an application.Through my application users can read the incoming phone number.My activity have two buttons, On and off. If the user clicks the on button ,then the following code will execute.
IntentFilter filter=new IntentFilter(TelephonyManager.EXTRA_STATE);
registerReceiver(new MyPhoneReceiver(), filter);
And If the user clicks the off button ,then the following code will execute.
unregisterReceiver(new MyPhoneReceiver());
MyPhoneReceiver is my brcastreceiver calss name.If the user is click the on button then the onReceive() method will be ready to read incoming call number and display that number.Likewise if I click the off button, then broadcast will be unregistered.
My onRecieve() code is
public void onReceive(Context context, Intent intent) {
System.out.println("am hereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("sarath DEBUG", state);
System.out.println("state is:"+state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w("sarath DEBUG", phoneNumber);
System.out.println("phone no. is:"+phoneNumber);
}
}
}
My problem is when i click the on button nothing is happening.when a incoming call came then also its not executing onRecive() code.please help me friends
See this article:
Using PhoneStateListener to listen to state change in Android
Source code is here.
This method requires the permission in AndroidManifest.xml. You added any permissions in your application?
I'm trying to turn the GPS location updates off when the screen locks. Having read the answer to this question Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?
I've written some code to implement a BroadcastReceiver but it's not working when the screen goes off.
I've registered a BroadcastReceiver in my code with
#Override
public void onResume() {
super.onResume();
IntentFilter iFilter = new IntentFilter();
iFilter.addAction("android.intent.action.ACTION_SCREEN_ON");
iFilter.addAction("android.intent.action.ACTION_SCREEN_OFF");
registerReceiver(screenStatReceiver, iFilter);
and the receiver itself is just a stub for now:
public BroadcastReceiver screenStatReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("BCAST_TAG", "Got broadcast");
String action = intent.getAction();
}
};
and in the manifest I have:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Any ideas as to why it's not being triggered when I debug it on my phone?
For this problem, you have to create a infinite service, which is registering a local broadcast receiver for these intents. If you do this way, then your app will look for screen off but make sure you have to make service which will always running in background like reboot receiver
You are registering the reciever in the onResume() method. Why dont you register it in the onCreate() so you can have persistent "listening"?
In this case you are listening for changes only after the activity is resumed.