how to measure the elapsed time while the phone is in use - android

I have code below to get the status of screen. What I am trying to do is to make a kind of chronometre to measure the elapsed time while phone is open or in use in my Android application. when screen is ON, the phone is in use,I thought this way. if there is any other way, I want to know.
The answer here was not good , I have tried and It did not work onresume, I dont know why... Also I think it is not a good way, not professional. I need your help to calculate the elapsed time while the phone is in use. Thanks
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
///start calculating the elapsed time
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
protected void onPause() {
if (ScreenReceiver.screenMod) {
Toast.makeText(getApplicationContext(),"turn off",Toast.LENGTH_SHORT).show();
/////stop calculating
} else {
}
super.onPause();
}
#Override
protected void onResume() {
if (!ScreenReceiver.screenMod) {
Toast.makeText(getApplicationContext(),"turn on",Toast.LENGTH_LONG).show();
////continue calculating
} else {
}
super.onResume();
}
}
ScreenReceiver.class
public class ScreenReceiver extends BroadcastReceiver {
public static boolean screenMod = true;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenMod = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenMod = true;
}
}
}

You can do that without using any ui interraction. Just declare your broadcast receiver in manifest. When you get Intent.ACTION_SCREEN_ON create a variable which holds system time with System.currentTimeMillis(). Save another variable which holds When you get Intent.ACTION_SCREEN_OFF event. Calculate the difference between to values you will get phone use time in milliseconds. Sum it with sharedpreferences last time

Related

Is their any way to make the service runnable when it killed?

Hi all i did research more than a lot but no one give me the right answer .
I am surprised because my question is so simple ,but didn't found a single solution.
I found a lots of question like mine but people making their answer round and round ,i am really sick of this now this is my last and final try
i will appreciate if any of you resolve it
Let me light up mu Question again in Brief:
i just want to make a simple service which hear the power button (really so simple) ,Now i did that easy thing , i mean i made a service with a broadcast receiver ok (Quite Simple) , and it run well , But!!! whenever i kill the app from background task , then the service stopped automatically , what i want here , that killing the task do not effect my service (how simple is this)
(Note: 1. i already used START_STICKY and Service.START_STICKY
2.Also i did a bad thing that is is called service in onDestroy.
But Still got thumbs down.
)
Please give me a fine solution
I will only appreciate after answer
(Because i am really so sick of this )
Here is my Code
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, MyService.class));
}
MyService:
public class MyService extends Service {
private BroadcastReceiver mReceiver = null;
public MyService() {
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
#Override
public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
ScreenReceiver:
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
#Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.e("LightWriter", "I WORK BRO.");
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.e("LightWriter", "I WORK BRO.");
wasScreenOn = true;
}
}}
I am felling so dumb about this , hey i got the answer
1. Those who think that its not possible are wrong
i just try with other device and it works well
the issue with my device is i have marshmallow , so if any one have it allow permission from permission manager (works fine ).
Thanks for your time:)

ANR after unlock the device

I am developing a fingerprint sensor application that use good amount of RAM,problem may be related to memory, i tried a lot still unsolved.
Q:- When i press home button application paused and from the recent button it resumed successfully, but when i press power button then unlock my device,it show ANR instead of my application screen.
It means it is not being resumed when unlocking the device.
Please suggest or give solutions if you got my point.
NOTE:
Used device is dedicated to my application no other application will be installed or run.
Maybe you need broadcast receiver to listen screen on or off.
public class ScreenReceiver extends BroadcastReceiver {
// thanks Jason
public static boolean wasScreenOn = true;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
wasScreenOn = true;
}
}
}
And in your Activity, it will be like this
public class ExampleActivity extends Activity {
#Override
protected void onCreate() {
// initialize receiver
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
// your code
}
#Override
protected void onPause() {
// when the screen is about to turn off
if (ScreenReceiver.wasScreenOn) {
// this is the case when onPause() is called by the system due to a screen state change
System.out.println("SCREEN TURNED OFF");
} else {
// this is when onPause() is called when the screen state has not changed
}
super.onPause();
}
#Override
protected void onResume() {
// only when screen turns on
if (!ScreenReceiver.wasScreenOn) {
// this is when onResume() is called due to a screen state change
System.out.println("SCREEN TURNED ON");
} else {
// this is when onResume() is called when the screen state has not changed
}
super.onResume();
}
}

Turning screen on and off in Android

