Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Androidアプリを作る 第7回

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for nonylene nonylene
January 08, 2017

Androidアプリを作る 第7回

Avatar for nonylene

nonylene

January 08, 2017
Tweet

More Decks by nonylene

Other Decks in Technology

Transcript

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

    • ʮαϒΞΫςΟϏςΟͷΑ͏ͳ΋ͷʯ https://developer.android.com/guide/components/fragments.html
  2. ࢖༻্ͷ஫ҙ 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
  3. ίʔυྫʢܰ͘ʣ * 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 ); } }
  4. * 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(); } }
  5. εϨου w $16ར༻ͷ୯Ґ w ಉ࣌ʹෳ਺ͷεϨουΛ࣮ߦͰ͖Δ εϨου ˠ࣌ؒˠ  ॲཧ" ॲཧ#

    ॲཧ" εϨου ˠ࣌ؒˠ  ॲཧ"  ॲཧ# γϯάϧεϨου ϚϧνεϨου
  6. "OESPJEͰͷεϨου w ϫʔΧʔ 8PSLFS εϨου w όοΫάϥ΢ϯυʢඇಉظʣ w 6*Λߋ৽Ͱ͖ͳ͍ w

    ෳ਺࡞੒Մೳ εϨου ˠ࣌ؒˠ NBJO ॲཧɾදࣔ XPSLFS ॲཧ# XPSLFS ॲཧ$
  7. Πϕϯτ ˠ࣌ؒˠ Click ! εϨου ˠ࣌ؒˠ NBJO ॲཧɾߋ৽ ॲཧɾߋ৽ ॲཧɾߋ৽

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

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

    new Handler(Looper.getMainLooper()); // Runnable ΛૹΔ -> ϝΠϯεϨουͰ run ͕ݺ͹ΕΔ handler.post( new Runnable() { @Override public void run() { // ͜͜ʹ࣮ߦ͢Δ಺༰Λهड़ // ྫ textView.setText(“hogehoge”); } } );
  10. 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 ͱͳ͍ͬͯΔ
  11. 0L)UUQͷྲྀΕ w ϦΫΤετ Request Λ࡞੒ w 63-΍ϔομʔͦͷଞΛઃఆ w ίʔϧόοΫ Callback

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

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