Kotlin で android アプリを作ってみた

4,665 views

Published on

Published in: Business, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,665
On SlideShare
0
From Embeds
0
Number of Embeds
2,677
Actions
Shares
0
Downloads
5
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Kotlin で android アプリを作ってみた

  1. 1. Kotlin で Android アプリを作ってみた bina1204 1
  2. 2. アジェンダ • 自己紹介 • Kotlin で Android アプリを作ってみた • Java と Kotlin の比較 • まとめ 2
  3. 3. 自己紹介 3
  4. 4. プロフィール びな @bina1204 http://gsbina.com 4
  5. 5. Kotlin 初心者です 5
  6. 6. Kotlin で Android アプリを作ってみた 6
  7. 7. Kotlin を覚える Kotlin Advent Calendar 2012 (全部俺) : ATND http://atnd.org/events/34627 7
  8. 8. Kotlin の Android アプリ開発の準備 8.2 Androidアプリ開発 - プログラミング言語Kotlin 解説 https://sites.google.com/site/tarokotlin/chap8/sec82 8
  9. 9. アプリを作ってみた タップすると スリープする 9
  10. 10. アプリを作ってみた 10
  11. 11. Java と Kotlin の比較 11
  12. 12. AdminReceiver.Java package com.gsbina.android.sleepnow; import android.app.admin.DeviceAdminReceiver; public class AdminReceiver extends DeviceAdminReceiver { } 12
  13. 13. AdminReceiver.kt package com.gsbina.android.sleepnow import android.app.admin.DeviceAdminReceiver public class AdminReceiver: DeviceAdminReceiver() { } 13
  14. 14. CreateShortcutActivity.Java @Override protected void onResume() { super.onResume(); final Intent sleepIntent = new Intent(PhoneSleepActivity.ACTION_SLEEP); final Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sleepIntent); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Sleep Now"); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, R.drawable.ic_launcher); sendBroadcast(shortcutIntent); finish(); } 14
  15. 15. CreateShortcutActivity.Kt protected override fun onResume() { super<Activity>.onResume() val sleepIntent = Intent(PhoneSleepActivity.ACTION_SLEEP) val shortcutIntent = Intent("com.android.launcher.action.INSTALL_SHORTCUT") shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sleepIntent) shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Sleep Now"); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, R.drawable.ic_launcher) sendBroadcast(shortcutIntent) finish() } 15
  16. 16. PhoneSleepActivity.java @Override protected void onResume() { super.onResume(); final DevicePolicyManager dpm = (DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE); if (dpm.isAdminActive(new AdminReceiver().getWho(this))) { // スリープする dpm.lockNow(); } else { Toast.makeText(this, "Cannot sleep", Toast.LENGTH_SHORT).show(); startActivity(new Intent(this, MainActivity.class)); } finish(); } public static final String ACTION_SLEEP = "com.gsbina.android.sleepnow.action.SLEEP"; 16
  17. 17. PhoneSleepActivity.Kt protected override fun onResume() { super<Activity>.onResume() val dpm = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager if (dpm.isAdminActive(AdminReceiver().getWho(this))) { // スリープする dpm.lockNow(); } else { Toast.makeText(this, "Cannot sleep", Toast.LENGTH_SHORT)?.show() startActivity(Intent(this, javaClass<MainActivity>())) } finish() } class object { val ACTION_SLEEP = "com.gsbina.android.sleepnow.action.SLEEP" } 17
  18. 18. MainActivity.java private final MainActivity mSelf = this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setUpView(); } private DevicePolicyManager mDevicePolicyManager; private void setUpView() { findViewById(R.id.device_admin).setOnClickListener(new OnClickDeviceAdmin()); findViewById(R.id.btn_create_shortcut).setOnClickListener(new OnClickCreateShortcut()); findViewById(R.id.btn_display_notification).setOnClickListener(new OnClickDisplayNotification()); findViewById(R.id.btn_hide_notification).setOnClickListener(new OnClickHideNotification()); findViewById(R.id.btn_sleep).setOnClickListener(new OnClickSleep()); findViewById(R.id.btn_uninstall).setOnClickListener(new OnClickUninstall()); } 18
  19. 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. 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. 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
  22. 22. APK ファイルの比較 22
  23. 23. サイズ Java 約 229KB Kotlin 約 467KB 23
  24. 24. classes.dex Java android com Kotlin android com jet kotlin org 24
  25. 25. AdminReceiver.class(Java) package com.gsbina.android.sleepnow; import android.app.admin.DeviceAdminReceiver; public class AdminReceiver extends DeviceAdminReceiver { public AdminReceiver() { } } 25
  26. 26. AdminReceiver.class(Kotlin) package com.gsbina.android.sleepnow; import android.app.admin.DeviceAdminReceiver; import jet.JetObject; public final class AdminReceiver extends DeviceAdminReceiver implements JetObject { public AdminReceiver() { } } 26
  27. 27. CreateShortcutActivity.class(Java) protected void onResume() { super.onResume(); Intent intent = new Intent("com.gsbina.android.sleepnow.action.SLEEP"); Intent intent1 = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); Intent intent2 = intent1.putExtra("android.intent.extra.shortcut.INTENT", intent); Intent intent3 = intent1.putExtra("android.intent.extra.shortcut.NAME", "Sleep Now"); Intent intent4 = intent1.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", 0x7f020000); sendBroadcast(intent1); finish(); } 27
  28. 28. CreateShortcutActivity.class(Kotlin) protected void onResume() { super.onResume(); String s = PhoneSleepActivity.object$.getACTION_SLEEP(); Intent intent = new Intent(s); Intent intent1 = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); String s1 = Intent.EXTRA_SHORTCUT_INTENT; Intrinsics.checkFieldIsNotNull(s1, "Intent", "EXTRA_SHORTCUT_INTENT"); Parcelable parcelable = (Parcelable)intent; Intent intent2 = intent1.putExtra(s1, parcelable); String s2 = Intent.EXTRA_SHORTCUT_NAME; Intrinsics.checkFieldIsNotNull(s2, "Intent", "EXTRA_SHORTCUT_NAME"); Intent intent3 = intent1.putExtra(s2, "Sleep Now"); String s3 = Intent.EXTRA_SHORTCUT_ICON_RESOURCE; Intrinsics.checkFieldIsNotNull(s3, "Intent", "EXTRA_SHORTCUT_ICON_RESOURCE"); int i = R.drawable.ic_launcher; Intent intent4 = intent1.putExtra(s3, i); sendBroadcast(intent1); finish(); } 28
  29. 29. PhoneSleepActivity.class(Java) protected void onResume() { super.onResume(); DevicePolicyManager devicepolicymanager = (DevicePolicyManager)getSystemService("device_policy"); android.content.ComponentName componentname = (new AdminReceiver()).getWho(this); if(devicepolicymanager.isAdminActive(componentname) { devicepolicymanager.lockNow(); } else { Toast.makeText(this, "Cannot sleep", 0).show(); Intent intent = new Intent(this, com/gsbina/android/sleepnow/MainActivity); startActivity(intent); } finish(); } public static final String ACTION_SLEEP = "com.gsbina.android.sleepnow.action.SLEEP"; 29
  30. 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. 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
  32. 32. まとめ 32
  33. 33. まとめ 33 final Intent intent = new Intent(this, MainActivity.class); val intent = Intent(this, javaClass<MainActivity>());
  34. 34. まとめ 34 new Runnable() { @Override public void run() { // 処理 } } Runnable { // 処理 }
  35. 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" }
  36. 36. まとめ コーディングの量が減る 36 Java Kotlin MainActivity 3773 3395 AdminReceiver 131 125 CreateShortcutActivity 594 566 PhoneSleepActivity 631 635 [文字数]

×