I am having a bit of trouble with regards to the Android code I am trying to produce. What I am trying to do is simply print out a 1 if the user wants to unlock their phone, and when the program first starts, and a 0 when the user presses the screen lock to lock their phone. I thought this had to do with the Android lifecycle so I tried using onPause and onResume, but my program only prints out 0's, never 1's. The logTime method simply prints out the 1 or a 0, and the onCreate method in the MainActivity calls onResume() once. Here is my code:
MainActivity:
protected void onPause(){
if(ScreenReceiver.screenOn){
logTime(ScreenReceiver.screenOn);
super.onPause();
}
protected void onResume()
if(!ScreenReceiver.screenOn){
logTime(!ScreenReceiver.screenOn);
super.onResume();
}
Screen Receiver:
public class ScreenReceiver extends BroadcastReceiver{
public static boolean screenOn;
#Override
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
screenOn = false;
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
screenOn = true;
}
}
}
I'm not sure why it only prints out 0's. Might anyone know why? Thanks!
There is a delay between your Activity's onPause() and the actual Broadcast being received, so usually you can get "odd" (wrong) readings since the variable change hasn't happened. This receiver needs to be dynamically registered, and you also want it to receive even when the screen is off and turns back on. Usually, Receivers are unregistered in onPause() to avoid leaking, but here, we're going to use onDestroy() since that's the last method call we can use. For simplicity, I made the BroadcastReceiver right in the Activity and called logTime() outright.
Sample Code:
public class MainActivity extends Activity {
BroadcastReceiver receiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
if(arg1.getAction().equals(Intent.ACTION_SCREEN_OFF)){
logTime(false);
}
else if(arg1.getAction().equals(Intent.ACTION_SCREEN_ON)){
logTime(true);
}
}
};
registerReceiver(receiver, filter);
}
#Override
protected void onDestroy()
{
try{
unregisterReceiver (receiver);
}
catch (NullPointerException e){
e.printStackTrace();
}
catch (IllegalStateException e1){
e.printStackTrace();
}
super.onDestroy();
}
public void logTime (boolean screen)
{
Time now = new Time();
now.setToNow();
String lsNow = now.format("%m-%d-%Y %I:%M:%S");
TextView myText = (TextView) findViewById (R.id.myText);
myText.append (" " + lsNow + (screen ? "0" : "1"));
}
}
Everything seems right based on your code, it should always print 0...
When the screen is turned off, screenOn is set to false, your Activity's onPause is called, it prints 0.
When your application is running and onResume is called, screenOn would be true (whether the screen was just turned on or not), there, you log the opposite of screenOn, so would be 0 again...

Display screen/activity on Unlock event?

I essentially want to display a screen whenever the screen is unlocked regardless of the application already running.
Can someone just tell me how to display text as soon as the phone gets unlocked. I can take it from then on.
I have the following code up till now which I found on the net....
Suppose I want to display abc.xml as soon as the phone gets unlocked. How will i add it in the ScreenReceiver Class ?
Also I do not want to set any screen when the application runs.Do I need to run the code below as service ?
public class SampleActivity extends Activity {
//Declare the necessary variables
private BroadcastReceiver mReceiver;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public void onDestroy()
{
super.onDestroy();
Log.v("$$$$$$", "In Method: onDestroy()");
if (mReceiver != null)
{
unregisterReceiver(mReceiver);
mReceiver = null;
}
}
}
where the Screen Reciever class is as follows
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
Log.v("$$$$$$", "In Method: ACTION_SCREEN_OFF");
// onPause() will be called.
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
Log.v("$$$$$$", "In Method: ACTION_SCREEN_ON");
//onResume() will be called.
// Better check for whether the screen was already locked
//if locked, do not take any resuming action in onResume()
//Suggest you, not to take any resuming action here.
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
Log.v("$$$$$$", "In Method: ACTION_USER_PRESENT");
// Handle resuming events
}
}
}
For one, you don't display abc.xml, you display an activity, dialog, or other UI component. You can set up a broadcast receiver that listens for the ACTION_BOOT_COMPLETED intent. Once the device boot completes, you can start a sticky service to listen for the actions you have above. Presumably you'll want to display abc.xml in an activity, so you'll need to fire startActivity from one of the if() blocks above.

How to register to sleep event in Android?

I am working on Android 3.0 and I need to know in my application when the device goes on sleep / turn off screen.
How can I register to this intent/event so I will be able to run some actions when this happens? Is there any action in BroadcastReceiver that notifies for this?
This page has a tutorial on exactly what you're looking for.
Code copied from that page (in order to turn this from a link-only answer to something directly useful):
1) Create a class in your application to receive the intent. For example, the following receiver stands alone and sets a static variable to be used in part 2:
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
wasScreenOn = true;
}
}
}
2) Modify your activity to receive screen on/off events. Your receiver will check the static variable in your broadcast receiver to know the reason for the intent you just received:
public class ExampleActivity extends Activity {
#Override
protected void onCreate() {
// initialize receiver
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
// your code
}
#Override
protected void onPause() {
// when the screen is about to turn off
if (ScreenReceiver.wasScreenOn) {
// this is the case when onPause() is called by the system due to a screen state change
System.out.println("SCREEN TURNED OFF");
} else {
// this is when onPause() is called when the screen state has not changed
}
super.onPause();
}
#Override
protected void onResume() {
// only when screen turns on
if (!ScreenReceiver.wasScreenOn) {
// this is when onResume() is called due to a screen state change
System.out.println("SCREEN TURNED ON");
} else {
// this is when onResume() is called when the screen state has not changed
}
super.onResume();
}
}

Categories

Resources