添加了挂断电话的alarm.
authorTrillGates <williamtrillgates@gmail.com>
Fri, 29 Oct 2021 07:36:16 +0000 (15:36 +0800)
committerTrillGates <williamtrillgates@gmail.com>
Fri, 29 Oct 2021 07:36:16 +0000 (15:36 +0800)
app/release/ICSOS_V8_2110282046_release.apk [moved from app/release/ICSOS_V7_2110281634_release.apk with 58% similarity]
app/release/output-metadata.json
app/src/main/AndroidManifest.xml
app/src/main/java/com/ic/sos/EndCallAlarmUtil.java [new file with mode: 0644]
app/src/main/java/com/ic/sos/EndCallReceiver.java [new file with mode: 0644]
app/src/main/java/com/ic/sos/SOSPresenter.java
app/version_code.properties

similarity index 58%
rename from app/release/ICSOS_V7_2110281634_release.apk
rename to app/release/ICSOS_V8_2110282046_release.apk
index 98e9ad2..70f3e5a 100644 (file)
Binary files a/app/release/ICSOS_V7_2110281634_release.apk and b/app/release/ICSOS_V8_2110282046_release.apk differ
index 38c2e25..ab85230 100644 (file)
@@ -12,7 +12,7 @@
       "filters": [],
       "versionCode": 1,
       "versionName": "1.0",
-      "outputFile": "ICSOS_V7_2110281634_release.apk"
+      "outputFile": "ICSOS_V8_2110282046_release.apk"
     }
   ]
 }
\ No newline at end of file
index 463bccc..9a56c8a 100644 (file)
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
+        <receiver android:name=".EndCallReceiver">
+            <intent-filter>
+                <action android:name="com.ic.alarm.action.END_CALL" />
+            </intent-filter>
+        </receiver>
     </application>
 
 </manifest>
\ No newline at end of file
diff --git a/app/src/main/java/com/ic/sos/EndCallAlarmUtil.java b/app/src/main/java/com/ic/sos/EndCallAlarmUtil.java
new file mode 100644 (file)
index 0000000..fdc23fb
--- /dev/null
@@ -0,0 +1,97 @@
+package com.ic.sos;
+
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+
+import com.ic.iclooger.ICLogger;
+
+import java.util.Calendar;
+import java.util.Date;
+
+public class EndCallAlarmUtil {
+
+    public static final String ALARM_ACTION = "com.ic.alarm.action.END_CALL";
+    public static final int END_CALL_ID = 0x9988;
+
+
+    public static void cancelAlarm(Context context) {
+        Intent intent = new Intent(ALARM_ACTION);
+        PendingIntent pi = PendingIntent.getBroadcast(context, END_CALL_ID, intent, PendingIntent
+                .FLAG_CANCEL_CURRENT);
+        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+        am.cancel(pi);
+    }
+
+    /**
+     * 毫秒,秒的话要换算
+     *
+     * @param context
+     * @param duration
+     */
+    public static void setAlarm(Context context, long duration) {
+        ICLogger.e("target duration==> " + duration);
+        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+        Calendar calendar = Calendar.getInstance();
+        long intervalMillis = 0;
+        Date date = new Date();
+        calendar.setTimeInMillis(date.getTime() + duration);
+        Intent intent = new Intent(ALARM_ACTION);
+        PendingIntent sender = PendingIntent.getBroadcast(context, END_CALL_ID, intent, PendingIntent
+                .FLAG_UPDATE_CURRENT);
+        ICLogger.d("intervalMillis ==> " + intervalMillis);
+        //4.4以上的
+        long targetTime = calMethod(0, calendar.getTimeInMillis());
+        ICLogger.d("targetTime ==> " + targetTime);
+        am.setExact(AlarmManager.RTC_WAKEUP, targetTime, sender);
+    }
+
+
+    /**
+     * @param weekFlag 传入的是周几
+     * @param dateTime 传入的是时间戳(设置当天的年月日+从选择框拿来的时分秒)
+     * @return 返回起始闹钟时间的时间戳
+     */
+    private static long calMethod(int weekFlag, long dateTime) {
+        long time = 0;
+        //weekflag == 0表示是按天为周期性的时间间隔或者是一次行的,weekfalg非0时表示每周几的闹钟并以周为时间间隔
+        if (weekFlag != 0) {
+            Calendar c = Calendar.getInstance();
+            int week = c.get(Calendar.DAY_OF_WEEK);
+            if (1 == week) {
+                week = 7;
+            } else if (2 == week) {
+                week = 1;
+            } else if (3 == week) {
+                week = 2;
+            } else if (4 == week) {
+                week = 3;
+            } else if (5 == week) {
+                week = 4;
+            } else if (6 == week) {
+                week = 5;
+            } else if (7 == week) {
+                week = 6;
+            }
+            if (weekFlag == week) {
+                if (dateTime > System.currentTimeMillis()) {
+                    time = dateTime;
+                } else {
+                    time = dateTime + 7 * 24 * 3600 * 1000;
+                }
+            } else if (weekFlag > week) {
+                time = dateTime + (weekFlag - week) * 24 * 3600 * 1000;
+            } else {
+                time = dateTime + (weekFlag - week + 7) * 24 * 3600 * 1000;
+            }
+        } else {
+            if (dateTime > System.currentTimeMillis()) {
+                time = dateTime;
+            } else {
+                time = dateTime + 24 * 3600 * 1000;
+            }
+        }
+        return time;
+    }
+}
diff --git a/app/src/main/java/com/ic/sos/EndCallReceiver.java b/app/src/main/java/com/ic/sos/EndCallReceiver.java
new file mode 100644 (file)
index 0000000..63bedab
--- /dev/null
@@ -0,0 +1,36 @@
+package com.ic.sos;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.IBinder;
+
+import com.android.internal.telephony.ITelephony;
+import com.ic.iclooger.ICLogger;
+
+import java.lang.reflect.Method;
+
+public class EndCallReceiver extends BroadcastReceiver {
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        endCall();
+    }
+
+    private void endCall() {
+        try {
+            Method method = Class.forName("android.os.ServiceManager")
+                    .getMethod("getService", String.class);
+            IBinder binder = (IBinder) method.invoke(null, new Object[]{Context.TELEPHONY_SERVICE});
+            ITelephony telephony = ITelephony.Stub.asInterface(binder);
+            ICLogger.d("endCall...");
+            if (telephony != null) {
+                telephony.endCall();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            ICLogger.e("error", e);
+        }
+    }
+
+}
index c061548..85037a9 100644 (file)
@@ -11,17 +11,14 @@ import android.database.Cursor;
 import android.net.Uri;
 import android.os.Handler;
 import android.os.HandlerThread;
