import android.Manifest import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.net.Uri import android.os.Build import android.os.Bundle import android.provider.Settings import android.telephony.PhoneNumberUtils import android.telephony.TelephonyManager import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import com.android.volley.Response import com.android.volley.toolbox.StringRequest import com.android.volley.toolbox.Volley import kotlinx.android.synthetic.main.activity_login.* import org.json.JSONException import org.json.JSONObject import java.text.SimpleDateFormat import java.util.* import kotlin.collections.HashMap
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) context = this@LoginActivity initView() }
fun initView() { // 코틀린에서는 더 이상 findViewById()를 사용하지 않아도 된다. // Kotlin Android Extension 만 적용시키면, 레이아웃 import 시키는 것만으로도 XML에서 정의내린 위젯들을 Id로 읽어들인다. btn_login.setOnClickListener{ val url: String = "http://www.abc.com/mobile/mlogin.php" val loginID: String = et_id.text.toString().trim() val loginPW: String = et_pw.text.toString().trim() loginVolley(this, url, loginID, loginPW) } }
private fun loginVolley(context: Context, url: String, userid: String, password: String) { // https://developer.android.com/training/volley/simple GET 방법
// 1. RequestQueue 생성 및 초기화 val requestQueue = Volley.newRequestQueue(context)
// 2. Request Obejct인 StringRequest 생성 val request: StringRequest = object : StringRequest(Method.POST, url, Response.Listener { response -> showJSONList(response) }, Response.ErrorListener { error -> Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show() } ) { override fun getParams(): Map<String, String> { val params: MutableMap<String,String> = HashMap() params["userid"] = userid params["password"] = password params["mobileNO"] = getPhoneNumber() // 로그인하는 휴대폰번호 정보 params["uID"] = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID) return params } } // 3) 생성한 StringRequest를 RequestQueue에 추가 requestQueue.add(request) }
fun showJSONList(response: String) { try { Log.e("TAG",response) val jsonObject = JSONObject(response) jsonObject.let { if(it.getString("userinfo") == null) { showAlert(it.getString("result").toString(), it.getString("message").toString()) } else { if(it.getString("result").toString().equals("success")){ val jsonInfo = JSONObject(it.getString("userinfo").toString()) jsonInfo.let{ // Preference 에 대한 정보 기록은 생략한다. startActivity(Intent(this,MainActivity::class.java)) } } } } } catch (e: JSONException) { e.printStackTrace() } }
companion object { var context: Context? = null
@SuppressLint("MissingPermission") // TED퍼미션을 미리 설정했다는 가정하에 fun getPhoneNumber(): String { var phoneNumber = "" try { val telephony = context?.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager if (telephony.line1Number != null) { phoneNumber = telephony.line1Number } else { if (telephony.simSerialNumber != null) { phoneNumber = telephony.simSerialNumber } } } catch (e: Exception) { e.printStackTrace() } if (phoneNumber.startsWith("+82")) { phoneNumber = phoneNumber.replace("+82", "0") } phoneNumber = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { PhoneNumberUtils.formatNumber( phoneNumber, Locale.getDefault().country ) } else { PhoneNumberUtils.formatNumber(phoneNumber) } return phoneNumber }
fun showAlert(title: String, message: String) { val builder = context?.let { AlertDialog.Builder(it) .setTitle(title) .setMessage(message) .setCancelable(false) .setPositiveButton("OK") { dialog, id -> dialog.dismiss() } } val alert = builder!!.create() alert.show() } }
}
|