Slide 1

Slide 1 text

"OESPJEΞϓϦΛ࡞Δ ,.$OPOZMFOF 'SBHNFOUɾωοτϫʔΫ

Slide 2

Slide 2 text

ࣗݾ঺հ w OPOZMFOF ͷʹΕΜ w ,.$ճ w ΧʔϏΟͰ͢ w ΏΔΏΓ#%ങͬͨ w SPPU

Slide 3

Slide 3 text

ࣗݾ঺հ w OPOZMFOF ͷʹΕΜ w εϚϗΞϓϦͱ͔ w 5XJUUFS(JU)VCFUD w IUUQOPOZMFOFIBUFOBCMPHKQ

Slide 4

Slide 4 text

͜Μ͹Μ͸

Slide 5

Slide 5 text

ࠓ೔΍Δ͜ͱ 'SBHNFOU ֓ཁͷΈ ωοτϫʔΫ w4MBDLʹ౤ߘ

Slide 6

Slide 6 text

'SBHNFOU

Slide 7

Slide 7 text

'SBHNFOU • Activity ʹදࣔ͢Δը໘Ϣχοτʢޙड़ʣ • ը໘Λ Activity ؒͰ࢖͍·Θ͢ • λϒͰෳ਺ͷը໘Λදࣔͱ͔ʹΑ͘࢖͏ • ʮαϒΞΫςΟϏςΟͷΑ͏ͳ΋ͷʯ https://developer.android.com/guide/components/fragments.html

Slide 8

Slide 8 text

"DUJWJUZͱ'SBHNFOU

Slide 9

Slide 9 text

ಉ͡"DUJWJUZ

Slide 10

Slide 10 text

'SBHNFOUʢଟ෼ʣ

Slide 11

Slide 11 text

"DUJWJUZ

Slide 12

Slide 12 text

'SBHNFOU

Slide 13

Slide 13 text

ಉ͡"DUJWJUZ

Slide 14

Slide 14 text

߲໨Λ੾Γସ͑
 ˠ'SBHNFOUΛ੾Γସ͑ ͨͿΜ

Slide 15

Slide 15 text

'SBHNFOU 7JFX "DUJWJUZ ؅ཧ දࣔɾ؅ཧ දࣔɾ؅ཧ ※ Fragment ࣗମ͸ ViewGroup ͷதʹ͋Δ

Slide 16

Slide 16 text

'SBHNFOUͷϝϦοτ w ಉ͡ը໘ϢχοτΛෳ਺ͷ"DUJWJUZͰڞ༗ w λϒ͝ͱʹ'SBHNFOUͱͯ͠ಠཱͰ͖Δ w "DUJWJUZͱಉ༷ͷϥΠϑαΠΫϧ͕͋Δ
 w "DUJWJUZͷॲཧΛ෼ׂͰ͖Δ

Slide 17

Slide 17 text

࢖͍ͬͯ͘ w 'SBHNFOU͸"DUJWJUZΑΓ΋গ͠ෳࡶ w ࠓ͸ʮ࢖Θͳ͚Ε͹ͳΒͳ͍࣌ʹ࢖͏ʯఔ౓Ͱ w ྫλϒΛ࢖͍͍ͨͱ͖ ը໘Λڞ௨Խ͍͚ͨͩ͠ͳΒಉ͡ Activity Λ࢖͑͹͍͍͚ͩ

Slide 18

Slide 18 text

࢖༻্ͷ஫ҙ w 4VQQPSU-JCSBSZͷ'SBHNFOUΛ࢖͏ w "OESPJE4%,ʹ΋ଘࡏ͢Δ͕ɺݹ͍όʔδϣϯͩͱ࣮૷ ҧ͏ͷͰͭΒ͍ • android.app.Fragment w 4VQQPSU-JCSBSZ൛͸࠷৽Ͱ࣮૷͕ಉ͡Ͱྑ͍ w ϥΠϒϥϦͱͯ͠ΞϓϦʹಉࠝ͢Δ͔Β • android.support.v4.app.Fragment http://stackoverflow.com/questions/17295497/fragment-or-support-fragment

