SlideShare a Scribd company logo
Kotlin で Android アプリを作ってみた
bina1204
1
アジェンダ
• 自己紹介
• Kotlin で Android アプリを作ってみた
• Java と Kotlin の比較
• まとめ
2
自己紹介
3
プロフィール
びな
@bina1204
http://gsbina.com
4
Kotlin 初心者です
5
Kotlin で Android アプリを作ってみた
6
Kotlin を覚える
Kotlin Advent Calendar 2012 (全部俺) : ATND
http://atnd.org/events/34627
7
Kotlin の Android アプリ開発の準備
8.2 Androidアプリ開発 - プログラミング言語Kotlin
解説
https://sites.google.com/site/tarokotlin/chap8/sec82
8
アプリを作ってみた
タップすると
スリープする
9
アプリを作ってみた
10
Java と Kotlin の比較
11
AdminReceiver.Java
package com.gsbina.android.sleepnow;
import android.app.admin.DeviceAdminReceiver;
public class AdminReceiver extends DeviceAdminReceiver {
}
12
AdminReceiver.kt
package com.gsbina.android.sleepnow
import android.app.admin.DeviceAdminReceiver
public class AdminReceiver: DeviceAdminReceiver() {
}
13
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
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
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
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
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
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
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
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
APK ファイルの比較
22
サイズ
Java
約 229KB
Kotlin
約 467KB
23
classes.dex
Java
android
com
Kotlin
android
com
jet
kotlin
org
24
AdminReceiver.class(Java)
package com.gsbina.android.sleepnow;
import android.app.admin.DeviceAdminReceiver;
public class AdminReceiver extends DeviceAdminReceiver
{
public AdminReceiver()
{
}
}
25
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
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
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
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
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
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
まとめ
33
final Intent intent = new Intent(this, MainActivity.class);
val intent = Intent(this, javaClass<MainActivity>());
まとめ
34
new Runnable() {
@Override
public void run() {
// 処理
}
}
Runnable {
// 処理
}
まとめ(定数)
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
Java Kotlin
MainActivity 3773 3395
AdminReceiver 131 125
CreateShortcutActivity 594 566
PhoneSleepActivity 631 635
[文字数]

More Related Content

What's hot

Gradle - the Enterprise Automation Tool
Gradle  - the Enterprise Automation ToolGradle  - the Enterprise Automation Tool
Gradle - the Enterprise Automation Tool
Izzet Mustafaiev
 
react.pdf
react.pdfreact.pdf
react.pdf
yihunie2
 
0900 learning-react
0900 learning-react0900 learning-react
0900 learning-react
RohitYadav696
 
Exploring the GitHub Service Universe
Exploring the GitHub Service UniverseExploring the GitHub Service Universe
Exploring the GitHub Service Universe
Björn Kimminich
 
Continuous Delivery NYC: From GitOps to an adaptable CI/CD Pattern for Kubern...
Continuous Delivery NYC: From GitOps to an adaptable CI/CD Pattern for Kubern...Continuous Delivery NYC: From GitOps to an adaptable CI/CD Pattern for Kubern...
Continuous Delivery NYC: From GitOps to an adaptable CI/CD Pattern for Kubern...
Andrew Phillips
 
Spring Projects Infrastructure
Spring Projects InfrastructureSpring Projects Infrastructure
Spring Projects Infrastructure
Gunnar Hillert
 
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
ZeroTurnaround
 
Your own full blown Gerrit plugin
Your own full blown Gerrit pluginYour own full blown Gerrit plugin
Your own full blown Gerrit plugin
Dariusz Łuksza
 
Kubernetes GitOps featuring GitHub, Kustomize and ArgoCD
Kubernetes GitOps featuring GitHub, Kustomize and ArgoCDKubernetes GitOps featuring GitHub, Kustomize and ArgoCD
Kubernetes GitOps featuring GitHub, Kustomize and ArgoCD
Sunnyvale
 
