120
C-LIS CO., LTD.

2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Embed Size (px)

Citation preview

Page 1: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

C-LIS CO., LTD.

Page 2: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

自己紹介

Page 3: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Android Studio本

3

2014年11月21日発売

技術評論社刊

Android Studio 0.8.6http://amzn.to/1HYRp32

Page 4: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

4

Page 5: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

5

改訂版が出ます(2016年1月上旬)

Page 6: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

改訂版の原稿を送ったのが11月16日

Page 7: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

7

Page 8: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

11月20日

8

Android Studio 1.5 Release

Page 9: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

続く

Page 10: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

2015年のAndroidアプリ開発入門

ABCD 2015 Kanazawa

2015/11/22 石川工業高等専門学校

Page 11: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

ターゲット

11

Android 2.x系からのAndroidアプリ開発者

これからAndroidアプリ開発を学ぼうとしている初心者

Page 12: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

2015年のAndroid

Page 13: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Androidのバージョン

13

2.3GingerBread

3.xHoneycomb

4.0

IceCreamSandwich

Page 14: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Androidのバージョン

14

4.1/4.2/4.3

Jelly Bean

4.4

KitKat

Page 15: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Androidのバージョン

15

5.0

Lollipop

6.0

Marshmallow

Page 16: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

バージョンの分布

16https://developer.android.com/intl/ja/about/dashboards/index.html

Page 17: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

プラットフォームの拡大

17

TV Auto

Page 18: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

プラットフォームの拡大

18

Wearable

Page 19: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

API Level

Page 20: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

API Level

20

バージョン API Level バージョンコード

6.0 23 M

5.1 22 Lollipop_MR1

5.0 21 Lollipop

4.4W 20 KitKat for Wearables Only

4.4 19 KITKAT

4.3 18 JELLY_BEAN_MR2

4.2 - 4.2.2 17 JELLY_BEAN_MR1

4.1 - 4.1.1 16 JELLY_BEAN

Page 21: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

API Level

21

バージョン API Level バージョンコード

4.0.3, 4.0.4 15 ICE_CREAM_SANDWICH_MR1

4.0, 4.0.1, 4.0.2 14 ICE_CREAM_SANDWICH

3.2 13 HONEYCOMB_MR2

3.1.x 12 HONEYCOMB_MR1

3.0.x 11 HONEYCOMB

2.3.3, 2.3.4 10 GINGERBREAD_MR1

2.3 - 2.3.2 9 GINGERBREAD

Page 22: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Fragment

Page 23: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Activity

23

Page 24: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Activity

24

Activity

• 1つの画面を構成する

• Androidのシステムコンポーネントの一つ(Activity/Service/BroadcastReceiver/ContentProvider) • AndroidManifest.xmlに記載する

• 他のアプリから呼び出すことができる

IntentFilter

Page 25: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

タブレットの登場(Android 3.0)

25

Page 26: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Fragment

26

• 1つの画面(Activity)を分割する

• システムコンポーネントではない

= AndroidManifest.xmlに記載の必要なし

• 他のアプリから呼び出すことができない

Page 27: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Fragmentの例

27

Page 28: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Android 3.0(API Level 11)未満

28

Fragmentは使えない

Page 29: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Support Libraries

29

http://developer.android.com/intl/ja/tools/support-library/index.html

• バージョンやプラットフォームの差を埋める互換(Compat)クラスを含む

• android.support.v4.app は、API Level 4以降に対応するv4 Support Library

v7 Support Libraries

v13 Support Library

v14 Preference Support Library

v17 Preference Support Library for TV

v17 Leaseback Library

Design Support Library

Page 30: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Fragmentを使うには

30

com.android.support-v4 ライブラリー

Page 31: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

public class MainActivity extends AppCompatActivity { public static final int REQUEST_CODE = 0x1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar ab = getSupportActionBar(); ab.setIcon(R.mipmap.ic_launcher); getSupportFragmentManager() .beginTransaction() .add(R.id.content, SettingFragment.newInstance(), SettingFragment.class.getSimpleName()) .commit(); }}