Slide 19

Slide 19 text

ίʔυྫʢܰ͘ʣ * ExampleFragment.java // android.support.v4.app.Fragment Λ༻͍Δ public class ExampleFragment extends Fragment { // View Λ࡞ͬͯฦ͢ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // R.layout.fragment_example Λ View ʹ͢Δ return inflater.inflate( R.layout.fragment_example, container, false ); } }

Slide 20

Slide 20 text

* Main2Activity.java public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); // SupportFragmentManager Λ༻͍Δ͜ͱ FragmentTransaction fragmentTransaction = 
 getSupportFragmentManager().beginTransaction();
 // ࢦఆͷ View Λ Fragment ͱஔ͖׵͑Δ fragmentTransaction.replace(
 R.id.base_view,
 new ExampleFragment());
 fragmentTransaction.commit(); } }

Slide 21

Slide 21 text

ωοτϫʔΫ

Slide 22

Slide 22 text

ݖݶ

Slide 23

Slide 23 text

ݖݶ w "OESPJE্Ͱಛఆͷ͜ͱΛߦ͏ͨΊʹඞཁ w ϓϥΠόγʔܥʹଟ͍ w ࿈བྷઌɾωοτϫʔΫɾϑΝΠϧฤूFUDʜ
 w "OESPJE.BOJGFTUʹهड़͢Δ

Slide 24

Slide 24 text

ݖݶ w 1MBZ4UPSFͰೖΕΔ࣌ʹ ฉ͔ΕΔ w "OESPJE͔Β͸Ұ෦͕ ϢʔβʔͷڐՄ੍ʹ

Slide 25

Slide 25 text

ωοτϫʔΫݖݶ w ϢʔβʔͷڐՄ͸ඞཁͳ͍ w Πϯετʔϧ࣌ʹදࣔ͞ΕΔ͚ͩ

Slide 26

Slide 26 text

ωοτϫʔΫݖݶ w "OESPJE.BOJGFTUYNMʹ࢖༻͢ΔࢫΛهड़

Slide 27

Slide 27 text

εϨου

Slide 28

Slide 28 text

εϨου w $16ར༻ͷ୯Ґ w ಉ࣌ʹෳ਺ͷεϨουΛ࣮ߦͰ͖Δ εϨου ˠ࣌ؒˠ ॲཧ" ॲཧ# ॲཧ" εϨου ˠ࣌ؒˠ ॲཧ" ॲཧ# γϯάϧεϨου ϚϧνεϨου

Slide 29

Slide 29 text

"OESPJEͰͷεϨου w ϝΠϯεϨουʢ6*εϨουʣ w ීஈ࢖͍ͬͯΔͷ͸ϝΠϯεϨου w 6*Λߋ৽Ͱ͖Δ w 7JFXɾ5PBTUɾ"DUJWJUZFUD w ϓϩηεʹ͖ͭҰͭ εϨου ˠ࣌ؒˠ NBJO ॲཧ" ॲཧ# ॲཧ"

Slide 30

Slide 30 text

"OESPJEͰͷεϨου w ϫʔΧʔ 8PSLFS εϨου w όοΫάϥ΢ϯυʢඇಉظʣ w 6*Λߋ৽Ͱ͖ͳ͍ w ෳ਺࡞੒Մೳ εϨου ˠ࣌ؒˠ NBJO ॲཧɾදࣔ XPSLFS ॲཧ# XPSLFS ॲཧ$

Slide 31

Slide 31 text

"OESPJEͰͷεϨου w ωοτϫʔΫ͸6*εϨουͰߦΘͳ͍ w "OESPJEҎ্Ͱ͸Τϥʔ w େ఍͕͔͔࣌ؒΔ͔Β w ϝΠϯεϨουͰ΍Δͱը໘͕ݻ·ͬͯ͠·͏ w ωοτϫʔΫ଴ͪͰ6*ͷߋ৽͕Ͱ͖ͳ͍

Slide 32

Slide 32 text