GitOps Toolkit (Cloud Native Nordics Tech Talk)
GitOps Toolkit (Cloud Native Nordics Tech Talk)GitOps Toolkit (Cloud Native Nordics Tech Talk)
GitOps Toolkit (Cloud Native Nordics Tech Talk)
Weaveworks
 
Extending Java EE with CDI and JBoss Forge
Extending Java EE with CDI and JBoss ForgeExtending Java EE with CDI and JBoss Forge
Extending Java EE with CDI and JBoss Forge
Antoine Sabot-Durand
 
Making the Most of Your Gradle Builds
Making the Most of Your Gradle BuildsMaking the Most of Your Gradle Builds
Making the Most of Your Gradle Builds
Egor Andreevich
 
What’s new in grails framework 5?
What’s new in grails framework 5?What’s new in grails framework 5?
What’s new in grails framework 5?
Puneet Behl
 
Assign, Commit, and Review
Assign, Commit, and ReviewAssign, Commit, and Review
Assign, Commit, and Review
Zhongyue Luo
 
Gradle how to's
Gradle how to'sGradle how to's
Gradle how to's
Sergiy Beley
 
Cloud nativemicroservices jax-london2020
Cloud nativemicroservices   jax-london2020Cloud nativemicroservices   jax-london2020
Cloud nativemicroservices jax-london2020
Emily Jiang
 
Tips & Tricks for Maven Tycho
Tips & Tricks for Maven TychoTips & Tricks for Maven Tycho
Tips & Tricks for Maven Tycho
Gunnar Wagenknecht
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
Michael Vorburger
 
Effective Development With Eclipse Mylyn, Git, Gerrit and Hudson
Effective Development With Eclipse Mylyn, Git, Gerrit and HudsonEffective Development With Eclipse Mylyn, Git, Gerrit and Hudson
Effective Development With Eclipse Mylyn, Git, Gerrit and Hudson
Chris Aniszczyk
 
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Building  android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...Building  android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Hammad Tariq
 

What's hot (20)

Gradle - the Enterprise Automation Tool
Gradle  - the Enterprise Automation ToolGradle  - the Enterprise Automation Tool
Gradle - the Enterprise Automation Tool
 
react.pdf
react.pdfreact.pdf
react.pdf
 
0900 learning-react
0900 learning-react0900 learning-react
0900 learning-react
 
Exploring the GitHub Service Universe
Exploring the GitHub Service UniverseExploring the GitHub Service Universe
Exploring the GitHub Service Universe
 
Continuous Delivery NYC: From GitOps to an adaptable CI/CD Pattern for Kubern...
Continuous Delivery NYC: From GitOps to an adaptable CI/CD Pattern for Kubern...Continuous Delivery NYC: From GitOps to an adaptable CI/CD Pattern for Kubern...
Continuous Delivery NYC: From GitOps to an adaptable CI/CD Pattern for Kubern...
 
Spring Projects Infrastructure
Spring Projects InfrastructureSpring Projects Infrastructure
Spring Projects Infrastructure
 
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
 
Your own full blown Gerrit plugin
Your own full blown Gerrit pluginYour own full blown Gerrit plugin
Your own full blown Gerrit plugin
 
Kubernetes GitOps featuring GitHub, Kustomize and ArgoCD
Kubernetes GitOps featuring GitHub, Kustomize and ArgoCDKubernetes GitOps featuring GitHub, Kustomize and ArgoCD
Kubernetes GitOps featuring GitHub, Kustomize and ArgoCD
 
GitOps Toolkit (Cloud Native Nordics Tech Talk)
GitOps Toolkit (Cloud Native Nordics Tech Talk)GitOps Toolkit (Cloud Native Nordics Tech Talk)
GitOps Toolkit (Cloud Native Nordics Tech Talk)
 
Extending Java EE with CDI and JBoss Forge
Extending Java EE with CDI and JBoss ForgeExtending Java EE with CDI and JBoss Forge
Extending Java EE with CDI and JBoss Forge
 
