国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android監聽系統來電并彈出提示窗口

2019-10-22 18:25:57
字體:
來源:轉載
供稿:網友

1.問題

項目中有自己企業的通訊錄,但是在應用中撥打公司通訊錄的聯系人,由于手機通訊錄中沒有相應的信息,只顯示一串電話號

2 .目的

監聽系統來電,獲取到電話號碼,通過調用接口,查詢出來相應電話號碼的詳細信息,并彈出系統懸浮框,給用戶提示。

3.實現

首先 注冊廣播監聽系統來電。監聽系統來電需要、注冊相應的權限

代碼地址:https://github.com/sdsjk/phone_alert.git

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /><uses-permission android:name="android.permission.READ_PHONE_STATE" />

自定義廣播去監聽系統來電

 public class PhoneReceiver extends BroadcastReceiver {  private Context mcontext;  @Override  public void onReceive(Context context, Intent intent){    mcontext=context;    System.out.println("action"+intent.getAction());    if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){      //如果是去電(撥出)      Log.e("TAG","撥出");    }else{      Log.e("TAG","來電");      TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);      tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);      //設置一個監聽器    }  }  private PhoneStateListener listener=new PhoneStateListener(){    @Override    public void onCallStateChanged(int state, final String incomingNumber) {      // TODO Auto-generated method stub      //state 當前狀態 incomingNumber,貌似沒有去電的API      super.onCallStateChanged(state, incomingNumber);      switch(state){        case TelephonyManager.CALL_STATE_IDLE:          Log.e("TAG","掛斷");          break;        case TelephonyManager.CALL_STATE_OFFHOOK:          Log.e("TAG","接聽");          break;        case TelephonyManager.CALL_STATE_RINGING:          //輸出來電號碼          Log.e("TAG","響鈴:來電號碼"+incomingNumber);          Log.e("TAG","響鈴:======"+Thread.currentThread().getName());          break;      }    }  };  };

需要靜態注冊廣播

 <receiver android:name="com.cloud.adapter.myview.PhoneReceiver">      <intent-filter>        <action android:name="android.intent.action.PHONE_STATE"/>        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />      </intent-filter>    </receiver>

其次在注冊完,廣播之后我們需要在監聽到系統的來電之后,后獲取到電話號之后去請求接口,獲取數據。并彈出系統懸浮框。

注意:在彈出系統懸浮框的時候需要注冊權限,并且檢查應用的允許彈出懸浮框權限是否開啟。

 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

在監聽中的 TelephonyManager.CALL_STATE_RINGING中操作

    inflate= LayoutInflater.from(mcontext);              wm = (WindowManager)mcontext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);              WindowManager.LayoutParams params = new WindowManager.LayoutParams();              params.type = WindowManager.LayoutParams.TYPE_PHONE;              params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;              params.gravity= Gravity.CENTER;              params.width = WindowManager.LayoutParams.MATCH_PARENT;              params.height = 600;              params.format = PixelFormat.RGBA_8888;              phoneView=inflate.inflate(R.layout.phone_alert,null);              wm.addView(phoneView, params);

自定義一個布局文件,作為要添加的View,布局文件如下

 <?xml version="1.0" encoding="utf-8"?>  <com.cloud.adapter.myview.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="300dp"  android:layout_height="200dp"  android:orientation="vertical"  android:layout_gravity="center"  android:id="@+id/rootview"  >  <LinearLayout  android:background="@drawable/top_background"  android:layout_width="300dp"  android:layout_height="100dp"  android:orientation="vertical"  android:layout_gravity="center"  android:gravity="center"  >  <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="陸XX"    android:textSize="26sp"    />  <TextView    android:layout_marginTop="5dp"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="16sp"    android:text="系統運行科科長"    />  </LinearLayout>  <LinearLayout    android:background="@drawable/bottom_background"    android:layout_width="300dp"    android:layout_height="100dp"    android:orientation="vertical"    android:layout_gravity="center"    android:gravity="center"    >    <TextView      android:textColor="#fff"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="公司本部-信息中心-系統運營科"      android:textSize="20sp"      />    <TextView      android:layout_marginTop="10dp"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textSize="20sp"      android:textColor="#fff"      android:text="XXX有限公司"      android:layout_marginBottom="10dp"      />  </LinearLayout>  </com.cloud.adapter.myview.MyLinearLayout>

使用到兩個背景shape

 <?xml version="1.0" encoding="utf-8"?>  <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">  <corners android:topLeftRadius="20dp"    android:topRightRadius="20dp"    />  <solid android:color="@color/colorPrimary"/>  </shape>  <?xml version="1.0" encoding="utf-8"?>  <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">  <corners android:bottomLeftRadius="20dp"    android:bottomRightRadius="20dp"    />  <solid android:color="#f44"/>  </shape>

廣播中完整代碼

 package com.cloud.adapter.myview;  import android.app.Activity;  import android.app.Service;  import android.content.BroadcastReceiver;  import android.content.Context;  import android.content.Intent;  import android.graphics.PixelFormat;  import android.os.Handler;  import android.os.Looper;  import android.telephony.PhoneStateListener;  import android.telephony.TelephonyManager;  import android.util.Log;  import android.view.Gravity;  import android.view.LayoutInflater;  import android.view.View;  import android.view.WindowManager;  import android.widget.TextView;  /**   * Created by zhang on 2017/10/10.   */  public class PhoneReceiver extends BroadcastReceiver {  private Context mcontext;  private WindowManager wm;  @Override  public void onReceive(Context context, Intent intent){    mcontext=context;    System.out.println("action"+intent.getAction());    if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){      //如果是去電(撥出)      Log.e("TAG","撥出");    }else{      //查了下android文檔,貌似沒有專門用于接收來電的action,所以,非去電即來電      Log.e("TAG","來電");      TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);      tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);      //設置一個監聽器    }  }  private TextView tv;  private LayoutInflater inflate;  private View phoneView;  private PhoneStateListener listener=new PhoneStateListener(){    @Override    public void onCallStateChanged(int state, final String incomingNumber) {      // TODO Auto-generated method stub      //state 當前狀態 incomingNumber,貌似沒有去電的API      super.onCallStateChanged(state, incomingNumber);      switch(state){        case TelephonyManager.CALL_STATE_IDLE:          Log.e("TAG","掛斷");          wm.removeView(tv);          break;        case TelephonyManager.CALL_STATE_OFFHOOK:          Log.e("TAG","接聽");          wm.removeView(tv);          break;        case TelephonyManager.CALL_STATE_RINGING:          inflate= LayoutInflater.from(mcontext);          wm = (WindowManager)mcontext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);          WindowManager.LayoutParams params = new WindowManager.LayoutParams();          params.type = WindowManager.LayoutParams.TYPE_PHONE;          params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;          params.gravity= Gravity.CENTER;          params.width = WindowManager.LayoutParams.MATCH_PARENT;          params.height = 600;          params.format = PixelFormat.RGBA_8888;          phoneView=inflate.inflate(R.layout.phone_alert,null);          wm.addView(phoneView, params);          Log.e("TAG","響鈴:來電號碼"+incomingNumber);          Log.e("TAG","響鈴:======"+Thread.currentThread().getName());          //輸出來電號碼          break;      }    }  };  };

效果圖

Android監聽系統來電,Android監聽來電,Android,彈出提示窗口

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 布尔津县| 绿春县| 肃北| 怀安县| 横峰县| 磐安县| 福贡县| 磐石市| 衡阳县| 烟台市| 永平县| 彩票| 洞头县| 宁都县| 昌黎县| 呼伦贝尔市| 中阳县| 灵川县| 扶沟县| 义马市| 吉水县| 定日县| 阜城县| 牟定县| 于田县| 藁城市| 南汇区| 临高县| 岐山县| 梁河县| 灌阳县| 丰县| 苍南县| 晋城| 榆中县| 全椒县| 勐海县| 永靖县| 北川| 东台市| 龙川县|