I realise this question appears elsewhere on SO, however I've tried all solutions that I can find on the site and am still having difficulty. These questions (and their answers) are also several years old, and rather than commenting on old questions, I thought I'd consolidate information here for others and investigate potentially related changes to Android between then and now. Still, if you feel this question qualifies as a duplicate, mark it as such.
Edit: As I stated, other answers on SO have not solved the problem. I don't see how commenting on each and every potential fix with "Doesn't work for me" is conducive to helping myself and others solve the problem, but if that's the case, so be it. For reference, 3 people have marked the questions as a potential duplicate of this question, so I have added those non-working answers to the list below.
My application has some very specific needs, but for the sake of brevity, let's just assume I need to set the system time and date manually to a fixed value. Below I have detailed some notes about my progress in attempting to get this working.
The app has a minimum SDK version of, and is targeting, 24. Others found that they needed to increase the version to this level.
The device has been rooted, and the application is being installed to /system/app so that I can change the date/time.
Some have commented that it is outright impossible for apps to set the time, however looking at apps such as ClockSync, this does not seem to be the case. I'm aware that my app will not be available on the Play store.
The app must set the date/time automatically without user input, so using the following code to make a user set it will not help;
startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));
I added the set time permission to the manifest in two ways, on their own and then together. I also tried adding the part about removing the maxSDKversion.
<uses-permission android:name="android.permission.SET_TIME" tools:remove="android:maxSdkVersion" />
<permission android:name="android.permission.SET_TIME"
android:protectionLevel="signature|system"/>
I had read that certain permissions required not only a manifest entry, but for the user to accept and allow them. However, SET_TIME is not listed among these. I did try doing it anyway, however my permission request was automatically rejected without showing the confirmation box, probably because it did not actually require the permission in the first place.
The code I'm using to try and set the time is as follows:
try {
Calendar c = Calendar.getInstance();
c.set(2017, 11, 21, 12, 32, 30);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.setTime(c.getTimeInMillis());
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
The error I receive in a toast when this fails is:
java.lang.securityexception: setTime: neither user 10051 nor current process has android.permission.SET_TIME
I have tried the two solutions in the question referenced in my above edit, both of which involve running code to set the date as such:
Runtime.getRuntime().exec("su && date -s 20171122.153720");}
Both of those answers do nothing - the time does not change and no exceptions are thrown. The other answer requires internet access which is outside the scope of my question.
Considering all of the above, is there any key step I have missed in making this work, or anything in the code that would indicate a problem?