SlideShare une entreprise Scribd logo
1  sur  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
[文字数]

Contenu connexe

Tendances

Gradle - the Enterprise Automation Tool
Gradle  - the Enterprise Automation ToolGradle  - the Enterprise Automation Tool
Gradle - the Enterprise Automation ToolIzzet Mustafaiev
 
Exploring the GitHub Service Universe
Exploring the GitHub Service UniverseExploring the GitHub Service Universe
Exploring the GitHub Service UniverseBjö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 InfrastructureGunnar 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 pluginDariusz Ł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 ArgoCDSunnyvale
 
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 ForgeAntoine 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 BuildsEgor 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 ReviewZhongyue Luo
 
Cloud nativemicroservices jax-london2020
Cloud nativemicroservices   jax-london2020Cloud nativemicroservices   jax-london2020
Cloud nativemicroservices jax-london2020Emily Jiang
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0Michael 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 HudsonChris 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
 

Tendances (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...
 

Similaire à 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.pdfShaiAlmog1
 
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 2015Friedger 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 2018Somkiat 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 pluginXavier Hallade
 
Android OS & SDK - Getting Started
Android OS & SDK - Getting StartedAndroid OS & SDK - Getting Started
Android OS & SDK - Getting StartedHemant 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.pdfShaiAlmog1
 
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 210Mahmoud 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-2023Nicolas HAAN
 
Native Android Development Practices
Native Android Development PracticesNative Android Development Practices
Native Android Development PracticesRoy Clarkson
 
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 SideVisual 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 KotlinEatDog
 
Lesson 5 Layouts.pptx
Lesson 5 Layouts.pptxLesson 5 Layouts.pptx
Lesson 5 Layouts.pptxTempTemp63
 
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 PipelineVeaceslav 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 prosparkfabrik
 

Similaire à 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
 

Dernier

The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...Aggregage
 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876dlhescort
 
Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1kcpayne
 
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort ServiceMalegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort ServiceDamini Dixit
 
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
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLBAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLkapoorjyoti4444
 
Falcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investorsFalcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investorsFalcon Invoice Discounting
 
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
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 MonthsIndeedSEO
 
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwaitdaisycvs
 
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 Presentationuneakwhite
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...rajveerescorts2022
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...lizamodels9
 
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 CityEric T. Tung
 
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, 2024Marel
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with CultureSeta Wicaksana
 
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 8000dlhescort
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptxnandhinijagan9867
 

Dernier (20)

The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
 
Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1
 
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort ServiceMalegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
 
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...
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLBAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
 
Falcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investorsFalcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investors
 
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...
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
 
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
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
 
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
 
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
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
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
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptx
 

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