Forráskód Böngészése

Implement sending

Thomas Dy 7 éve
szülő
commit
3a1cda2cd5

+ 4 - 0
src/main/AndroidManifest.xml

@@ -3,6 +3,9 @@
       package="com.pleasantprogrammer.mpp"
       android:versionCode="1"
       android:versionName="1.0">
+
+    <uses-permission android:name="android.permission.SEND_SMS" />
+
     <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
         <activity android:name="MainActivity"
                   android:label="@string/app_name">
@@ -11,5 +14,6 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+        <service android:name="SendService"></service>
     </application>
 </manifest>

+ 12 - 1
src/main/kotlin/MainActivity.kt

@@ -1,6 +1,7 @@
 package com.pleasantprogrammer.mpp;
 
 import android.app.Activity
+import android.content.Intent
 import android.os.Bundle
 
 import org.jetbrains.anko.*
@@ -11,8 +12,18 @@ class MainActivity : Activity() {
     super.onCreate(savedInstanceState)
 
     verticalLayout {
-      textView("Hello, world!")
+      button("Send") {
+        onClick {
+          sendRequest()
+        }
+      }
     }
   }
 
+  fun sendRequest() {
+    val intent = Intent(SendService.ACTION_SEND)
+    intent.setClass(this, SendService::class.java)
+    startService(intent)
+  }
+
 }

+ 26 - 0
src/main/kotlin/SendService.kt

@@ -0,0 +1,26 @@
+package com.pleasantprogrammer.mpp;
+
+import android.app.IntentService
+import android.content.Context
+import android.content.Intent
+import android.content.SharedPreferences
+import android.telephony.SmsManager
+
+class SendService : IntentService("SendService") {
+
+  companion object {
+    val ACTION_SEND = "com.pleasantprogrammer.mpp.action.SEND"
+  }
+
+  val sharedPreferences: SharedPreferences by lazy {
+    getSharedPreferences("store", Context.MODE_PRIVATE)
+  }
+
+  override fun onHandleIntent(intent: Intent) {
+    if(intent.action == ACTION_SEND) {
+      val smsManager = SmsManager.getDefault()
+      smsManager.sendTextMessage("5554", null, "GS99", null, null)
+      sharedPreferences.edit().putLong("last_requested", System.currentTimeMillis()).commit()
+    }
+  }
+}