Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Android's date/time programmatically

I need set the date/time from Android programmatically, but I'm not having success! I have these three sources above:

Source code 1

AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);  
alarm.setTime(1330082817000); 

AndroidManifest:

<uses-permission android:name="android.permission.SET_TIME" />  
<uses-permission android:name="android.permission.SET_TIME_ZONE" />  

Exception:

Service fatal error : Unable to start activity ComponentInfo{br.com.tdta.service/br.com.tdta.service.Service}: java.lang.SecurityException: setTime: Neither user 10038 nor current process has android.permission.SET_TIME.  
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.tdta.service/br.com.tdta.service.Service}: java.lang.SecurityException: setTime: Neither user 10038 nor current process has android.permission.SET_TIME.  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)  
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)  
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)  
    at android.os.Handler.dispatchMessage(Handler.java:99)  
    at android.os.Looper.loop(Looper.java:123)  
    at android.app.ActivityThread.main(ActivityThread.java:4627)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:521)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)  
    at dalvik.system.NativeStart.main(Native Method)  
Caused by: java.lang.SecurityException: setTime: Neither user 10038 nor current process has android.permission.SET_TIME.  
    at android.os.Parcel.readException(Parcel.java:1247)  
    at android.os.Parcel.readException(Parcel.java:1235)  
    at android.app.IAlarmManager$Stub$Proxy.setTime(IAlarmManager.java:237)  
    at android.app.AlarmManager.setTime(AlarmManager.java:289)  
    at br.com.tdta.service.Service.onCreate(Service.java:32)  
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)  
... 11 more  

====

Source code 2

boolean itsWork = SystemClock.setCurrentTimeMillis(1330082817000);  
System.out.println(itsWork);  

Manifest:

<uses-permission android:name="android.permission.SET_TIME" />  
<uses-permission android:name="android.permission.SET_TIME_ZONE" />  

itsWork value:

false  

====

Source code 3

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd.hhmmss");
String data = format.format(new Date(1330082817000));
try {
    Runtime.getRuntime().exec("date -s " + data);
} catch (IOException e) {
}

What am I doing wrong?

Thanks in advance!

like image 437
eliangela Avatar asked Feb 24 '12 16:02

eliangela


People also ask

How to set time in android programmatically?

For example, to set the time to 2013/08/15 12:34:56, you could do: Calendar c = Calendar. getInstance(); c. set(2013, 8, 15, 12, 34, 56); AlarmManager am = (AlarmManager) this.

How do I set the date and time in Android Studio?

You can use the SimpleDateFormat class for formatting date in your desired format. Just check this link where you get an idea for your example. For example: String dateStr = "04/05/2010"; SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); Date dateObj = curFormater.

What is date and time in android?

Date and Time in Android are formatted using the SimpleDateFormat library from Java, using Calendar instance which helps to get the current system date and time. The current date and time are of the type Long which can be converted to a human-readable date and time.


1 Answers

The user application does not have permission to change the device time. Please read the answer by cashbash in the following post for the alternate option.

Copying here for quick reference:

According to this thread, user apps cannot set the time, regardless of the permissions we give it. Instead, the best approach is to make the user set the time manually. We will use:

startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));

Unfortunately, there is no way to link them directly to the time setting (which would save them one more click). By making use of ellapsedRealtime, we can ensure that the user sets the time correctly.

like image 56
Praful Bhatnagar Avatar answered Sep 22 '22 13:09

Praful Bhatnagar