Making the Most of Your Gradle Builds
Making the Most of Your Gradle BuildsMaking the Most of Your Gradle Builds
Making the Most of Your Gradle Builds
 
What’s new in grails framework 5?
What’s new in grails framework 5?What’s new in grails framework 5?
What’s new in grails framework 5?
 
Assign, Commit, and Review
Assign, Commit, and ReviewAssign, Commit, and Review
Assign, Commit, and Review
 
Gradle how to's
Gradle how to'sGradle how to's
Gradle how to's
 
Cloud nativemicroservices jax-london2020
Cloud nativemicroservices   jax-london2020Cloud nativemicroservices   jax-london2020
Cloud nativemicroservices jax-london2020
 
Tips & Tricks for Maven Tycho
Tips & Tricks for Maven TychoTips & Tricks for Maven Tycho
Tips & Tricks for Maven Tycho
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
Effective Development With Eclipse Mylyn, Git, Gerrit and Hudson
Effective Development With Eclipse Mylyn, Git, Gerrit and HudsonEffective Development With Eclipse Mylyn, Git, Gerrit and Hudson
Effective Development With Eclipse Mylyn, Git, Gerrit and Hudson
 
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Building  android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...Building  android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
 

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

Hacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdfHacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdf
ShaiAlmog1
 
Level Up Your Android Build -Droidcon Berlin 2015
Level Up Your Android Build -Droidcon Berlin 2015Level Up Your Android Build -Droidcon Berlin 2015
Level Up Your Android Build -Droidcon Berlin 2015
Friedger Müffke
 
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Somkiat Khitwongwattana
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginMastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Xavier Hallade
 
Android OS & SDK - Getting Started
Android OS & SDK - Getting StartedAndroid OS & SDK - Getting Started
Android OS & SDK - Getting Started
Hemant Chhapoliya
 
Hacking the Codename One Source Code - Part IV.pdf
Hacking the Codename One Source Code - Part IV.pdfHacking the Codename One Source Code - Part IV.pdf
Hacking the Codename One Source Code - Part IV.pdf
ShaiAlmog1
 
The Ring programming language version 1.9 book - Part 81 of 210
The Ring programming language version 1.9 book - Part 81 of 210The Ring programming language version 1.9 book - Part 81 of 210
The Ring programming language version 1.9 book - Part 81 of 210
Mahmoud Samir Fayed
 
Loopback: An Easy and Robust Mobile Backend - Michael Hantler & Aviv Callande...
Loopback: An Easy and Robust Mobile Backend - Michael Hantler & Aviv Callande...Loopback: An Easy and Robust Mobile Backend - Michael Hantler & Aviv Callande...
Loopback: An Easy and Robust Mobile Backend - Michael Hantler & Aviv Callande...
Codemotion Tel Aviv
 
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Nicolas HAAN
 
Native Android Development Practices
Native Android Development PracticesNative Android Development Practices
Native Android Development Practices
Roy Clarkson
 
Gradle presentation
Gradle presentationGradle presentation
Gradle presentation
Oriol Jiménez
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
Visual Engineering
 
GR8Conf 2011: Grails, how to plug in
GR8Conf 2011: Grails, how to plug inGR8Conf 2011: Grails, how to plug in
GR8Conf 2011: Grails, how to plug inGR8Conf
 
Dependency Injections in Kotlin
Dependency Injections in KotlinDependency Injections in Kotlin
Dependency Injections in Kotlin
EatDog
 
Lesson 5 Layouts.pptx
Lesson 5 Layouts.pptxLesson 5 Layouts.pptx
Lesson 5 Layouts.pptx
TempTemp63
 
Android Layouts
Android LayoutsAndroid Layouts
Android Layouts
ADRIANASAAGCAOILI
 
CI/CD on Android project via Jenkins Pipeline
CI/CD on Android project via Jenkins PipelineCI/CD on Android project via Jenkins Pipeline
CI/CD on Android project via Jenkins Pipeline
Veaceslav Gaidarji
 
Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!
Roberto Franchini
 
Gitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a proGitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a pro
sparkfabrik
 
Js tacktalk team dev js testing performance
Js tacktalk team dev js testing performanceJs tacktalk team dev js testing performance
Js tacktalk team dev js testing performance
Артем Захарченко
 

Similar to Kotlin で android アプリを作ってみた (20)

Hacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdfHacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdf
 
Level Up Your Android Build -Droidcon Berlin 2015
Level Up Your Android Build -Droidcon Berlin 2015Level Up Your Android Build -Droidcon Berlin 2015
Level Up Your Android Build -Droidcon Berlin 2015
 
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginMastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
 
Android OS & SDK - Getting Started
Android OS & SDK - Getting StartedAndroid OS & SDK - Getting Started
Android OS & SDK - Getting Started
 
Hacking the Codename One Source Code - Part IV.pdf
Hacking the Codename One Source Code - Part IV.pdfHacking the Codename One Source Code - Part IV.pdf
Hacking the Codename One Source Code - Part IV.pdf
 
The Ring programming language version 1.9 book - Part 81 of 210
The Ring programming language version 1.9 book - Part 81 of 210The Ring programming language version 1.9 book - Part 81 of 210
The Ring programming language version 1.9 book - Part 81 of 210
 
Loopback: An Easy and Robust Mobile Backend - Michael Hantler & Aviv Callande...
Loopback: An Easy and Robust Mobile Backend - Michael Hantler & Aviv Callande...Loopback: An Easy and Robust Mobile Backend - Michael Hantler & Aviv Callande...
Loopback: An Easy and Robust Mobile Backend - Michael Hantler & Aviv Callande...
 
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
 
Native Android Development Practices
Native Android Development PracticesNative Android Development Practices
Native Android Development Practices
 
Gradle presentation
Gradle presentationGradle presentation
Gradle presentation
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 
GR8Conf 2011: Grails, how to plug in
GR8Conf 2011: Grails, how to plug inGR8Conf 2011: Grails, how to plug in
GR8Conf 2011: Grails, how to plug in
 
Dependency Injections in Kotlin
Dependency Injections in KotlinDependency Injections in Kotlin
Dependency Injections in Kotlin
 
Lesson 5 Layouts.pptx
Lesson 5 Layouts.pptxLesson 5 Layouts.pptx
Lesson 5 Layouts.pptx
 
Android Layouts
Android LayoutsAndroid Layouts
Android Layouts
 
CI/CD on Android project via Jenkins Pipeline
CI/CD on Android project via Jenkins PipelineCI/CD on Android project via Jenkins Pipeline
CI/CD on Android project via Jenkins Pipeline
 
Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!
 
Gitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a proGitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a pro
 
Js tacktalk team dev js testing performance
Js tacktalk team dev js testing performanceJs tacktalk team dev js testing performance
Js tacktalk team dev js testing performance
 

Recently uploaded

ModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdfModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
fisherameliaisabella
 
Project File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdfProject File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdf
RajPriye
 
Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)
Lviv Startup Club
 
Introduction to Amazon company 111111111111
Introduction to Amazon company 111111111111Introduction to Amazon company 111111111111
Introduction to Amazon company 111111111111
zoyaansari11365
 
Cree_Rey_BrandIdentityKit.PDF_PersonalBd
Cree_Rey_BrandIdentityKit.PDF_PersonalBdCree_Rey_BrandIdentityKit.PDF_PersonalBd
Cree_Rey_BrandIdentityKit.PDF_PersonalBd
creerey
 
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdfikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
agatadrynko
 
An introduction to the cryptocurrency investment platform Binance Savings.
An introduction to the cryptocurrency investment platform Binance Savings.An introduction to the cryptocurrency investment platform Binance Savings.
An introduction to the cryptocurrency investment platform Binance Savings.
Any kyc Account
 
Authentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto RicoAuthentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto Rico
Corey Perlman, Social Media Speaker and Consultant
 
