Androidアプリを作る 第7回

9b6ca173883528d3bd047ca8f20b0bce?s=47 nonylene
January 08, 2017

Androidアプリを作る 第7回

9b6ca173883528d3bd047ca8f20b0bce?s=128

nonylene

January 08, 2017
Tweet

Transcript

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

  2. ࣗݾ঺հ w OPOZMFOF ͷʹΕΜ  w ,.$ճ w ΧʔϏΟͰ͢ w

    ΏΔΏΓ#%ങͬͨ w SPPU
  3. ࣗݾ঺հ w OPOZMFOF ͷʹΕΜ  w εϚϗΞϓϦͱ͔ w 5XJUUFS(JU)VCFUD w

    IUUQOPOZMFOFIBUFOBCMPHKQ
  4. ͜Μ͹Μ͸

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

  6. 'SBHNFOU

  7. 'SBHNFOU • Activity ʹදࣔ͢Δը໘Ϣχοτʢޙड़ʣ • ը໘Λ Activity ؒͰ࢖͍·Θ͢ • λϒͰෳ਺ͷը໘Λදࣔͱ͔ʹΑ͘࢖͏

    • ʮαϒΞΫςΟϏςΟͷΑ͏ͳ΋ͷʯ https://developer.android.com/guide/components/fragments.html
  8. "DUJWJUZͱ'SBHNFOU

  9. ಉ͡"DUJWJUZ

  10. 'SBHNFOUʢଟ෼ʣ

  11. "DUJWJUZ

  12. 'SBHNFOU

  13. ಉ͡"DUJWJUZ

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

  15. 'SBHNFOU 7JFX "DUJWJUZ ؅ཧ දࣔɾ؅ཧ දࣔɾ؅ཧ ※ Fragment ࣗମ͸ ViewGroup

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

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

  18. ࢖༻্ͷ஫ҙ 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
  19. ίʔυྫʢܰ͘ʣ * 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 ); } }
  20. * 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(); } }
  21. ωοτϫʔΫ

  22. ݖݶ

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

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

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

  26. ωοτϫʔΫݖݶ w "OESPJE.BOJGFTUYNMʹ࢖༻͢ΔࢫΛهड़ <manifest …> <uses-permission
 android:name=“android.permission.INTERNET"
 /> <application …

    </application> </manifest>
  27. εϨου

  28. εϨου w $16ར༻ͷ୯Ґ w ಉ࣌ʹෳ਺ͷεϨουΛ࣮ߦͰ͖Δ εϨου ˠ࣌ؒˠ  ॲཧ" ॲཧ#

    ॲཧ" εϨου ˠ࣌ؒˠ  ॲཧ"  ॲཧ# γϯάϧεϨου ϚϧνεϨου
  29. "OESPJEͰͷεϨου w ϝΠϯεϨουʢ6*εϨουʣ w ීஈ࢖͍ͬͯΔͷ͸ϝΠϯεϨου w 6*Λߋ৽Ͱ͖Δ w 7JFXɾ5PBTUɾ"DUJWJUZFUD w

    ϓϩηεʹ͖ͭҰͭ εϨου ˠ࣌ؒˠ NBJO ॲཧ" ॲཧ# ॲཧ"
  30. "OESPJEͰͷεϨου w ϫʔΧʔ 8PSLFS εϨου w όοΫάϥ΢ϯυʢඇಉظʣ w 6*Λߋ৽Ͱ͖ͳ͍ w

    ෳ਺࡞੒Մೳ εϨου ˠ࣌ؒˠ NBJO ॲཧɾදࣔ XPSLFS ॲཧ# XPSLFS ॲཧ$
  31. "OESPJEͰͷεϨου w ωοτϫʔΫ͸6*εϨουͰߦΘͳ͍ w "OESPJEҎ্Ͱ͸Τϥʔ w େ఍͕͔͔࣌ؒΔ͔Β w ϝΠϯεϨουͰ΍Δͱը໘͕ݻ·ͬͯ͠·͏ w

    ωοτϫʔΫ଴ͪͰ6*ͷߋ৽͕Ͱ͖ͳ͍
  32. Πϕϯτ $MJDL εϨου ˠ࣌ؒˠ NBJO ॲཧɾߋ৽ ωοτϫʔΫॲཧ ॲཧɾߋ৽ UI ͷߋ৽଴͕ͪൃੜ

    ϝΠϯεϨου࢖༻
  33. Πϕϯτ ˠ࣌ؒˠ Click ! εϨου ˠ࣌ؒˠ NBJO ॲཧɾߋ৽ ॲཧɾߋ৽ ॲཧɾߋ৽

    XPSLFS ωοτϫʔΫॲཧ UI ͷߋ৽଴͕ͪൃੜ͠ͳ͍ ϫʔΧʔεϨου࢖༻
  34. ৽εϨουΛཱͯΔ new Thread( // Runnable ͷ run ͕৽εϨουͰݺ͹ΕΔ new Runnable()

    { @Override public void run() { // ͜͜ʹ࣮ߦ಺༰Λهड़ } } // start Ͱ৽εϨουɺ run ͩͱಉ͡εϨου(!!) ).start();
  35. ϝΠϯεϨουͰॲཧΛ͢Δ // ϝΠϯεϨου (Looper.getMainLooper()) ʹ // ϝοηʔδΛ఻͑Δ܅Λऔಘ Handler handler =

    new Handler(Looper.getMainLooper()); // Runnable ΛૹΔ -> ϝΠϯεϨουͰ run ͕ݺ͹ΕΔ handler.post( new Runnable() { @Override public void run() { // ͜͜ʹ࣮ߦ͢Δ಺༰Λهड़ // ྫ textView.setText(“hogehoge”); } } );
  36. ωοτϫʔΫܥ
 ϥΠϒϥϦ

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

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

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

    1JDBTTP w 4RVBSF͕ࣾެ։
  40. ωοτϫʔΫܥϥΠϒϥϦ w 0L)UUQ w ωοτϫʔΫͷجຊతͳ෦෼ w )551ʹ΋ରԠ w "OESPJE಺෦Ͱ΋࢖ΘΕ͍ͯΔ

  41. ωοτϫʔΫܥϥΠϒϥϦ w 3FUSPpU w +TPOΛ࢖͏"1*ʹΞΫηε͢Δͷʹศར w ࣗಈͰ+40/Λઐ༻Ϋϥεʹม׵ͯ͘͠ΕͨΓ w 1JDBTTP w

    ը૾Λऔͬͯ͘Δͷʹศར w *NBHF7JFXʹදࣔͨ͠ΓΩϟογϡͨ͠Γ
  42. 0L)UUQ
 ͰΞΫηε

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

    w BQQͷ಺෦Ͱ࢖͏ͷͰapp/build.gradle
  44. 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 ͱͳ͍ͬͯΔ
  45. 0L)UUQ w هड़ޙʹ
 (SBEMF4ZODΛߦ͏ w *%&ʹೝࣝͤ͞ΔͨΊ

  46. 0L)UUQͷྲྀΕ w ϦΫΤετ Request Λ࡞੒ w 63-΍ϔομʔͦͷଞΛઃఆ w ίʔϧόοΫ Callback

    Λ࡞੒ w ωοτϫʔΫ׬ྃޙʹݺ͹ΕΔ w OkHttpClientͰඇಉظϦΫΤετ
  47. ϦΫΤετ࡞੒ * MainActivity.java public class MainActivity extends AppCompatActivity { …

    public void sendRequest() { Request kmcRequest = new Request.Builder() .url("https://www.kmc.gr.jp") .build(); } } KMC ͷτοϓϖʔδʹΞΫηε͢ΔϦΫΤετ
  48. * 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
  49. * 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 ʹ
  50. * 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(); } } ); } ϝΠϯεϨουͰ࣮ߦ
  51. * 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 Ͱग़͢
  52. 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 Λ࡞੒
  53. 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 ʢඇಉظ࣮ߦʣ
  54. ΫϦοΫͯ͠ϦΫΤετ * MainActivity.java : onCreate submitButton.setOnClickListener( new View.OnClickListener() { ɹ@Override

    public void onClick(View v) { sendRequest(); ɹ} } );
  55. ϦΫΤετͯ͠ΈΔ

  56. 4MBDLʹ౤ߘ

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

  58. 4MBDL5PLFOอଘ

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

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

    w SharedPreferencesʹอଘ
  61. ϓϥΠϕʔτͳTUSJOHT w SFTWBMVFTΛӈΫϦοΫ w /FXWBMVFTSFTPVSDFpMF

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

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


  64. ϓϥΠϕʔτͳTUSJOHT w QSJWBUFYNMʹUPLFOΛઃఆ * Private.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string

    name=“token">xoxp-hogehoge</string> </resources>
  65. 3FRVFTU࡞੒

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

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

  68. 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(); } ૹ৴͢Δσʔλ
  69. * 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
  70. * 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࡞੒ νϟϯωϧɾςΩετ
  71. 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
  72. ౤ߘ੒ޭ