Fragmentを使うには

Page 32: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

public class SettingFragment extends Fragment {

}

Fragmentを使うには

android.support.v4.app.Fragment

android.app.Fragment

Page 33: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

どちらのパッケージを使う?

android.support.v4.app.Fragment android.app.Fragment

Page 34: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

バージョンの分布

34https://developer.android.com/intl/ja/about/dashboards/index.html

Page 35: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

ViewPager

35

Page 36: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

android.support.v4.view.ViewPager

36

http://developer.android.com/intl/ja/reference/android/support/v4/view/ViewPager.html

android.support.v4.app.Fragment android.app.Fragment

Page 37: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

最近のAndroidでは……

37

Page 38: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Google Maps

Page 39: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

39

Google Map API

Page 40: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

40

https://code.google.com/intl/ja/android/maps-api-signup.html

Page 41: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

41

https://console.developers.google.com

Page 42: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Google Maps v2 API

42

Page 43: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Google Play Services

43

Page 44: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Google Play Services

44

• Google Adwords

• Google Fit

• Play Games API

• Location API

• Panorama API

• NearBy

• Google Plus

• Vision (顔認識・バーコード認識)

• Google Wallet

• Wearable

Page 45: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

プラットフォームの拡大

45

Amazon Kindle Fire

Androidベースのプラットフォーム

Google Play Store無し

= Androidのエコシステムに入らない

Page 46: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

UI / UX

Page 47: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

さよならMenu Button

47

Say Goodbye to the Menu Button http://android-developers.blogspot.jp/2012/01/say-goodbye-to-menu-button.html

Page 48: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Menu Button → ActionBar

48

Page 49: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

NavigationDrawer

49

Page 50: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

ActionBar → ToolBar

50

Page 51: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/main_content" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>

ActionBar → ToolBar

Page 52: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primary_dark</item> <item name="colorAccent">@color/accent</item> <item name="preferenceTheme">@style/PreferenceThemeOverlay</item> </style>

ActionBar → ToolBar

Page 53: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

ToolBar

Page 54: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

ToolBarのタイトルが黒い

Change title color in toolbar?

http://stackoverflow.com/questions/28954586/change-title-color-in-toolbar

54

Page 55: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/main_content" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="enterAlways|snap" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </android.support.design.widget.AppBarLayout>

AppBarLayout

Page 56: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

AppBarLayout

Page 57: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/main_content" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="enterAlways|snap" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </android.support.design.widget.AppBarLayout>

AppBarLayout

Page 58: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Design Support Library

58

Google I/O 2015で発表

画面デザインに特化したサポートライブラリー

https://github.com/chrisbanes/cheesesquare

Page 59: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Cheesesquare

59

Page 60: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Material Design

60

https://www.google.com/design/spec/material-design/introduction.html

Page 61: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

FAB

61

Floating Action Button

Page 62: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Floating Action Button

62

Page 63: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Material Color

63

Primary Color

Accent Color

Page 64: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

64

Material Pallet http://www.materialpalette.com/

Page 65: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

開発環境

65

Android Developer Tools

Android Studio

Page 66: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Android Developer Tools

66

Eclipse+プラグイン 2007年から標準の開発環境

2015年12月末でサポート終了を発表

An update on Eclipse Android Developer Tools http://android-developers.blogspot.jp/2015/06/an-update-on-eclipse-android-developer.html

Page 67: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Android Studio

67

Google I/O 2013で発表

2015年にバージョン1.0がリリース

チェコJetBRAINS社が開発している

「IntelliJ IDEA Community Edition」

がベース

ビルドシステムは「Grade」

Android plugin for Gradle でAndroid

アプリのビルドに対応している

Page 68: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Android Studio本

68

2014年11月21日発売

技術評論社刊

Android Studio 0.8.6http://amzn.to/1HYRp32

Page 69: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

69

Page 70: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

70

http://amzn.to/