-import android.os.IBinder;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 
 import androidx.lifecycle.MutableLiveData;
 
-import com.android.internal.telephony.ITelephony;
 import com.ic.iclooger.ICLogger;
 
-import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -285,9 +282,9 @@ public class SOSPresenter {
             //开始准备超时
             if (callDuration != 0) {
                 //需要按时挂断
-                handler.removeCallbacks(endCallTask);
+                EndCallAlarmUtil.cancelAlarm(mContext);
                 ICLogger.d("set up end call task...");
-                handler.postDelayed(endCallTask, callDuration * 1000);
+                EndCallAlarmUtil.setAlarm(mContext, callDuration * 1000);
             }
             callPhone(targetPhoneNum);
         }
@@ -297,25 +294,6 @@ public class SOSPresenter {
         }
     }
 
-    //挂断电话,在挂断电话回调的地方,判断有没有必要进行下一次轮询
-    private final Runnable endCallTask = this::endCall;
-
-    private void endCall() {
-        try {
-            Method method = Class.forName("android.os.ServiceManager")
-                    .getMethod("getService", String.class);
-            IBinder binder = (IBinder) method.invoke(null, new Object[]{Context.TELEPHONY_SERVICE});
-            ITelephony telephony = ITelephony.Stub.asInterface(binder);
-            ICLogger.d("endCall...");
-            if (telephony != null) {
-                telephony.endCall();
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-            ICLogger.e("error", e);
-        }
-    }
-
     public void callPhone(String phone) {
         if (telephonyManager.getCallState() != CALL_STATE_IDLE) {
             return;
index 4948920..0099c1e 100644 (file)
@@ -1,2 +1,2 @@
-#Thu Oct 28 16:34:22 CST 2021
-VERSION_CODE=7
+#Thu Oct 28 20:46:51 CST 2021
+VERSION_CODE=8