19. MainActivity.kt
val mSelf = this
protected override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
setUpView()
}
var mDevicePolicyManager: DevicePolicyManager? = null
private fun setUpView() {
findViewById(R.id.device_admin)?.setOnClickListener(OnClickDeviceAdmin())
findViewById(R.id.btn_create_shortcut)?.setOnClickListener(OnClickCreateShortcut())
findViewById(R.id.btn_display_notification)?.setOnClickListener(OnClickDisplayNotification())
findViewById(R.id.btn_hide_notification)?.setOnClickListener(OnClickHideNotification())
findViewById(R.id.btn_sleep)?.setOnClickListener(OnClickSleep())
findViewById(R.id.btn_uninstall)?.setOnClickListener(OnClickUninstall())
}
19
20. MainActivity.java
private class OnClickDeviceAdmin implements OnClickListener {
@Override
public void onClick(View v) {
final Switch s = (Switch) v;
if (s.isChecked()) {
activateDeviceAdmin();
} else {
mDevicePolicyManager.removeActiveAdmin(new AdminReceiver().getWho(mSelf));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
updateView();
}
}, 250);
}
}
private void activateDeviceAdmin() {
final Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
final ComponentName name = new AdminReceiver().getWho(mSelf);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, name);
startActivity(intent);
}
}
20
21. MainActivity.kt
inner class OnClickDeviceAdmin(): OnClickListener {
public override fun onClick(v: View?) {
val s = v as Switch
if (s.isChecked()) {
activateDeviceAdmin()
} else {
mDevicePolicyManager?.removeActiveAdmin(AdminReceiver().getWho(mSelf))
Handler().postDelayed(Runnable {
updateView()
}, 250)
}
}
private fun activateDeviceAdmin() {
val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
val name = AdminReceiver().getWho(mSelf)
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, name)
startActivity(intent)
}
}
21
30. PhoneSleepActivity.class(Kotlin)
protected void onResume()
{
super.onResume();
String s = Context.DEVICE_POLICY_SERVICE;
Intrinsics.checkFieldIsNotNull(s, "Context", "DEVICE_POLICY_SERVICE");
Object obj = getSystemService(s);
if(obj == null)
throw new TypeCastException("jet.Any? cannot be cast to android.app.admin.DevicePolicyManager");
DevicePolicyManager devicepolicymanager = (DevicePolicyManager)obj;
android.content.ComponentName componentname = (new AdminReceiver()).getWho(this);
if(devicepolicymanager.isAdminActive(componentname))
{
devicepolicymanager.lockNow();
} else
{
Context context = getApplicationContext();
CharSequence charsequence = (CharSequence)"Cannot sleep";
int i = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, charsequence, i);
if(toast != null)
toast.show();
Intent intent = new Intent(this, com/gsbina/android/sleepnow/MainActivity);
startActivity(intent);
}
finish();
}
30
31. PhoneSleepActivity.class(Kotlin)
public static final class object
implements JetObject
{
public final String getACTION_SLEEP()
{
return ACTION_SLEEP;
}
private final String ACTION_SLEEP = "com.gsbina.android.sleepnow.action.SLEEP";
object()
{
}
}
public static final object object$ = new object();
31
35. まとめ(定数)
35
public static final String ACTION_SLEEP =
"com.gsbina.android.sleepnow.action.SLEEP";
class object {
val ACTION_SLEEP = "com.gsbina.android.sleepnow.action.SLEEP"
}