十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
1.問(wèn)題
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、白塔網(wǎng)絡(luò)推廣、微信小程序開(kāi)發(fā)、白塔網(wǎng)絡(luò)營(yíng)銷(xiāo)、白塔企業(yè)策劃、白塔品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供白塔建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
項(xiàng)目中有自己企業(yè)的通訊錄,但是在應(yīng)用中撥打公司通訊錄的聯(lián)系人,由于手機(jī)通訊錄中沒(méi)有相應(yīng)的信息,只顯示一串電話號(hào)
2 .目的
監(jiān)聽(tīng)系統(tǒng)來(lái)電,獲取到電話號(hào)碼,通過(guò)調(diào)用接口,查詢出來(lái)相應(yīng)電話號(hào)碼的詳細(xì)信息,并彈出系統(tǒng)懸浮框,給用戶提示。
3.實(shí)現(xiàn)
首先 注冊(cè)廣播監(jiān)聽(tīng)系統(tǒng)來(lái)電。監(jiān)聽(tīng)系統(tǒng)來(lái)電需要、注冊(cè)相應(yīng)的權(quán)限
代碼地址:https://github.com/sdsjk/phone_alert.git
自定義廣播去監(jiān)聽(tīng)系統(tǒng)來(lái)電
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","來(lái)電"); TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); //設(shè)置一個(gè)監(jiān)聽(tīng)器 } } private PhoneStateListener listener=new PhoneStateListener(){ @Override public void onCallStateChanged(int state, final String incomingNumber) { // TODO Auto-generated method stub //state 當(dāng)前狀態(tài) incomingNumber,貌似沒(méi)有去電的API super.onCallStateChanged(state, incomingNumber); switch(state){ case TelephonyManager.CALL_STATE_IDLE: Log.e("TAG","掛斷"); break; case TelephonyManager.CALL_STATE_OFFHOOK: Log.e("TAG","接聽(tīng)"); break; case TelephonyManager.CALL_STATE_RINGING: //輸出來(lái)電號(hào)碼 Log.e("TAG","響鈴:來(lái)電號(hào)碼"+incomingNumber); Log.e("TAG","響鈴:======"+Thread.currentThread().getName()); break; } } }; };
需要靜態(tài)注冊(cè)廣播
其次在注冊(cè)完,廣播之后我們需要在監(jiān)聽(tīng)到系統(tǒng)的來(lái)電之后,后獲取到電話號(hào)之后去請(qǐng)求接口,獲取數(shù)據(jù)。并彈出系統(tǒng)懸浮框。
注意:在彈出系統(tǒng)懸浮框的時(shí)候需要注冊(cè)權(quán)限,并且檢查應(yīng)用的允許彈出懸浮框權(quán)限是否開(kāi)啟。
在監(jiān)聽(tīng)中的 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);
自定義一個(gè)布局文件,作為要添加的View,布局文件如下
<?xml version="1.0" encoding="utf-8"?>
使用到兩個(gè)背景shape
<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
廣播中完整代碼
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文檔,貌似沒(méi)有專門(mén)用于接收來(lái)電的action,所以,非去電即來(lái)電 Log.e("TAG","來(lái)電"); TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); //設(shè)置一個(gè)監(jiān)聽(tīng)器 } } 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 當(dāng)前狀態(tài) incomingNumber,貌似沒(méi)有去電的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","接聽(tīng)"); 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","響鈴:來(lái)電號(hào)碼"+incomingNumber); Log.e("TAG","響鈴:======"+Thread.currentThread().getName()); //輸出來(lái)電號(hào)碼 break; } } }; };
效果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。