Πϕϯτ $MJDL εϨου ˠ࣌ؒˠ NBJO ॲཧɾߋ৽ ωοτϫʔΫॲཧ ॲཧɾߋ৽ UI ͷߋ৽଴͕ͪൃੜ ϝΠϯεϨου࢖༻

Slide 33

Slide 33 text

Πϕϯτ ˠ࣌ؒˠ Click ! εϨου ˠ࣌ؒˠ NBJO ॲཧɾߋ৽ ॲཧɾߋ৽ ॲཧɾߋ৽ XPSLFS ωοτϫʔΫॲཧ UI ͷߋ৽଴͕ͪൃੜ͠ͳ͍ ϫʔΧʔεϨου࢖༻

Slide 34

Slide 34 text

৽εϨουΛཱͯΔ new Thread( // Runnable ͷ run ͕৽εϨουͰݺ͹ΕΔ new Runnable() { @Override public void run() { // ͜͜ʹ࣮ߦ಺༰Λهड़ } } // start Ͱ৽εϨουɺ run ͩͱಉ͡εϨου(!!) ).start();

Slide 35

Slide 35 text

ϝΠϯεϨουͰॲཧΛ͢Δ // ϝΠϯεϨου (Looper.getMainLooper()) ʹ // ϝοηʔδΛ఻͑Δ܅Λऔಘ Handler handler = new Handler(Looper.getMainLooper()); // Runnable ΛૹΔ -> ϝΠϯεϨουͰ run ͕ݺ͹ΕΔ handler.post( new Runnable() { @Override public void run() { // ͜͜ʹ࣮ߦ͢Δ಺༰Λهड़ // ྫ textView.setText(“hogehoge”); } } );

Slide 36

Slide 36 text

ωοτϫʔΫܥ
 ϥΠϒϥϦ

Slide 37

Slide 37 text

"OESPJEͰωοτϫʔΫ w ϥΠϒϥϦແ͠ͷ৔߹ "TZOD5BTL͔"TZOD5BTL-PBEFSΛ࡞੒ w ผεϨουΛཱͯͯ΍ͬͯ͘ΕΔ܅ ίʔϧόοΫΛ࡞੒ wϝΠϯεϨουͰը໘ߋ৽ όοΫάϥ΢ϯυͰωοτϫʔΫʹΞΫηε w%FGBVMU)UUQ$MJFOUΛ࢖͏

Slide 38

Slide 38 text

ϥΠϒϥϦ࢖͍·͢ w ϥΠϒϥϦ࢖Θͳ͍ωοτϫʔΫ͸৭ʑ໘౗ w ʢ͜͜ʹ༷ʑͳ۪ஒʣ

Slide 39

Slide 39 text

ωοτϫʔΫܥϥΠϒϥϦ w 0L)UUQ w 4RVBSF͕ࣾެ։͍ͯ͠ΔϥΠϒϥϦ w 3FUSPpU w 4RVBSF͕ࣾެ։͍ͯ͠Δ w 1JDBTTP w 4RVBSF͕ࣾެ։

Slide 40

Slide 40 text

ωοτϫʔΫܥϥΠϒϥϦ w 0L)UUQ w ωοτϫʔΫͷجຊతͳ෦෼ w )551ʹ΋ରԠ w "OESPJE಺෦Ͱ΋࢖ΘΕ͍ͯΔ

Slide 41

Slide 41 text

ωοτϫʔΫܥϥΠϒϥϦ w 3FUSPpU w +TPOΛ࢖͏"1*ʹΞΫηε͢Δͷʹศར w ࣗಈͰ+40/Λઐ༻Ϋϥεʹม׵ͯ͘͠ΕͨΓ w 1JDBTTP w ը૾Λऔͬͯ͘Δͷʹศར w *NBHF7JFXʹදࣔͨ͠ΓΩϟογϡͨ͠Γ

Slide 42

Slide 42 text

0L)UUQ
 ͰΞΫηε

Slide 43

Slide 43 text

0L)UUQ w ࠓճ͸0L)UUQΛ࢖͏ w γϯϓϧͳͷͰ w app/build.gradle ͰϥΠϒϥϦΛ௥Ճ w ϓϩδΣΫττοϓʹ͋ΔํͰ͸ͳ͍ʢ஫ҙʣ w BQQͷ಺෦Ͱ࢖͏ͷͰapp/build.gradle

Slide 44

Slide 44 text

0L)UUQ * app/build.gradle dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:24.2.1' testCompile ‘junit:junit:4.12' compile ‘com.squareup.okhttp3:okhttp:3.5.0' } groupId(άϧʔϓ໊):artifactId(ϥΠϒϥϦ໊):version ͱͳ͍ͬͯΔ

Slide 45

Slide 45 text

0L)UUQ w هड़ޙʹ
 (SBEMF4ZODΛߦ͏ w *%&ʹೝࣝͤ͞ΔͨΊ

Slide 46

Slide 46 text

0L)UUQͷྲྀΕ w ϦΫΤετ Request Λ࡞੒ w 63-΍ϔομʔͦͷଞΛઃఆ w ίʔϧόοΫ Callback Λ࡞੒ w ωοτϫʔΫ׬ྃޙʹݺ͹ΕΔ w OkHttpClientͰඇಉظϦΫΤετ

Slide 47

Slide 47 text

ϦΫΤετ࡞੒ * MainActivity.java public class MainActivity extends AppCompatActivity { … public void sendRequest() { Request kmcRequest = new Request.Builder() .url("https://www.kmc.gr.jp") .build(); } } KMC ͷτοϓϖʔδʹΞΫηε͢ΔϦΫΤετ

Slide 48

Slide 48 text

* MainActivity.java public class MainActivity extends AppCompatActivity { … public void sendRequest() { Request kmcRequest = …; Callback callback = new Callback() { @Override public void onFailure(Call call, IOException e) { // do nothing } @Override public void onResponse(Call call, final Response response) throws IOException { … } }; } } ίʔϧόοΫ࡞੒ ੒ޭ -> onResponse, ࣦഊ -> onFailure

Slide 49

Slide 49 text

* MainActivity.java: onResponse @Override public void onResponse(Call call, final Response response) throws IOException { final String body = response.body().string(); new Handler(Looper.getMainLooper()).post( new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,
 body, Toast.LENGTH_LONG).show(); } } ); } response ͔Β body Λऔಘͯ͠ String ʹ

Slide 50

Slide 50 text

* MainActivity.java: onResponse @Override public void onResponse(Call call, final Response response) throws IOException { final String body = response.body().string(); new Handler(Looper.getMainLooper()).post( new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,
 body, Toast.LENGTH_LONG).show(); } } ); } ϝΠϯεϨουͰ࣮ߦ

Slide 51

Slide 51 text

* MainActivity.java: onResponse @Override public void onResponse(Call call, final Response response) throws IOException { final String body = response.body().string(); new Handler(Looper.getMainLooper()).post( new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,
 body, Toast.LENGTH_LONG).show(); } } ); } औಘͨ͠಺༰Λ Toast Ͱग़͢

Slide 52

Slide 52 text

0L)UUQͰϦΫΤετ * MainActivity.java public class MainActivity extends AppCompatActivity { … public void sendRequest() { Request kmcRequest = …; Callback callback = …; OkHttpClient client = new OkHttpClient(); client.newCall(kmcRequest) .enqueue(callback); } } OkHttpClient Λ࡞੒

Slide 53

Slide 53 text

0L)UUQͰϦΫΤετ * MainActivity.java public class MainActivity extends AppCompatActivity { … public void sendRequest() { Request kmcRequest = …; Callback callback = …; OkHttpClient client = new OkHttpClient(); client.newCall(kmcRequest) .enqueue(callback); } } ϦΫΤετΛ enqueue ʢඇಉظ࣮ߦʣ

Slide 54

Slide 54 text

ΫϦοΫͯ͠ϦΫΤετ * MainActivity.java : onCreate submitButton.setOnClickListener( new View.OnClickListener() { ɹ@Override public void onClick(View v) { sendRequest(); ɹ} } );

Slide 55

Slide 55 text

ϦΫΤετͯ͠ΈΔ

