SlideShare ist ein Scribd-Unternehmen logo
1 von 36
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
[文字数]

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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...
 

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

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
GR8Conf
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
amitlee9823
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
Abortion pills in Kuwait Cytotec pills in Kuwait
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
lizamodels9
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
amitlee9823
 
Call Girls In Majnu Ka Tilla 959961~3876 Shot 2000 Night 8000
Call Girls In Majnu Ka Tilla 959961~3876 Shot 2000 Night 8000Call Girls In Majnu Ka Tilla 959961~3876 Shot 2000 Night 8000
Call Girls In Majnu Ka Tilla 959961~3876 Shot 2000 Night 8000
dlhescort
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
amitlee9823
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
daisycvs
 

Kürzlich hochgeladen (20)

Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
JAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
JAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLJAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
JAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024
 
Uneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration PresentationUneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration Presentation
 
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
 
Value Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsValue Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and pains
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptx
 
Call Girls In Majnu Ka Tilla 959961~3876 Shot 2000 Night 8000
Call Girls In Majnu Ka Tilla 959961~3876 Shot 2000 Night 8000Call Girls In Majnu Ka Tilla 959961~3876 Shot 2000 Night 8000
Call Girls In Majnu Ka Tilla 959961~3876 Shot 2000 Night 8000
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
 
Business Model Canvas (BMC)- A new venture concept
Business Model Canvas (BMC)-  A new venture conceptBusiness Model Canvas (BMC)-  A new venture concept
Business Model Canvas (BMC)- A new venture concept
 
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort ServiceEluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League City
 

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 [文字数]