Improving profitability for small business
Improving profitability for small businessImproving profitability for small business
Improving profitability for small business
Ben Wann
 
LA HUG - Video Testimonials with Chynna Morgan - June 2024
LA HUG - Video Testimonials with Chynna Morgan - June 2024LA HUG - Video Testimonials with Chynna Morgan - June 2024
LA HUG - Video Testimonials with Chynna Morgan - June 2024
Lital Barkan
 
Company Valuation webinar series - Tuesday, 4 June 2024
Company Valuation webinar series - Tuesday, 4 June 2024Company Valuation webinar series - Tuesday, 4 June 2024
Company Valuation webinar series - Tuesday, 4 June 2024
FelixPerez547899
 
Cracking the Workplace Discipline Code Main.pptx
Cracking the Workplace Discipline Code Main.pptxCracking the Workplace Discipline Code Main.pptx
Cracking the Workplace Discipline Code Main.pptx
Workforce Group
 
Set off and carry forward of losses and assessment of individuals.pptx
Set off and carry forward of losses and assessment of individuals.pptxSet off and carry forward of losses and assessment of individuals.pptx
Set off and carry forward of losses and assessment of individuals.pptx
HARSHITHV26
 
Premium MEAN Stack Development Solutions for Modern Businesses
Premium MEAN Stack Development Solutions for Modern BusinessesPremium MEAN Stack Development Solutions for Modern Businesses
Premium MEAN Stack Development Solutions for Modern Businesses
SynapseIndia
 
Buy Verified PayPal Account | Buy Google 5 Star Reviews
Buy Verified PayPal Account | Buy Google 5 Star ReviewsBuy Verified PayPal Account | Buy Google 5 Star Reviews
Buy Verified PayPal Account | Buy Google 5 Star Reviews
usawebmarket
 
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Lviv Startup Club
 
The Influence of Marketing Strategy and Market Competition on Business Perfor...
The Influence of Marketing Strategy and Market Competition on Business Perfor...The Influence of Marketing Strategy and Market Competition on Business Perfor...
The Influence of Marketing Strategy and Market Competition on Business Perfor...
Adam Smith
 
Meas_Dylan_DMBS_PB1_2024-05XX_Revised.pdf
Meas_Dylan_DMBS_PB1_2024-05XX_Revised.pdfMeas_Dylan_DMBS_PB1_2024-05XX_Revised.pdf
Meas_Dylan_DMBS_PB1_2024-05XX_Revised.pdf
dylandmeas
 
Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptx
Cynthia Clay
 
FINAL PRESENTATION.pptx12143241324134134
FINAL PRESENTATION.pptx12143241324134134FINAL PRESENTATION.pptx12143241324134134
FINAL PRESENTATION.pptx12143241324134134
LR1709MUSIC
 

Recently uploaded (20)

ModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdfModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
 
Project File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdfProject File Report BBA 6th semester.pdf
Project File Report BBA 6th semester.pdf
 
Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)Maksym Vyshnivetskyi: PMO Quality Management (UA)
Maksym Vyshnivetskyi: PMO Quality Management (UA)
 
Introduction to Amazon company 111111111111
Introduction to Amazon company 111111111111Introduction to Amazon company 111111111111
Introduction to Amazon company 111111111111
 
Cree_Rey_BrandIdentityKit.PDF_PersonalBd
Cree_Rey_BrandIdentityKit.PDF_PersonalBdCree_Rey_BrandIdentityKit.PDF_PersonalBd
Cree_Rey_BrandIdentityKit.PDF_PersonalBd
 
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdfikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
 
An introduction to the cryptocurrency investment platform Binance Savings.
An introduction to the cryptocurrency investment platform Binance Savings.An introduction to the cryptocurrency investment platform Binance Savings.
An introduction to the cryptocurrency investment platform Binance Savings.
 
Authentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto RicoAuthentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto Rico
 
Improving profitability for small business
Improving profitability for small businessImproving profitability for small business
Improving profitability for small business
 