Slide 56

Slide 56 text

4MBDLʹ౤ߘ

Slide 57

Slide 57 text

4MBDL౤ߘͷྲྀΕ w 5PLFOΛ࡞੒ʢલճͷεϥΠυʣ w 5PLFOΛൿີͷ৔ॴʹอଘ w Request࡞੒ w ౤ߘ

Slide 58

Slide 58 text

4MBDL5PLFOอଘ

Slide 59

Slide 59 text

5PLFOอଘ w 5PLFO͸ൿີʹ͠·͠ΐ͏ w 4MBDL͔Β࿈བྷ͘Δ w (JUϨϙδτϦʹ௥Ճ͠ͳ͍Α͏ʹ͢Δ

Slide 60

Slide 60 text

5PLFOอଘखஈ "ൿີͷϦιʔεϑΝΠϧΛ࡞ͬͯอଘ w ࠓճ͸ private.xml w .gitignoreʹ௥Ճ w ͦ͜ʹ5PLFOΛอଘ #Ϣʔβʔʹೖྗͤ͞Δ w SharedPreferencesʹอଘ

Slide 61

Slide 61 text

ϓϥΠϕʔτͳTUSJOHT w SFTWBMVFTΛӈΫϦοΫ w /FXWBMVFTSFTPVSDFpMF

Slide 62

Slide 62 text

ϓϥΠϕʔτͳTUSJOHT w QSJWBUFYNMΛ࡞੒

Slide 63

Slide 63 text

ϓϥΠϕʔτͳTUSJOHT w HJUJHOPSFʹprivate.xmlΛ௥Ճ w ϓϩδΣΫτ௚Լͷ(JU*HOPSFͰ0,


Slide 64

Slide 64 text

ϓϥΠϕʔτͳTUSJOHT w QSJWBUFYNMʹUPLFOΛઃఆ * Private.xml xoxp-hogehoge

Slide 65

Slide 65 text

3FRVFTU࡞੒

Slide 66

Slide 66 text

4MBDL౤ߘ"1* w IUUQTTMBDLDPNBQJDIBUQPTU.FTTBHF w IUUQTBQJTMBDLDPNNFUIPETDIBUQPTU.FTTBHF w tokenͱtextͱchannelΛઃఆͯ͠౤ߘ

Slide 67

Slide 67 text

3FRVFTU࡞੒ RequestBody࡞੒ w ࠓճ͸x-www-form-urlencodedܗࣜͰૹ৴ Request࡞੒ w ࡞੒ͨ͠RequestBodyΛૹ৴͢ΔΑ͏ʹ͢Δ

Slide 68

Slide 68 text

3FRVFTU#PEZ࡞੒ * MainActivitiy.java public void sendRequest() { RequestBody formBody = new FormBody.Builder() .add("token", getString(R.string.token)) .add("channel", “#android-project") .add("text", “͜Μʹͪ͸͜Μʹͪ͸") .build(); } ૹ৴͢Δσʔλ

Slide 69

Slide 69 text

* MainActivitiy.java public void sendRequest() { RequestBody formBody = new FormBody.Builder() .add("token", getString(R.string.token)) .add("channel", “#android-project") .add("text", “͜Μʹͪ͸͜Μʹͪ͸") .build(); } 3FRVFTU#PEZ࡞੒ ઃఆͨ͠ Token

Slide 70

Slide 70 text

* MainActivitiy.java public void sendRequest() { RequestBody formBody = new FormBody.Builder() .add("token", getString(R.string.token)) .add("channel", “#android-project") .add("text", “͜Μʹͪ͸͜Μʹͪ͸") .build(); } 3FRVFTU#PEZ࡞੒ νϟϯωϧɾςΩετ

Slide 71

Slide 71 text

3FRVFTU࡞੒ * MainActivitiy.java public void sendRequest() { RequestBody formBody = …; Request slackRequest = new Request.Builder() .url(“https://slack.com/api/chat.postMessage") .post(formBody) .build(); } formBody Λ POST

Slide 72

Slide 72 text

౤ߘ੒ޭ