Androidアプリを作る 第7回

9b6ca173883528d3bd047ca8f20b0bce?s=47 nonylene
January 08, 2017

Androidアプリを作る 第7回

9b6ca173883528d3bd047ca8f20b0bce?s=128

nonylene

January 08, 2017
Tweet

Transcript

  1. 7.

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

    • ʮαϒΞΫςΟϏςΟͷΑ͏ͳ΋ͷʯ https://developer.android.com/guide/components/fragments.html
  2. 11.
  3. 12.
  4. 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
  5. 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 ); } }
  6. 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(); } }
  7. 22.
  8. 27.
  9. 28.

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

    ॲཧ" εϨου ˠ࣌ؒˠ  ॲཧ"  ॲཧ# γϯάϧεϨου ϚϧνεϨου
  10. 30.

    "OESPJEͰͷεϨου w ϫʔΧʔ 8PSLFS εϨου w όοΫάϥ΢ϯυʢඇಉظʣ w 6*Λߋ৽Ͱ͖ͳ͍ w

    ෳ਺࡞੒Մೳ εϨου ˠ࣌ؒˠ NBJO ॲཧɾදࣔ XPSLFS ॲཧ# XPSLFS ॲཧ$
  11. 33.

    Πϕϯτ ˠ࣌ؒˠ Click ! εϨου ˠ࣌ؒˠ NBJO ॲཧɾߋ৽ ॲཧɾߋ৽ ॲཧɾߋ৽

    XPSLFS ωοτϫʔΫॲཧ UI ͷߋ৽଴͕ͪൃੜ͠ͳ͍ ϫʔΧʔεϨου࢖༻
  12. 34.

    ৽εϨουΛཱͯΔ new Thread( // Runnable ͷ run ͕৽εϨουͰݺ͹ΕΔ new Runnable()

    { @Override public void run() { // ͜͜ʹ࣮ߦ಺༰Λهड़ } } // start Ͱ৽εϨουɺ run ͩͱಉ͡εϨου(!!) ).start();
  13. 35.

    ϝΠϯεϨουͰॲཧΛ͢Δ // ϝΠϯεϨου (Looper.getMainLooper()) ʹ // ϝοηʔδΛ఻͑Δ܅Λऔಘ Handler handler =

    new Handler(Looper.getMainLooper()); // Runnable ΛૹΔ -> ϝΠϯεϨουͰ run ͕ݺ͹ΕΔ handler.post( new Runnable() { @Override public void run() { // ͜͜ʹ࣮ߦ͢Δ಺༰Λهड़ // ྫ textView.setText(“hogehoge”); } } );
  14. 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 ͱͳ͍ͬͯΔ
  15. 46.

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

    Λ࡞੒ w ωοτϫʔΫ׬ྃޙʹݺ͹ΕΔ w OkHttpClientͰඇಉظϦΫΤετ
  16. 47.

    ϦΫΤετ࡞੒ * MainActivity.java public class MainActivity extends AppCompatActivity { …

    public void sendRequest() { Request kmcRequest = new Request.Builder() .url("https://www.kmc.gr.jp") .build(); } } KMC ͷτοϓϖʔδʹΞΫηε͢ΔϦΫΤετ
  17. 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
  18. 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 ʹ
  19. 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(); } } ); } ϝΠϯεϨουͰ࣮ߦ
  20. 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 Ͱग़͢
  21. 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 Λ࡞੒
  22. 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 ʢඇಉظ࣮ߦʣ
  23. 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(); } ૹ৴͢Δσʔλ
  24. 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
  25. 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࡞੒ νϟϯωϧɾςΩετ
  26. 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