import android.os.PowerManager;
import android.os.SystemClock;
import android.support.annotation.Nullable;
+import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.enqualcomm.support.service.Deliver;
import com.enqualcomm.support.service.HeartRateManager;
import com.enqualcomm.support.service.KeepAlive;
+import com.enqualcomm.support.service.PhoneCallListener;
import com.enqualcomm.support.service.SendChatMessage;
import com.enqualcomm.support.service.SilentManager;
import com.enqualcomm.support.service.WeTalkMsgObserver;
ICLogger.d("loadLimitValue...");
loadLimitValue();
});
+ //注册电话监听
+ mHandler.post(() -> {
+ registerPhoneStateListener();
+ });
//add by TrillGates =====end
server_starting = true;
String curProcessName = getProcessName(this, android.os.Process.myPid());
ReConnectToServer();
}
+ /**
+ * 异步注册
+ * 监听电话状态
+ */
+ private void registerPhoneStateListener() {
+ PhoneCallListener phoneCallListener = new PhoneCallListener();
+ TelephonyManager telephonyManager =
+ (TelephonyManager) getApplicationContext().getSystemService(TELEPHONY_SERVICE);
+ if (telephonyManager != null) {
+ telephonyManager.listen(phoneCallListener, PhoneCallListener.LISTEN_CALL_STATE);
+ }
+ }
+
/**
* 异步执行
* 服务创建的时候,加载各种阀值:温度,心率,血氧,血压
--- /dev/null
+package com.enqualcomm.support.service;
+
+import android.content.ContentResolver;
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.CallLog;
+import android.telephony.PhoneStateListener;
+import android.telephony.ServiceState;
+
+import com.enqualcomm.support.provider.ICDatabaseHelper;
+import com.enqualcomm.support.provider.ICNotificationProvider;
+import com.enqualcomm.support.utils.ICLogger;
+
+import static android.telephony.TelephonyManager.CALL_STATE_IDLE;
+import static com.enqualcomm.support.provider.ICGlobalSettingsProvider.SETTINGS;
+
+/**
+ * 监听电话数据库的状态
+ * 如果有未接电话,创建通知到通知库里,UI上监听到通知库的变化,就会显示通知了
+ */
+public class PhoneCallListener extends PhoneStateListener {
+
+ private final Context mContext;
+
+ public PhoneCallListener(Context context) {
+ this.mContext = context;
+ }
+
+ @Override
+ public void onServiceStateChanged(ServiceState serviceState) {
+ super.onServiceStateChanged(serviceState);
+ ICLogger.d("onServiceStateChanged == > " + serviceState);
+ }
+
+ @Override
+ public void onCallStateChanged(int state, String incomingNumber) {
+ super.onCallStateChanged(state, incomingNumber);
+ ICLogger.d("state == > " + state + " incomingNumber ==> " + incomingNumber);
+ //挂断的时候,去查询一下是不是未接电话即可
+ if (state == CALL_STATE_IDLE) {
+ //去查询是否有未接电话
+ queryCallState();
+ }
+ }
+
+ private void queryCallState() {
+ String[] projection = {
+ CallLog.Calls.NUMBER,
+ CallLog.Calls.TYPE
+ };
+ String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE;
+ Cursor c = mContext.getContentResolver().query(
+ Uri.parse("content://call_log/calls"),
+ projection,
+ where,
+ null,
+ null
+ );
+ if (c != null) {
+ if (c.moveToNext()) {
+ String phoneNum = c.getString(0);
+ //创建通知,如果大于1的知,说明有未接电话
+ createNotification(phoneNum);
+ //修改顶部的状态
+ updateSettingsState();
+ }
+ c.close();
+ }
+ }
+
+ private void updateSettingsState() {
+ ContentResolver resolver = mContext.getContentResolver();
+ ContentValues values = new ContentValues();
+ values.put(ICDatabaseHelper.sKey, "has_missed_phone");
+ values.put(ICDatabaseHelper.sValue, 1);
+ resolver.insert(Uri.parse(SETTINGS), values);
+ }
+
+ private void createNotification(String phoneNum) {
+ ContentValues contentValues = new ContentValues();
+ //分类是心率的包名
+ contentValues.put(ICDatabaseHelper.notification_category, "com.ic.phone");
+ contentValues.put(ICDatabaseHelper.notification_title, phoneNum);
+ contentValues.put(ICDatabaseHelper.notification_type, "missed_phone");
+ contentValues.put(ICDatabaseHelper.notification_date, System.currentTimeMillis());
+ contentValues.put(ICDatabaseHelper.notification_order, 0);
+ contentValues.put(ICDatabaseHelper.notification_create_time, System.currentTimeMillis());
+ ContentResolver resolver = mContext.getContentResolver();
+ resolver.insert(Uri.parse(ICNotificationProvider.NOTIFICATIONS), contentValues);
+ }
+
+}