Turn off proximity sensor after closing app - Android - java

I made an app that reads values off of the proximity sensor and performs certain actions based off of that data. My problem is that the actions will continue to perform even when the user presses the home button and leaves the app, thus causing a drainage in the battery.
My question is how do I turn off the proximity sensor once the app is existed or simple when the home button is pressed (without the app being exited properly)
This is my first Android app. Here is how I assign the proximity sensor in my code:
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
proxSensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
sm.registerListener(this,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
Thanks
EDIT
To those that are wondering and don't want to look it up, I solved it by using the following:
public void onPause()
{
super.onPause();
sm.unregisterListener(this);
}
public void onResume()
{
super.onResume();
sm.registerListener(this,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
}

Look into the Activity method onStop(). It's called whenever the Activity is no longer visible, (app is exited or home button is pressed). It is into this method where you will put your code to stop the proximity sensor.
See Android Activity lifecycle diagram here for more info:
http://developer.android.com/reference/android/app/Activity.html

Apps don't exit on Android. At most Activities will finish. But to turn off behavior when another activity is active, turn it off in the activity's onPause.

Related

How to completely destroy an activity from code?

I am building an medication reminder app in android using java. User can set one or more reminders and according to those will get notified to take the medicine.
Problem is, whenever a notification is generated, and the user taps on it and the receiver activity opens up, the user is presented with two choices of either taking the medicine or skipping it. Now, I have made sure that in both the cases, the activity will finish and have called its onDestroy() method too. My objective is to prevent the activity from appearing in the recent app list, so that the user can not repeatedly take or skip medicines.
Here's the details(the links all point to screenshots of the app if it helps in any way):
The notification comes,User taps on the notification and the Reminder Receive activity opens up
User either takes or skips the medicine, and the activity finishes.
but the activity is still being shown on the recent app list, and if tapped on it, it opens up the reminder receive activity again, with the user able to perform the same action again
I want to prevent this particular behaviour from happening.
Here's the things I have tried(I have a fragment,running on top of the activity):
finishing the activity from fragment,then calling onDestroy() on it
ReminderRecieveActivity activity = (ReminderRecieveActivity) requireActivity();
activity.finish();
activity.onDestroy();
Modifying the onDestroy() method of the activity like so
#Override
protected void onDestroy() {
super.onDestroy();
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
System.exit(0);
}
But still the problem persists, Please help.
Why not create a separate Activity with android:noHistory="true" and also android:excludeFromRecents="true"? You don't have to kill the process to do that.

How to monitor if my App is paused or resumed?

I have an Android app where I want to track when the app is paused or resumed.
paused: User pressed the home button and the app is still running in the background.
resumed: app runs in background and user opens the app.
How can I being notified when my app was paused/resumed?
paused: User pressed the home button and the app is still running in the background.
I am going to guess that the initial state is that one of your activities was in the foreground at the time the HOME button was pressed.
On the whole, there is no notion in Android of an "app" being in the foreground or the background, though we sometimes use that phrasing as shorthand for other scenarios.
Whatever activity was in the foreground will be called with onPause() and onStop() when the user presses HOME, but those events are also called in many other scenarios (e.g., user presses BACK). onUserLeaveHint() will be called when the user presses HOME but not BACK, but onUserLeaveHint() is not called in other scenarios (e.g., incoming call screen takes over the foreground). Whether onUserLeaveHint() will meet your requirements, I cannot say.
resumed: app runs in background and user opens the app.
onStart() and onResume(), at minimum, will be called on your activity that takes over the foreground. Those will be called at other times too, such as when the activity is coming onto the screen for the first time. There is also onRestart(), which will be called only if the activity is being started after having been stopped (i.e., after a prior onStop() call), which will weed out the newly-created-activity scenario. However, onRestart() will be called in other scenarios as well, such as part of a configuration change (e.g., screen rotation).
In general, what you are seeking is not really part of the Android architecture. You may need to approach your problem in some other way.
In you Android activity you can override the onPause and onResume methods.
See the documentation on Lifecycle Callbacks for a list of other lifecycle callbacks that you can implement.

Android - calling finish() on activity does not stop it from running

So I've looked up similar problems to this, and have followed the advice in those threads, but it seems to give me no change in behavior.
So I'm writing an app that essentially notifies the user when they're going too fast or too slow based on GPS onLocationChanged() calls. I overrided the onBackPressed() method to finish() and return but the activity continues to run in the background when I go back to the main activity.
To be clear, I DO want the activity to run when the app is minimized or screen is off. I only want it to stop when the user goes back to the menu (IE, hits the back button)
Here's the link to the class on pastebin:
http://pastebin.com/V7z5c3HH
Thanks for your help! =D
Unsubscribe your location listener in the onDestroy method.
However, what you need for your GPS processing is probably a Service, not an Activity.
You need to remove the updates for that listener.
#Override
protected void onDestroy() {
locationManager.removeUpdates(locationListener);
super.onDestroy();
}

android - How to detect application being activated

When the app is launched, Application onCreate is called. How to detect when the app is brought to front from running in background?
Look for onResume() method. Its is always called when your app comes foreground.
As per google docs:
The foreground lifetime of an activity happens between a call to
onResume() until a corresponding call to onPause(). During this time
the activity is in front of all other activities and interacting with
the user. An activity can frequently go between the resumed and paused
states -- for example when the device goes to sleep, when an activity
result is delivered, when a new intent is delivered -- so the code in
these methods should be fairly lightweight.
CODE SAMPLE:
#Override
public void onResume()
{
super.onResume();
Log.d("tag", "This screen is back");
}
You can override onResume().
#Override
public void onResume()
{
Log.d("tag", "This screen is back");
}
However, I would agree with the comment that you probably should look more into this to see how Android works.

Android Java: Understanding Activity Life?

I'm new to android development and I get how the android has an activity life.
If I have an app and I press a button to use the phone's camera funcationality like so...
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_VIDEO_REQUEST);
}
How does onPause() or onDestroy() and the other stuff work?
I have this outside the onCreate()
protected void onPause(){
super.onPause();
}
If I want to press the back button or press the home button, do I have to destroy or pause the camera function? If so, I'm still trying to figure out how to do so?
Thanks!
When you start new activity from your current activity then there are two possibility of your current activity
Pause
Stop
Paused:
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
Stopped:
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
For example you are starting the Camera activity from your activity then your current activity will be Stop because the Camera activity will covers all your screen and your activity is not visible to camera activity.
Here is the complete description.
You are starting the Camera activity using the Intent so you do not have to handle the call back methods of the camera activity. System will manage the call back method you do not have to manage it.You just have to manage the Activity result which you will get in your activity from the Camera activity.
EDIT
And also ob course you never have to directly call any life cycle methods of the Activity.System automatically calls this method according to activity state.You just have to write your implementation in this methods to do your work.

Categories