Count interactions with screen in Android phone - android

I am new in Android and is developing an app which runs in background as service to collect user activity. till now my app is able to get information about Time_Start, Time_End and Name of other app used by user.
I want to improve my app to be able to count how many interactions(like user tap, touch,...) user make while using other app. Can any one give some advices about this issue? Just the way to do and I'll do all details by myself.
Thanks!

The best way to do this is to implement Listeners for the types of user interactions you would like to count. Have a global variable in your activity. Every time the user interacts, add one to it.
If you want to keep count across the entire app, you could save a variable in SharedPreferences, and at the end of each activity, add your activity's global variable count to the value you have store in shared preferences. Or, you could just edit the value you have stored in SharedPreferences right off the bat on every user interaction. Just remember to reset the value of the SharedPreferences when you first start the application, as values stored in SharedPreferences are stored even after the app is closed unless you specifically remove them, or clear the SharedPreferences.
Another way you could keep count is by using a global static variable that is accessible from all other activities. This way you would be able to increment it from other activities. This is generally considered bad practices though, so I advise against this.
The documentation for SharedPreferences is here:
http://developer.android.com/reference/android/content/SharedPreferences.html
The documentation for implementing input event Listeners is here:
http://developer.android.com/guide/topics/ui/ui-events.html
Hope this helped!

Related

counter to track the number of times a user has used a feature - android

I'm making an app that starts with a splash activity and somewhere in its some other activity requires a counter to track the number of times a user has used a particular feature. This counter should have all the counts since when the user had registered to my app for the very first time. I want to store this counter to firebase and not use shared preferences as the value will be lost the moment the user uninstalls the app. Could someone please suggest a method here?
You can use Shared Preference to this job.
https://developer.android.com/reference/android/content/SharedPreferences

Sharing small amount of data between apps

I have couple of applications that implements some login logic. If lets say one application is logged to some_account#gmail.com I want that all of these applications be logged to some_account#gmail.com. If I logout I want to all application do the same. But I don't want to immediately do the same. Application itself can handle it, but it need to know if some other application is already logged in and if yes just log in for the same email address as this app. So I need to know what is the email address for which other app is logged. I need to store one string.
First I was thinking about SharedPreferences, but this is rather bad idea because there are other options (and stackoverflow is full of bad example of SharedPreferences usage between processes). Despite it I tried this. Set up sharedUserId on all apps, called createPackageContext and eventually try to get preferences. But I cannot read from it. Always I got null, even if I used Context.Mode_WORLD_READABLE - which is deprecated by the way.
Ok, lesson learned do not use SharedPreferences for that (I suppose). But everything what I need now is to store single string somewhere where it could be read by other my apps.
Maybe I should use ContentProvider? But seriously... for one string?
What is the other option? I am sure that for so simple operation I really don't need Service or ContentProvider, but I actually haven't got idea how to do that.
You could use a broadcast receiver. All you would have to do is send a broadcast to application B when the data changes in application A. Then application B can handle the data in the background, and store it however you need to. It might be a bit of over kill, and there could be a better way to do it, but it would work.

ANDROID - class Application

I'm trying to understand how the Application class.
I've noticed that need to declare it in <application> manifest within the tag and then can access the variables in other classes as they were global variables. And even out of the application the value of these varieties do not change.
However, if you unplug the phone, the next time you turn it on and start applying the value of the variables returned to its initial state.
I wonder if you can maintain the state of variables when we turned off the phone and reconnect it?
Application data is available as long as your application is "active". When the OS decides to terminate it to clear memory, so goes your application data (you typically don't control when this happens, as per the mobile development good practices: the OS decides on its own), and it's not persisted for the next time you start the app. So anything you store in the Application should be stored again each time the app is started.
It should be used to keep short-term data available to you. A good use case is when you need to access a complex data structure from multiple activities: it's not possible to use bundles for that. You can generate your complex data structure in your start activity, store it in the application, and then retrieve it in any other application that may need it.
But you should not use it for long-term persistent data. For that, the best is to use a SQLite database.
I'm not sure I fully understand what you mean, but it seems like you want to use Shared Preferences.
try this Question: Android - How Do I Set A Preference In Code

How to store user state in Android in my case

I'm asking this question because I want to be sure which is the best way to store the user "logged in" / "logged out" state in my android application.
Here is what I am doing. In my application I connect to the server, send him username and password and if everything is ok it returns me OK. So now I want to be able to save the user logged in state all over my application because I need to change the content depends on that if user is logged in or not.
And the other thing is that I need to be able to save the user's login state, so the next time when he start the application he will be automatically logged in.
Any suggestions or ideas which is the best way to do that?
I made a little research over internet and find a few ways to do that :
MyApplication class which extends android.app.Application
SharedPreference
Or maybe a SQLite
There can be many reasons when your app looses focus, like pressing home button incoming call, etc but no matter what happens your onPause method of current Activity is called so what you have to is-
Override onPause method of every activity
Use SharedPreference to save the state of Application
I use static classes or SharedPreference - it'is easy and it's works.
SQLite it not good way (in my opinion)
your option SharedPreference is the perfect one in this condition as you need to store only boolean and you will access if often in your app to change the content.
The problem with storing the state on the device (using either the SharedPreferences or SQLite) is that the user can manipulate it. So if you simply store "isLoggedIn" in your SQLite-Database, this entry can be manipulated using dpms (for example).
So if you need to store the state, you should use the Application-class (this can be manipulated, too, but it's harder to do so). If the user closes the application, you destroy the state-variable so he needs to do the login when the application is opened the next time.
For usability, you could store username and (if the user wants so) the password.
Also, if you have a web-service that you use to check if the users login-data is correct, why don't you use for example OAuth to authentify the user on the server and deliver the content from it? This would basically make your application a pure "client".
SharedPreference is the best option to access single value in terms of timing which i observe to be true as compared to sql lite.

Determining how much time has passed since the app was last used in android?

I want to display a msg to the user like "You have been not using this app since XXX days". How exactly can i do that in android? can we store the time info in shared preferences and then do the comparison with present date. am facing trouble handling time info in shared preferences. Would like to know if there are any other solutions to this prob
Yes, the solution via shared preferences is a doable one. You can store the date as a long and then construct the new date later from this long.
I would suggest having all of your Activity classes update a timestamp stored in shared preferences when their onPause() method is called.
The easiest way to have this functionality across all of the activities in your app would be to have them all extend some base activity and just put the code in the onPause() method of that.

Categories

Resources