Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
public void frequency1(View view)
{
  UsageEvents.Event event=new UsageEvents.Event();
 if(event.getEventType()==UsageEvents.Event.DEVICE_STARTUP)
{
count++;
}
 tv.setText(Integer.toString(count));
}


What I have tried:

I am using this function to display the frequency of unlocking the mobile phone on a TextView but whenever the app is running the TextView field is blank i.e. the count variable is not being updated in the TextView field.
Posted
Comments
wseng 9-Nov-22 23:08pm    
1. Did frequency1 method get called?
2. is count++ line get executed?
Gayatri Gupta 10-Nov-22 1:44am    
This is the main class code I have written till now...
public class MainActivity extends AppCompatActivity {
private TextView tv;
private int count=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView);

}
public void frequency1(View view) {
UsageEvents.Event event=new UsageEvents.Event();
if(event.getEventType()==UsageEvents.Event.DEVICE_STARTUP)
{
count++;
}
tv.setText(Integer.toString(count));
}

}
[no name] 10-Nov-22 2:42am    
Is count saved anywhere before device startup and reset?
Gayatri Gupta 10-Nov-22 2:45am    
No...I have just initialized it to zero in main class.
[no name] 10-Nov-22 2:49am    
Thus, it doesn't follow to the logic of your task as when user resets his device, the counter value is the same provided your function is executed.

Moreover, I'd like to recommend you to learn SQLite and Hibernate as it's of high demand nowadays and local database on the device can be used as a storage for reset/startup count value.

1 solution

First of all you have to bind frequency1() function to event like onUpdate(), etc.

Also, the counter variable is to be static for reasons like the activity can be re-instantiated for multiple times.

As I've posted in comments, the count is to be saved somewhere if device startup or reset takes its turn.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900