Created
June 3, 2018 08:38
-
-
Save ftnext/704223d95c81a275686e75d1235c1b16 to your computer and use it in GitHub Desktop.
AsyncTaskのサンプルコード(http://androidpala.com/kotlin-asynctask-android/ )をもとに、AsyncTaskを定期実行する機能を実装。アプリ起動後、AsyncTaskで2秒おきに現在時刻を取得する。トグルボタンをオフにすると現在時刻の取得が止まる。トグルボタンオンで再開する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <android.support.constraint.ConstraintLayout 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" | |
| tools:context=".MainActivity"> | |
| <TextView | |
| android:id="@+id/my_text" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginEnd="8dp" | |
| android:layout_marginStart="8dp" | |
| android:layout_marginTop="72dp" | |
| android:text="AsyncTask Example" | |
| android:textSize="18sp" | |
| android:textStyle="bold" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintRight_toRightOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" /> | |
| <ProgressBar | |
| android:id="@+id/MyProgressBar" | |
| style="?android:attr/progressBarStyle" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginEnd="8dp" | |
| android:layout_marginStart="8dp" | |
| android:layout_marginTop="32dp" | |
| android:visibility="invisible" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/my_text" /> | |
| <ToggleButton | |
| android:id="@+id/MyToggleButton" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:layout_marginEnd="8dp" | |
| android:layout_marginStart="8dp" | |
| android:layout_marginTop="32dp" | |
| android:checked="true" | |
| android:text="ToggleButton" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/MyProgressBar" /> | |
| </android.support.constraint.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.asynctaskpractice | |
| import android.os.AsyncTask | |
| import android.support.v7.app.AppCompatActivity | |
| import android.os.Bundle | |
| import android.os.Handler | |
| import android.util.Log | |
| import android.view.View | |
| import kotlinx.android.synthetic.main.activity_main.* | |
| import java.io.BufferedReader | |
| import java.io.InputStream | |
| import java.io.InputStreamReader | |
| import java.net.HttpURLConnection | |
| import java.net.URL | |
| class MainActivity : AppCompatActivity() { | |
| val Tag = "MainActivity" | |
| val handler = Handler() | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| val runnable = object: Runnable { | |
| override fun run() { | |
| AsyncTaskExample().execute() | |
| handler.postDelayed(this, 2000) | |
| } | |
| } | |
| handler.post(runnable) | |
| MyToggleButton.setOnCheckedChangeListener { buttonView, isChecked -> | |
| if (isChecked) { | |
| Log.d(Tag, "Toggle Button is $isChecked -> Start GET Access") | |
| handler.post(runnable) | |
| } else { | |
| Log.d(Tag, "Toggle Button is $isChecked -> Stop GET Access") | |
| handler.removeCallbacks(runnable) | |
| } | |
| } | |
| } | |
| inner class AsyncTaskExample: AsyncTask<String, String, String>() { | |
| override fun onPreExecute() { | |
| super.onPreExecute() | |
| MyProgressBar.visibility = View.VISIBLE | |
| } | |
| override fun doInBackground(vararg p0: String?): String { | |
| var Result = "" | |
| val API_URL = "http://androidpala.com/tutorial/http.php?get=1" | |
| try { | |
| val URL = URL(API_URL) | |
| val connect = URL.openConnection() as HttpURLConnection | |
| connect.readTimeout = 8000 | |
| connect.connectTimeout = 8000 | |
| connect.requestMethod = "GET" | |
| connect.doOutput = true | |
| connect.connect() | |
| val ResponseCode = connect.responseCode | |
| Log.d(Tag, "ResponseCode $ResponseCode") | |
| if (ResponseCode == 200) { | |
| val tempStream = connect.inputStream | |
| if (tempStream != null) { | |
| Result = ConvertToString(tempStream) | |
| } | |
| } | |
| } catch (Ex: Exception) { | |
| Log.e("", "Error in doInBackground ${Ex.message}") | |
| } | |
| return Result | |
| } | |
| override fun onPostExecute(result: String?) { | |
| super.onPostExecute(result) | |
| MyProgressBar.visibility = View.INVISIBLE | |
| if (result == "") { | |
| my_text.text = "Network Error" | |
| } else { | |
| my_text.text = result | |
| } | |
| } | |
| } | |
| fun ConvertToString(inStream: InputStream):String { | |
| var Result = "" | |
| val isReader = InputStreamReader(inStream) | |
| var bReader = BufferedReader(isReader) | |
| var temp_str: String? | |
| try { | |
| while (true) { | |
| temp_str = bReader.readLine() | |
| if (temp_str == null) { break } | |
| Result += temp_str | |
| } | |
| } catch (Ex: Exception) { | |
| Log.e(Tag, "Error in ConvertToString ${Ex.printStackTrace()}") | |
| } | |
| return Result | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment