AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.MyApplication"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/taskTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tarea para el día" android:layout_centerInParent="true" android:textSize="30sp" /> </RelativeLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.example.myapplication import android.os.Bundle import android.widget.TextView import androidx.activity.ComponentActivity import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.json.JSONArray import java.io.BufferedReader import java.io.InputStreamReader import java.net.HttpURLConnection import java.net.URL import java.util.* class MainActivity : ComponentActivity() { private lateinit var taskTextView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) taskTextView = findViewById(R.id.taskTextView) // Obtiene el día actual del mes val dayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH).toString() // Obtener la tarea correspondiente al día actual de manera asíncrona GlobalScope.launch(Dispatchers.IO) { val task = getTaskForDay("https://www.jesusninoc.com/tareas.json", dayOfMonth) withContext(Dispatchers.Main) { taskTextView.text = "$task" } } } private suspend fun getTaskForDay(urlString: String, dayToCheck: String): String { val url = URL(urlString) val connection = url.openConnection() as HttpURLConnection val bufferedReader = BufferedReader(InputStreamReader(connection.inputStream)) val response = StringBuilder() var line: String? while (bufferedReader.readLine().also { line = it } != null) { response.append(line) } val jsonArray = JSONArray(response.toString()) for (i in 0 until jsonArray.length()) { val jsonObject = jsonArray.getJSONObject(i) if (jsonObject.getString("dia") == dayToCheck) { return jsonObject.getString("tarea") } } return "" } } |