改訂版が出ます(2016年1月上旬)

Page 71: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

改訂版の原稿を送ったのが11月16日

Page 72: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

72

Page 73: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

11月20日

73

Android Studio 1.5 Release

Page 74: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

続き

Page 75: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Android Studio Update Checker

75

Page 76: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Android Studio Update Checker

76

•Support V7 Library AppCompat Preference

•Design Support Library

Page 77: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

各バージョンでの見た目

77

Page 78: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

各バージョンでの見た目

78

Page 79: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Notification(通知)

Page 80: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

重要度 種別

80

Page 81: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

プライベートな通知を非表示に

81

Page 82: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setContentTitle(context.getText(R.string.new_android_studio)) .setContentText(context.getText(R.string.new_android_studio_version_available)) .setTicker(context.getText(R.string.new_android_studio_version_available)) .setPriority(NotificationCompat.PRIORITY_MAX) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setSmallIcon(R.mipmap.ic_launcher) .setDefaults(Notification.DEFAULT_VIBRATE);NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(builder) .setBigContentTitle(context.getText(R.string.new_android_studio_version_available));for (UpdateState.Product.Channel channel : updatedChannelList) { UpdateState.Product.Channel.Build build = channel.builds.get(0); inboxStyle.addLine(String.format(Locale.US, "%s in %s channel.\n", build.version, channel.status));} NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);nm.notify(NOTIFICATION_ID, builder.build());

NotificationCompat(support.v4)

82

Page 83: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

NotificationCompat

83

Page 84: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Android Wear

84

Page 85: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

NotificationCompat

85

java.lang.IllegalArgumentException: contentIntent required:

pkg=io.keiji.asupdatechecker

id=1

notification=Notification(vibrate=default,sound=null,defaults=0x2,flags=0x80)

Android 2.3

Page 86: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

PreferenceFragmentCompat(support.v7)

86http://stackoverflow.com/questions/32070670/preferencefragmentcompat-requires-preferencetheme-to-be-set

Page 87: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Product Flavors

87

supportedFlavor Support Libraryを使ったバージョン

API Level 7以降に対応

latestFlavor Support Libraryを使わないバージョン

API Level 11以降に対応

https://github.com/keiji/AsUpdateChecker

Page 88: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Demo

Page 89: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

まとめ

89

ViewPagerを使う場合はSupport Library 一択

ただし、PreferenceFragmentを使う場合は通常の

Fragmentがお勧め

その場合、個別のActivityとしてSupport Library

Fragmentと混合しない。

https://github.com/keiji/AsUpdateChecker

Page 90: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

まとめ

90

• Androidはバージョンアップ毎に、さまざまなAPIを追加している。

• 以前のバージョンでも同等機能が使えるように、さまざまなSupport

LibraryがGoogleから提供されている。

• Androidの機能拡張はプラットフォームだけでなく、Google Play開発者

サービスを通じての機能提供も重要度を増している。

• Support Libraryは完全ではない。場合によってはproductFlavorなどを

使ってAPKの分割を検討する必要もある。

Page 91: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

C-LIS CO., LTD.

各製品名・ブランド名、会社名などは、一般に各社の商標または登録商標です。 本資料中では、©、®、™を割愛しています。

本資料は、有限会社シーリスの著作物であり、

クリエイティブコモンズの表示-非営利-継承 3.0 Unported ライセンスの元で公開しています。

Page 92: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

おまけ

Page 93: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

オープンソース・ライブラリー

Page 94: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

findViewById

Page 95: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

public class MainActivity extends AppCompatActivity { public static final int REQUEST_CODE = 0x1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar ab = getSupportActionBar(); ab.setIcon(R.mipmap.ic_launcher); }}

findViewById

Page 96: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener { private Button buttonPushMe; private ImageView imageAndroid; private SeekBar seekBarRed; private SeekBar seekBarGreen; private SeekBar seekBarBlue;

ButterKnife

http://jakewharton.github.io/butterknife/

Page 97: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonPushMe = (Button) findViewById(R.id.btn_pushme); buttonPushMe.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { buttonPushMe.setText("ボタンが押されました!"); imageAndroid.setImageResource(R.drawable.droid2); } }); imageAndroid = (ImageView) findViewById(R.id.iv_android); seekBarRed = (SeekBar) findViewById(R.id.sb_red); seekBarRed.setOnSeekBarChangeListener(this); seekBarGreen = (SeekBar) findViewById(R.id.sb_green); seekBarGreen.setOnSeekBarChangeListener(this); seekBarBlue = (SeekBar) findViewById(R.id.sb_blue); seekBarBlue.setOnSeekBarChangeListener(this); }

ButterKnife

http://jakewharton.github.io/butterknife/

Page 98: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener { @Bind(R.id.iv_android) ImageView imageAndroid; @Bind(R.id.sb_red) SeekBar seekBarRed; @Bind(R.id.sb_green) SeekBar seekBarGreen; @Bind(R.id.sb_blue) SeekBar seekBarBlue;

ButterKnife

http://jakewharton.github.io/butterknife/

Page 99: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); seekBarRed.setOnSeekBarChangeListener(this); seekBarGreen.setOnSeekBarChangeListener(this); seekBarBlue.setOnSeekBarChangeListener(this); }

ButterKnife

http://jakewharton.github.io/butterknife/

Page 100: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener { @Bind(R.id.btn_pushme) Button buttonPushMe; @OnClick(R.id.btn_pushme) public void onClick(View v) { buttonPushMe.setText("ボタンが押されました!"); imageAndroid.setImageResource(R.drawable.droid2); }

ButterKnife

http://jakewharton.github.io/butterknife/

Page 101: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

SQLite ̶ データベース

Page 102: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

public class DbHelper extends SQLiteOpenHelper { private static final String DB_NAME = "user-data.db"; private static final int DB_VERSION = 1; public DbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } private static final String CREATE_USER_TABLE = "CREATE TABLE users (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT," + "gender INTEGER," + "megane INTEGER" + ");"; @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_USER_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}

SQLite

Page 103: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

ContentValues values1 = new ContentValues();values1.put("name", "Claudia Madobe");values1.put("gender", 0); values1.put("megane", 1); ContentValues values2 = new ContentValues();values2.put("name", "Claude Madobe");values2.put("gender", 1); values2.put("megane", 1); SQLiteDatabase db = new DbHelper(this).getWritableDatabase();db.beginTransaction();Log.d(TAG, "id = " + db.insert("users", null, values1));Log.d(TAG, "id = " + db.insert("users", null, values2));db.setTransactionSuccessful();db.endTransaction();Cursor cursor = db.query("users", new String[]{"name", "megane"}, "gender = ? AND megane = ?", new String[]{"0", "1"}, null, null, null);Log.d(TAG, "count = " + cursor.getCount());while (cursor.moveToNext()) { Log.d(TAG, "name = " + cursor.getString(cursor.getColumnIndex("name")));}

SQLite

Page 104: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

実行結果 count = 1 name = Claudia Madobe

SQLite

Page 105: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

public class User { public static final int GENDER_FEMALE = 0; public static final int GENDER_MALE = 1; private long id; private String name; private int gender = GENDER_FEMALE; private boolean megane = true; public long save(SQLiteDatabase db) { ContentValues values = new ContentValues(); values.put("name", name); values.put("gender", gender); values.put("megane", megane ? 1 : 0); return db.insert("users", null, values); }

// アクセサ省略 }

Userクラス

Page 106: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

public class User extends RealmObject { @Ignore public static final int GENDER_FEMALE = 0; @Ignore public static final int GENDER_MALE = 1; private long id; private String name; private int gender = GENDER_FEMALE; private boolean megane = true;

// アクセサ省略 }

Realm

https://realm.io

Page 107: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Realm realm = Realm.getInstance(this); realm.beginTransaction();User user = realm.createObject(User.class); user.setName("Claudia Madobe"); user.setGender(User.GENDER_FEMALE); user.setMegane(true);user = realm.createObject(User.class); user.setName("Claude Madobe"); user.setGender(User.GENDER_MALE);user.setMegane(true);realm.commitTransaction();RealmResults<User> result = realm.where(User.class) .equalTo("gender", User.GENDER_FEMALE) .equalTo("megane", true) .findAll();Log.d(TAG, "count = " + result.size());for (User userData : result) { Log.d(TAG, "name = " + userData.getName());}

Realm

https://realm.io

Page 108: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

実行結果 count = 1 name = Claudia Madobe

SQLite

Page 109: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

JSONのパース

Page 110: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

{ "users" : [ { "name" : "Claudia Madobe", "gender" : 0, "megane" : true }, { "name" : "Claude Madobe", "gender" : 1, "megane" : true } ] }

JSONObject

Page 111: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

List<User> userList = new ArrayList<>();try { JSONObject jsonObject = new JSONObject(JSON); JSONArray users = jsonObject.getJSONArray("users"); for (int i = 0; i < users.length(); i++) { JSONObject userJson = users.getJSONObject(i); User user = new User(); user.setName(userJson.getString("name")); user.setGender(userJson.getInt("gender")); user.setMegane(userJson.getBoolean("megane")); userList.add(user); } Log.d(TAG, "count = " + userList.size()); for (User userData : userList) { Log.d(TAG, "name = " + userData.getName()); }} catch (JSONException e) {}

JSONObject

Page 112: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

実行結果 count = 1 name = Claudia Madobe

name = Claude Madobe

JSONObject

{ "users" : [ { "name" : "Claudia Madobe", "gender" : 0, "megane" : true }, { "name" : "Claude Madobe", "gender" : 1, "megane" : true } ] }

Page 113: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

@JsonModelpublic class User { public static final int GENDER_FEMALE = 0; public static final int GENDER_MALE = 1; @JsonKey private long id; @JsonKey private String name; @JsonKey private int gender = GENDER_FEMALE; @JsonKey private boolean megane = true;

JsonPullParser

https://github.com/vvakame/JsonPullParser

Page 114: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

@JsonModelpublic class UserList { @JsonKey private List<User> users; public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; }}

JsonPullParser

https://github.com/vvakame/JsonPullParser

Page 115: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

try { UserList userList = UserListGen.get(JSON); Log.d(TAG, "count = " + userList.getUsers().size()); for (User userData : userList.getUsers()) { Log.d(TAG, "name = " + userData.getName()); }} catch (IOException e) {} catch (JsonFormatException e) {}

JsonPullParser

https://github.com/vvakame/JsonPullParser

Page 116: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

実行結果 count = 1 name = Claudia Madobe

name = Claude Madobe

JsonPullParser

https://github.com/vvakame/JsonPullParser

{ "users" : [ { "name" : "Claudia Madobe", "gender" : 0, "megane" : true }, { "name" : "Claude Madobe", "gender" : 1, "megane" : true } ] }

Page 117: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Deprecated(非推奨)

Page 118: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Deprecated

118

Apache HTTP Client API Level 22でdeprecatedに指定。

compileSdkVersion 23で、通常ではビルドできない扱いに。

•URLConnection

•OkHttp

Page 119: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

Deprecated

119

Camera API API Level 21でdeprecatedに指定。

Nexus 5XなどではCamera APIをそのまま使うと、

カメラ映像が上下反転する不具合(仕様)有り

https://www.reddit.com/r/Android/comments/3rjbo8/nexus5x_marshmallow_camera_problem/cwqzqgh

•Camera2 API

Page 120: 2015年のAndroidアプリ開発入門 - ABCD 2015 Kanazawa

まとめ

120

• オープンソースライブラリーの活用は、開発効率や性能の向上に繋がる。

新しいOSSの登場やバージョンアップを注視する必要がある。

• 非推奨(deprecated)に指定されて使えなくなったり、使用すると不具

合が発生するAPIも出はじめた。