LA HUG - Video Testimonials with Chynna Morgan - June 2024
LA HUG - Video Testimonials with Chynna Morgan - June 2024LA HUG - Video Testimonials with Chynna Morgan - June 2024
LA HUG - Video Testimonials with Chynna Morgan - June 2024
 
Company Valuation webinar series - Tuesday, 4 June 2024
Company Valuation webinar series - Tuesday, 4 June 2024Company Valuation webinar series - Tuesday, 4 June 2024
Company Valuation webinar series - Tuesday, 4 June 2024
 
Cracking the Workplace Discipline Code Main.pptx
Cracking the Workplace Discipline Code Main.pptxCracking the Workplace Discipline Code Main.pptx
Cracking the Workplace Discipline Code Main.pptx
 
Set off and carry forward of losses and assessment of individuals.pptx
Set off and carry forward of losses and assessment of individuals.pptxSet off and carry forward of losses and assessment of individuals.pptx
Set off and carry forward of losses and assessment of individuals.pptx
 
Premium MEAN Stack Development Solutions for Modern Businesses
Premium MEAN Stack Development Solutions for Modern BusinessesPremium MEAN Stack Development Solutions for Modern Businesses
Premium MEAN Stack Development Solutions for Modern Businesses
 
Buy Verified PayPal Account | Buy Google 5 Star Reviews
Buy Verified PayPal Account | Buy Google 5 Star ReviewsBuy Verified PayPal Account | Buy Google 5 Star Reviews
Buy Verified PayPal Account | Buy Google 5 Star Reviews
 
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
Evgen Osmak: Methods of key project parameters estimation: from the shaman-in...
 
The Influence of Marketing Strategy and Market Competition on Business Perfor...
The Influence of Marketing Strategy and Market Competition on Business Perfor...The Influence of Marketing Strategy and Market Competition on Business Perfor...
The Influence of Marketing Strategy and Market Competition on Business Perfor...
 
Meas_Dylan_DMBS_PB1_2024-05XX_Revised.pdf
Meas_Dylan_DMBS_PB1_2024-05XX_Revised.pdfMeas_Dylan_DMBS_PB1_2024-05XX_Revised.pdf
Meas_Dylan_DMBS_PB1_2024-05XX_Revised.pdf
 
Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptx
 
FINAL PRESENTATION.pptx12143241324134134
FINAL PRESENTATION.pptx12143241324134134FINAL PRESENTATION.pptx12143241324134134
FINAL PRESENTATION.pptx12143241324134134
 

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

  • 1. Kotlin で Android アプリを作ってみた bina1204 1
  • 2. アジェンダ • 自己紹介 • Kotlin で Android アプリを作ってみた • Java と Kotlin の比較 • まとめ 2
  • 6. Kotlin で Android アプリを作ってみた 6
  • 7. Kotlin を覚える Kotlin Advent Calendar 2012 (全部俺) : ATND http://atnd.org/events/34627 7
  • 8. Kotlin の Android アプリ開発の準備 8.2 Androidアプリ開発 - プログラミング言語Kotlin 解説 https://sites.google.com/site/tarokotlin/chap8/sec82 8
  • 11. Java と Kotlin の比較 11
  • 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. 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. 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. 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. 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. 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
  • 25. AdminReceiver.class(Java) package com.gsbina.android.sleepnow; import android.app.admin.DeviceAdminReceiver; public class AdminReceiver extends DeviceAdminReceiver { public AdminReceiver() { } } 25
  • 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. 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. 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. 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. 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
  • 33. まとめ 33 final Intent intent = new Intent(this, MainActivity.class); val intent = Intent(this, javaClass<MainActivity>());
  • 34. まとめ 34 new Runnable() { @Override public void run() { // 処理 } } Runnable { // 処理 }
  • 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 Java Kotlin MainActivity 3773 3395 AdminReceiver 131 125 CreateShortcutActivity 594 566 PhoneSleepActivity 631 635 [文字数]