I want to identify when user has switched to Automatic date & time for that i have tried to catch Intent.ACTION_TIME_CHANGED intent , which fires twice in case of Automatic date & time. But when we change time manually Intent.ACTION_TIME_CHANGED fires only once.
i have tried to identify by using autoTimeCalled static variable and now i want to identify when user has changed date manually.
public static boolean isAutoTimeCalled= false
if (intent.getAction().equals(Intent.ACTION_TIME_CHANGED)){
autoTimeCalled++;
if( autoTimeCalled ==2 ){
Log.e("AUTO" , autoTimeCalled+"");
autoTimeCalled =0;
isAutoTimeCalled = true;
}
}
can anyone suggest me the better approach to bifurcate between manual and auto time operations.
Fetch value in your receiver:
android.provider.Settings.Global.getInt(getContentResolver(), android.provider.Settings.Global.AUTO_TIME, 0);
It will tell you current settings. It is for API 17 and above.
For lower API
android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0);
Related
In my app, I have the following code that tells me if a feature is enabled by default :
public boolean getFeatureEnabled()
{
return mPrefs.getBoolean("FEATURE_ENABLED", DEFAULT_ENABLED);
}
This preference is overwritten only when the user changes the setting from UI. So by default it draws its value from DEFAULT_ENABLED which is a class variable somewhere.
In the current version, DEFAULT_ENABLED is true but on the next version of my app will be false.
The problem is that after the update, with the above code the old users who did not change the default setting from UI will have their feature disable - and I want to avoid this.
Any advices on how to handle this ?
As I understand, you have a feature that was enabled by default but this default was never written to SharedPreferences unless explicitly changed by the user.
Now you want the feature to be disabled by default but without affecting the behavior for users that already have it enabled.
I can think of 3 options:
Option 1 If you are already saving the last version, you could check that in your migration logic:
private void migratePreferences(Context context) {
SharedPreferences prefs = context.getSharedPreferences("your_preference_file", MODE_PRIVATE);
int lastKnownVersionCode = (prefs.getInt("LAST_INSTALLED_VERSION", BuildConfig.VERSION_CODE);
prefs.edit().putInt("LAST_INSTALLED_VERSION", BuildConfig.VERSION_CODE).apply();
//this is the old featureEnabled check
boolean oldPreferenceValue = prefs.getBoolean("FEATURE_ENABLED", true);
boolean newPreferenceValue;
if (prefs.contains("FEATURE_ENABLED")) {
//the feature was modified by the user so respect their preference
newPreferenceValue = prefs.getBoolean("FEATURE_ENABLED", false);
} else if (lastKnownVersionCode == BUGGY_VERSION_WITH_FEATURE_ENABLED_BY_DEFAULT) {
//the user is updating from the buggy version.
// this check could include a range of versions if you've released several buggy versions.
// this is also where option 2 would be inserted
newPreferenceValue = oldPreferenceValue;
} else {
//the new default that will apply to fresh installs
newPreferenceValue = false;
}
//save the preference
prefs.edit().putBoolean("FEATURE_ENABLED", newPreferenceValue).apply();
}
This, however depends on your already having a call to this method somewhere in your app startup code.
Option 2 In case you don't, there is still hope. You can check if this is your first install using the answers given in this StackOverflow answer
Option 3 You can release an intermediate version of your app that behaves as it does now but saves the unsaved default setting in SharedPreferences. This will keep the feature AS IS for your eager users but you will have to wait until a significant portion of users updates before releasing the desired behavior.
Put another flag "FIRST_TIME" as "true" in your preferences in new build. Check on the very first screen of your app
if(FIRST_TIME==true)
{
//put FEATURE_ENABLED = false;
//put FIRST_TIME = false;
}
By doing this FEATURE_ENABLED will set to false for the first time the user launches the app and will not consider the default value
I know there are a lot of questions asked that are related to this but they all seems not to solve my problem.
I want to check if the date on the user's device is correct, start an activity but in a case where the date on the users device is wrong, it should show an error activity that asks the user to adjust their date just like how whatsapp implemented theirs..
You must have server's timestamps to determine if the time in device is fake.
Npt pool is a free service to help you get true timestamps.
To use, device must ONLINE, you can not check without the Internet.
Copy class SntpClient to your project: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/net/SntpClient.java
Code:
SntpClient client = new SntpClient();
long now = -1;
if (client.requestTime("pool.ntp.org", TIME_OUT)) {
now = client.getNtpTime();
if (Math.abs(now - System.currentTimeMillis()) >= ONE_DAY){
// the device's time is wrong
startErrorActivity();
...
} else {
// the different time lower than 1 day
startNextActivity();
}
} else {
// something wrong, can't get server's time
}
Don't forget add INTERNET permission to your manifest
Recently, I have developed an app on travelling domain. The aim of this app is to show the user; the path from source to destination. When the user comes in the range of 25 meters range of the destination, the user gets a notification/ an alert stating that the destination is nearby.
What I tried to achieve it:
In the onLocationChanged() I kept on for the destination's range & if the user is in the range, the notification/alert will pop-out. However, when the I tested the app, I found out that when I am in the range, the notifications flood the device horribly as the condition of showing the notification/alert is based on the onLocationChanged() ie as location changes, the loop executes the exact same number of times and the user get annoyed by the app.
Also, the app does not work when I search for a different location. It does not show the destination marker. For the first time, the destination marker is seen but later searches do not show the destination marker; I wonder why?!
This problem has bugged me since long time. Please help me on this one!!
you can keep a check on the number of time the notification is displayed, for example in loop
boolean notification_shown = false;
for(...)
{
if(!notification_shown)
{
//show notification
notification_shown = true;
}
else
{
//rest of your coding
}
}
this will show the notification only one time. If you want to display notification for few more times than you can use counter. like
int counter = 0;
//increment it till you want and than stop
Two possible solutions that are probably best combined: one, use a static dialog so that you only have one instance of the alert; two, unregister the onLocationChanged() listener as soon as you reach the destination (the first time it alerts the user).
When you are using loop in the onLocationChange() method, in side your loop take a variable say "count" and then check the condition for the distance. After the particular condition got true then increment the count. If counter get 1 then pop an alert and after that exit from the loop.
int count=0;
public void onLocationChanged(Location locFromGps) {
if(locFromGps<=30)
{
if(count==1)
{
exit(0);
}
count++;
Toast.makeText(getApplicationContext(),"Location is near", 0).show();
}
}
I want to use this info to determine the most frequently used apps on my device.
I am also interested in finding out if there is a way to determine how much time I have spent using each app on my device.
It seems this info might be accessible only on "jailbroken" devices. I am hoping that is not the case.
Thanks much!
i think you can't do that for all apps.
instead , if you want to know how much times your app has been launched , try this :
on the splachscreen of your app ( or the first activity launched) , try to increment a number , and save it on the SharedPreferences :
SharedPreferences prefs = getSharedPreferences("prefs",Context.MODE_PRIVATE);
int counter = prefs.getInt("counter", -1);
if(counter == -1 ) {
//first launch of the app, init the counter
counter = 1;
}
else {
// increment the counter
counter++;
}
// update the value of the counter
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("counter",counter);
editor.commit();
//then you can do what you want with your counter , send it to your back end via web service...etc
How to identify whether Android phone clock is set to hours mode or AM/PM mode?
Try out is24HourFormat function of the DateFormat class. It should return true if the user has selected 24-hour-format, otherwise false. Have a look at the docs too: DateFormat.is24HourFormat(Context context)