i have an android app that i write in c# by downloading xamarin tools (i love c# and i wanna code using this instead of writing it in java..hehe.). this app uses one thread only and needs to lock the phone when a button is clicked. however, i need to enable
the device as admin in order to do this. i found a code snippet that does this but this is written in java so i translated it to c#. however, when i run my app the device won't be enabled. i have search several answers in google but i still have not figured
out how to do this right. i posted already in xamarin forum but nobody responded to my queries. can anybody here help me find what is wrong? my code snippet is found below.
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Main); deviceManger = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService); activityManager = (ActivityManager)GetSystemService(Context.ActivityService); compName = new ComponentName(this, Java.Lang.Class.FromType(typeof(DeviceAppAdmin)).Name); //SetContentView(Resource.Layout.Main); btnlock = FindViewById <ImageButton> (Resource.Id.btnLockScreen); disable = FindViewById<Button>(Resource.Id.btnDisable); enable = FindViewById<Button>(Resource.Id.btnEnable); btnlock.SetOnClickListener(this); disable.SetOnClickListener(this); enable.SetOnClickListener(this); } public void OnClick(View v) { if(v == btnlock) { bool active = deviceManger.IsAdminActive(compName); if (active) { deviceManger.SetMaximumTimeToLock(compName,5000); //5sec delay before lock //deviceManger.LockNow(); } } if(v == enable) { Intent intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin); intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin,compName); intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "Allow app to lock device screen and change screen brightness."); StartActivityForResult(intent, RESULT_ENABLE); } } protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data) { switch (requestCode) { case RESULT_ENABLE: if (resultCode == Result.Ok) Android.Util.Log.Info("Device Admin", "Admin enabled!"); else Android.Util.Log.Info("Device Admin", "Admin enable FAILED!"); return; } base.OnActivityResult(requestCode,resultCode, data); }thanks.