0
votes

In a View class, I override onFinishInflate() to invoke setupEventHandlers() which defines some listeners. For example:

setDate.setOnClickListener( new OnSingleClick() {
    @Override
    public void onSingleClick( final View v ) {
            controller.onSetDate();
        }
    }
} );

In controller.onSetDate() I have the command:

SystemClock.setCurrentTimeMillis(dateTime.getMillis());

(dateTime is a DateTime Object)

After this method is called, the system kind of freezes. The listeners stop working. Nothing happens when I click the attached buttons (also checked in debug mode - it doesn't enter the listener when a button is clicked). Only pressing back (on the android device itself) works, and then the system returns to normal again (it recognizes the listeners).

Why is this happening?

EDIT:

I'm working on a rooted device, and all the permissions are set.

Now my code looks like this:

AlarmManager alarm = (AlarmManager) MainApplication.getAppContext().getSystemService( Context.ALARM_SERVICE );
long time = dateTime.getMillis();
DateTimeZone zone = dateTime.getZone();
alarm.setTime( time );
alarm.setTimeZone( zone.toTimeZone().getID() );
TimeZone.setDefault( zone.toTimeZone() );
DateTimeZone.setDefault( zone );

The problem occurs only when I try to set the time/date backwards. Setting it forward works fine.

2
Since that method "requires the calling process to have appropriate permissions", my guess is that your app lacks those permissions.CommonsWare
It doesn't. Changing the time works. I run "chmod 666 /dev/alarm" before calling SystemClock.setCurrentTimeMillis()Alaa M.

2 Answers

2
votes

The javadoc about SystemClock.setCurrentTimeMillis indicates that your app must have the appropriate permission. A quick look into the code shows that SystemClock.setCurrentTimeMillis is finally calling AlarmManager.setTime which requires the permission : android.permission.SET_TIME

You should add the permission in your manifest :

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

Note that this a System level permission, so your app must be signed with the appropriate signature (system) otherwise the permission won't be granted.

You can check that the permission is granted with :

String permission = "android.permission.SET_TIME";
int res = context.checkCallingOrSelfPermission(permission);
boolean permissionSetTimeGranted = (res == PackageManager.PERMISSION_GRANTED);

Remark about your comment : I don't see why running a chmod 666 /dev/alarm would change anything (except that it is probably why your app is freezing instead of simply returning false)

1
votes

The solution is:

re-inflate the view after setting the date/time:

dateAndTimeView = ( DateAndTimeSetupView ) View.inflate( DateAndTimeSetupController.this, R.layout.date_and_time_setup, null );
setContentView( dateAndTimeView );