最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android監(jiān)聽系統(tǒng)來電并彈出提示窗口

 更新時間:2017年10月12日 10:04:38   作者:蘇打水解渴  
本篇文章主要介紹了Android監(jiān)聽系統(tǒng)來電并彈出提示窗口,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1.問題

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

2 .目的

監(jiān)聽系統(tǒng)來電,獲取到電話號碼,通過調(diào)用接口,查詢出來相應(yīng)電話號碼的詳細(xì)信息,并彈出系統(tǒng)懸浮框,給用戶提示。

3.實現(xiàn)

首先 注冊廣播監(jiān)聽系統(tǒng)來電。監(jiān)聽系統(tǒng)來電需要、注冊相應(yīng)的權(quán)限

代碼地址: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" />

自定義廣播去監(jiān)聽系統(tǒng)來電

 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);
      //設(shè)置一個監(jiān)聽器
    }
  }


  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,貌似沒有去電的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;
      }
    }
  };
  };

需要靜態(tài)注冊廣播

 <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>

其次在注冊完,廣播之后我們需要在監(jiān)聽到系統(tǒng)的來電之后,后獲取到電話號之后去請求接口,獲取數(shù)據(jù)。并彈出系統(tǒng)懸浮框。

注意:在彈出系統(tǒng)懸浮框的時候需要注冊權(quán)限,并且檢查應(yīng)用的允許彈出懸浮框權(quán)限是否開啟。

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

在監(jiān)聽中的 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="系統(tǒng)運(yùn)行科科長"
    />
  </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="公司本部-信息中心-系統(tǒng)運(yùn)營科"
      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);
      //設(shè)置一個監(jiān)聽器
    }
  }

  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,貌似沒有去電的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;
      }
    }
  };
  };

效果圖


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android仿XListView支持下拉刷新和上劃加載更多的自定義RecyclerView

    Android仿XListView支持下拉刷新和上劃加載更多的自定義RecyclerView

    這篇文章主要介紹了仿XListView支持下拉刷新和上劃加載更多的自定義RecyclerView的實例代碼,非常不錯,具有參考價值,感興趣的朋友可以參考下
    2016-05-05
  • Kotlin Flow常用封裝類StateFlow使用詳解

    Kotlin Flow常用封裝類StateFlow使用詳解

    這篇文章主要為大家介紹了Kotlin Flow常用封裝類StateFlow使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Android獲取聯(lián)系人頭像的方法

    Android獲取聯(lián)系人頭像的方法

    這篇文章主要介紹了Android獲取聯(lián)系人頭像的方法,結(jié)合實例形式分析了Android調(diào)用聯(lián)系人頭像的相關(guān)操作技巧,涉及Bitmap對象及ImageView的使用方法,需要的朋友可以參考下
    2016-08-08
  • Android中AsyncTask異步任務(wù)使用詳細(xì)實例(一)

    Android中AsyncTask異步任務(wù)使用詳細(xì)實例(一)

    AsyncTask是Android提供的輕量級的異步類,可以直接繼承AsyncTask,在類中實現(xiàn)異步操作,并提供接口反饋當(dāng)前異步執(zhí)行的程度(可以通過接口實現(xiàn)UI進(jìn)度更新),最后反饋執(zhí)行的結(jié)果給UI主線程,通過本文給大家介紹Android中AsyncTask異步任務(wù)使用詳細(xì)實例(一),需要的朋友參考下
    2016-02-02
  • Android程序開發(fā)之手機(jī)APP創(chuàng)建桌面快捷方式

    Android程序開發(fā)之手機(jī)APP創(chuàng)建桌面快捷方式

    這篇文章主要介紹了Android程序開發(fā)之手機(jī)APP創(chuàng)建桌面快捷方式 的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • Android動態(tài)布局小結(jié)

    Android動態(tài)布局小結(jié)

    android動態(tài)布局相比靜態(tài)布局,動態(tài)布局不用再將xml轉(zhuǎn)變了布局代碼,提高了一定的效率,本篇文章給大家介紹android動態(tài)布局小結(jié),對android動態(tài)布局相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • Android 自定義view實現(xiàn)TopBar效果

    Android 自定義view實現(xiàn)TopBar效果

    這篇文章主要為大家詳細(xì)介紹了Android 自定義view實現(xiàn)TopBar效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Android冷啟動優(yōu)化的3個小案例分享

    Android冷啟動優(yōu)化的3個小案例分享

    為了提高App的冷啟動耗時,除了在常規(guī)的業(yè)務(wù)側(cè)進(jìn)行耗時代碼優(yōu)化之外,為了進(jìn)一步縮短啟動耗時,需要在純技術(shù)測做一些優(yōu)化探索,本期我們從類預(yù)加載、Retrofit 、ARouter方面進(jìn)行了進(jìn)一步的優(yōu)化,感興趣的同學(xué)跟著小編一起來看看吧
    2023-07-07
  • Android下通過httpClient發(fā)送GET和POST請求的實例代碼

    Android下通過httpClient發(fā)送GET和POST請求的實例代碼

    這篇文章介紹了Android下通過httpClient發(fā)送GET和POST請求的實例代碼,有需要的朋友可以參考一下
    2013-08-08
  • Android ContentResolver使用說明

    Android ContentResolver使用說明

    這篇文章主要介紹了Android ContentResolver使用說明,需要的朋友可以參考下
    2016-01-01

最新評論

连平县| 稻城县| 若尔盖县| 红安县| 贵定县| 类乌齐县| 湘西| 安图县| 扎赉特旗| 阿拉善左旗| 青浦区| 内黄县| 孟津县| 秦皇岛市| 包头市| 酒泉市| 江北区| 麻江县| 菏泽市| 安徽省| 广宗县| 西林县| 龙江县| 福泉市| 托克托县| 南充市| 星子县| 白沙| 长沙县| 滦南县| 宁乡县| 舟曲县| 太湖县| 澄城县| 武安市| 宁津县| 天峻县| 大宁县| 合肥市| 井冈山市| 屏东县|