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)
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 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);
I have seen this question : check phone settings for haptic feedback but I don't understand the solution (for me it does not work).
Is there another solution ? I did not find anything on the javadoc.. ?
Thanks
You can add this method to any of your classes and use it to check whether haptic feedback is enabled.
public static boolean isHapticFeedbackEnabled(Context context) {
int enabled = Settings.System.getInt(context.getContentResolver(),
android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED, 0);
return enabled != 0;
}
The method goes into system settings database and checks value of "haptic_feedback_enabled" setting. If there is 0 then haptic feedback is disables, otherwise it is enabled.
I'm implementing ACRA in my Android App. I would like to be have it's interaction mode be silent by default, but if I see that a new version of my App is available, "Toast" the user upon crash with a message to update their copy of the App.
However it seems that ACRA interaction mode must be hard coded and loaded once upon init of application. Any way to set the mode at that point not via a hard coded setting? E.g. I'm looking to control the "mode = " setting which is copied below.
#ReportsCrashes(formKey = "xxxxxxxxxxxxxxxx",
mode = ReportingInteractionMode.TOAST,
resToastText = R.string.crash_toast_text)
public class MyApplication extends Application ...
I see that ACRA Issue 85 has added setters for resources for Toast parameters, but does it also have ability to configure interaction mode?
Maybe this? I didn't try but seems useful (at point of start)
http://www.java2s.com/Open-Source/Android/App/acra/org/acra/ErrorReporter.java.htm
How to get current time format of the android device? i.e. If the android device's current time format is changed from 12hr to 24 hr format and vice versa, then how to check that setting programatically?
I want to change the time shown in my app widget to be of same format as the device's and whenever the time format is changed then my widget should also show that time format. Any pointers will be helpful.
thanks.
You can use TIME_12_24.
int time_format = 0;
try {
time_format = Settings.System.getInt(getContentResolver(), Settings.System.TIME_12_24);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
Log.i(TAG,"Time format: "+time_format);
time_format will have 12 or 24 based on the settings.
Make sure you have added this permission to manifiest file.
<user-permission android:name="android.permission.WRITE_SETTINGS" />
EDIT :
You can also use DateFormat.is24HourFormat(). It doesn't require an extra permission.
The simple way would be to use to use android native android.text.format.DateFormat class's is24HourFormat(this) static method as below.
if (android.text.format.DateFormat.is24HourFormat(YourActivityName.this)) {
// 24 hour format
}
else
{
// 12 hour format
}
check, http://developer.android.com/reference/android/text/format/DateFormat.html
DateFormat::getDateFormat()
Date date = new Date(location.getTime());
dateFormat.format(date);
Please check this...
http://developer.android.com/reference/java/text/SimpleDateFormat.html
How do you format date and time in Android?
24 hour clock not working in dateformat android