Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 監聽來去電 彈出懸浮窗提示

android 監聽來去電 彈出懸浮窗提示

編輯:關於Android編程

最近給老大做個安卓手機的小程序,希望實現 來去電的時候,提示一下(主要是看看有沒有這個號碼的相關事情)。

在網上找了很多,都是只有來電的懸浮窗。

算了,還是自己研究吧。

下面給出監聽部分和懸浮窗,可拖拽的。

 

 

[java]
//來去電廣播  
public class InterceptCall extends BroadcastReceiver { 
    private static final String TAG = "PhoneStatReceiver"; 
    private WindowManager wm; 
    WindowManager.LayoutParams params; 
    private float mTouchStartX; 
    private float mTouchStartY; 
    private float x; 
    private float y; 
 
    private Button tv; 
    Date curDate = null; 
 
    Context mcontext; 
 
    private void updateViewPosition() { 
        // 更新浮動窗口位置參數  
        params.x = (int) (x - mTouchStartX); 
        params.y = (int) (y - mTouchStartY); 
        wm.updateViewLayout(tv, params); 
 
    } 
 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        mcontext = context; 
         
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { 
            // 如果是去電(撥出)  
            // mCallEvent = CALL_EVENT_OUTGOING;  
            MyApplication.NumberType = 1; 
            String mCallNumber = intent 
                    .getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
            Log.i(TAG, "call OUT:" + mCallNumber); 
            MyApplication.PhoneNum = mCallNumber; 
 
            wm = null; 
            wm = (WindowManager) context.getApplicationContext() 
                    .getSystemService(Context.WINDOW_SERVICE); 
            // WindowManager.LayoutParams  
            params = new WindowManager.LayoutParams(); 
             
            params.type = LayoutParams.TYPE_PHONE; 
             
 
            // 設置Window flag  
            params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL 
                    | LayoutParams.FLAG_NOT_FOCUSABLE; 
            params.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角 <SPAN style="COLOR: #ff0000">為實現拖拽,必須加上這個,不然會漂移,不信你試試。</SPAN>  
            // 以屏幕左上角為原點,設置x、y初始  
            params.x = 20; 
            params.y = 5; 
            params.width = WindowManager.LayoutParams.WRAP_CONTENT; 
            params.height = WindowManager.LayoutParams.WRAP_CONTENT; 
            params.format = PixelFormat.RGBA_8888; 
            tv = new Button(context); 
             
            tv.setText("雙擊查看:\n" + mCallNumber + "的記錄");// +sds);  
 
            wm.addView(tv, params); 
 
            tv.setOnTouchListener(new OnTouchListener() { 
 
                @Override 
                public boolean onTouch(View v, MotionEvent event) { 
 
                    // 獲取相對屏幕的坐標,即以屏幕左上角為原點  
                    x = event.getRawX(); 
                    y = event.getRawY() - 25; // 25是系統狀態欄的高度  
 
                    switch (event.getAction()) { 
                    case MotionEvent.ACTION_DOWN: 
                        // 獲取相對View的坐標,即以此View左上角為原點  
                        mTouchStartX = event.getX(); 
                        mTouchStartY = event.getY(); 
 
                        break; 
                    case MotionEvent.ACTION_MOVE: 
                        updateViewPosition(); 
 
                        break; 
                    // 判斷手勢點擊起來的時候:  
                    case MotionEvent.ACTION_UP: 
                        updateViewPosition(); 
                        mTouchStartX = mTouchStartY = 0; 
 
                        if (curDate != null) { 
                            Date dateNow = new Date(System.currentTimeMillis()); 
                            // date轉成毫秒  
                            long diff = dateNow.getTime() - curDate.getTime(); 
 
                            // 這樣得到的差值是微秒級別  
 
                            if (diff < 400) { 
                                // 雙擊  
                                try { 
                                    if (wm != null) { 
                                        wm.removeView(tv); 
 
                                    } 
                                } catch (Exception e) { 
 
                                } 
 
                                curDate = null; 
 
                                Intent service = new Intent(); 
                                service.setClass(mcontext, TeleLogContent.class); 
                                service.putExtra("PhoneNumer", 
                                        MyApplication.PhoneNum); 
                                service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                                mcontext.startActivity(service); 
 
                            } else { 
                                // 單擊  
 
                                curDate = new Date(System.currentTimeMillis()); 
                            } 
                            // 這裡也是單擊  
                        } else { 
                            // 單擊  
 
                            curDate = new Date(System.currentTimeMillis()); 
                        } 
 
                        break; 
                    } 
                    return true; 
                } 
            }); 
            // AskCallStateThread callStateThread = new  
            // AskCallStateThread(context);  
            // callStateThread.start();  
 
        } else { 
             
            String comeInNum = intent 
                    .getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 
             
            System.out.println("來電號碼:" + comeInNum); 
 
            TelephonyManager telM = (TelephonyManager) context 
                    .getSystemService(Context.TELEPHONY_SERVICE); 
 
            // 設置一個監聽器  
            telM.listen(new TelListener(context), 
                    PhoneStateListener.LISTEN_CALL_STATE); 
        } 
    } 
 

//來去電廣播
public class InterceptCall extends BroadcastReceiver {
 private static final String TAG = "PhoneStatReceiver";
 private WindowManager wm;
 WindowManager.LayoutParams params;
 private float mTouchStartX;
 private float mTouchStartY;
 private float x;
 private float y;

 private Button tv;
 Date curDate = null;

 Context mcontext;

 private void updateViewPosition() {
  // 更新浮動窗口位置參數
  params.x = (int) (x - mTouchStartX);
  params.y = (int) (y - mTouchStartY);
  wm.updateViewLayout(tv, params);

 }

 @Override
 public void onReceive(Context context, Intent intent) {
  mcontext = context;
  
  if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
   // 如果是去電(撥出)
   // mCallEvent = CALL_EVENT_OUTGOING;
   MyApplication.NumberType = 1;
   String mCallNumber = intent
     .getStringExtra(Intent.EXTRA_PHONE_NUMBER);
   Log.i(TAG, "call OUT:" + mCallNumber);
   MyApplication.PhoneNum = mCallNumber;

   wm = null;
   wm = (WindowManager) context.getApplicationContext()
     .getSystemService(Context.WINDOW_SERVICE);
   // WindowManager.LayoutParams
   params = new WindowManager.LayoutParams();
   
   params.type = LayoutParams.TYPE_PHONE;
   

   // 設置Window flag
   params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
     | LayoutParams.FLAG_NOT_FOCUSABLE;
   params.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角 為實現拖拽,必須加上這個,不然會漂移,不信你試試。
   // 以屏幕左上角為原點,設置x、y初始
   params.x = 20;
   params.y = 5;
   params.width = WindowManager.LayoutParams.WRAP_CONTENT;
   params.height = WindowManager.LayoutParams.WRAP_CONTENT;
   params.format = PixelFormat.RGBA_8888;
   tv = new Button(context);
   
   tv.setText("雙擊查看:\n" + mCallNumber + "的記錄");// +sds);

   wm.addView(tv, params);

   tv.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

     // 獲取相對屏幕的坐標,即以屏幕左上角為原點
     x = event.getRawX();
     y = event.getRawY() - 25; // 25是系統狀態欄的高度

     switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
      // 獲取相對View的坐標,即以此View左上角為原點
      mTouchStartX = event.getX();
      mTouchStartY = event.getY();

      break;
     case MotionEvent.ACTION_MOVE:
      updateViewPosition();

      break;
     // 判斷手勢點擊起來的時候:
     case MotionEvent.ACTION_UP:
      updateViewPosition();
      mTouchStartX = mTouchStartY = 0;

      if (curDate != null) {
       Date dateNow = new Date(System.currentTimeMillis());
       // date轉成毫秒
       long diff = dateNow.getTime() - curDate.getTime();

       // 這樣得到的差值是微秒級別

       if (diff < 400) {
        // 雙擊
        try {
         if (wm != null) {
          wm.removeView(tv);

         }
        } catch (Exception e) {

        }

        curDate = null;

        Intent service = new Intent();
        service.setClass(mcontext, TeleLogContent.class);
        service.putExtra("PhoneNumer",
          MyApplication.PhoneNum);
        service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mcontext.startActivity(service);

       } else {
        // 單擊

        curDate = new Date(System.currentTimeMillis());
       }
       // 這裡也是單擊
      } else {
       // 單擊

       curDate = new Date(System.currentTimeMillis());
      }

      break;
     }
     return true;
    }
   });
   // AskCallStateThread callStateThread = new
   // AskCallStateThread(context);
   // callStateThread.start();

  } else {
   
   String comeInNum = intent
     .getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
   
   System.out.println("來電號碼:" + comeInNum);

   TelephonyManager telM = (TelephonyManager) context
     .getSystemService(Context.TELEPHONY_SERVICE);

   // 設置一個監聽器
   telM.listen(new TelListener(context),
     PhoneStateListener.LISTEN_CALL_STATE);
  }
 }

}


以上主要是去電部分的提示框。(第一次寫android 的程序,所以經驗不足啊。見笑了)

下面是來電部分:


[java]
import java.util.Date; 
 
 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.PixelFormat; 
 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.view.View; 
 
import android.view.WindowManager; 
import android.view.View.OnTouchListener; 
import android.view.WindowManager.LayoutParams; 
 
import android.widget.Button; 
 
 
//監聽來電  
public class TelListener extends PhoneStateListener { 
    private Context context; 
    private float mTouchStartX; 
    private float mTouchStartY; 
    private float x; 
    private float y; 
    int type = 0; 
    private WindowManager wm; 
    WindowManager.LayoutParams params; 
    private Button tv; 
    int listview = 0; 
 
    int calli = 1; 
 
    public TelListener(Context context) { 
        this.context = context; 
    } 
 
    private void updateViewPosition() { 
        // 更新浮動窗口位置參數  
        params.x = (int) (x - mTouchStartX); 
        params.y = (int) (y - mTouchStartY); 
        wm.updateViewLayout(tv, params); 
 
    } 
 
    Date curDate = null; 
    String phoneNumber = ""; 
 
    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 
 
        super.onCallStateChanged(state, incomingNumber); 
 
        switch (state) { 
        case TelephonyManager.CALL_STATE_IDLE: 
            // 掛斷  
             
            break; 
        case TelephonyManager.CALL_STATE_OFFHOOK: 
            // 接聽  
             
 
            break; 
        case TelephonyManager.CALL_STATE_RINGING: 
            // 來電  
            MyApplication.NumberType = 2; 
            if (MyApplication.isCloses == 0) { 
                MyApplication.isCloses = 1; 
                MyApplication.PhoneNum = incomingNumber; 
                wm = null; 
                wm = (WindowManager) context.getApplicationContext() 
                        .getSystemService(Context.WINDOW_SERVICE); 
                params = new WindowManager.LayoutParams(); 
                params.type = LayoutParams.TYPE_PHONE; 
                params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL 
                        | LayoutParams.FLAG_NOT_FOCUSABLE; 
                params.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角  
                // 以屏幕左上角為原點,設置x、y初始  
                params.x = 20; 
                params.y = 5; 
                params.width = WindowManager.LayoutParams.WRAP_CONTENT; 
                params.height = WindowManager.LayoutParams.WRAP_CONTENT; 
                params.format = PixelFormat.RGBA_8888; 
                tv = new Button(context); 
                tv.setText("雙擊查看\n" + incomingNumber + "的記錄"); 
                wm.addView(tv, params); 
                tv.setOnTouchListener(new OnTouchListener() { 
 
                    @Override 
                    public boolean onTouch(View v, MotionEvent event) { 
 
                        // 獲取相對屏幕的坐標,即以屏幕左上角為原點  
                        x = event.getRawX(); 
                        y = event.getRawY() - 25; // 25是系統狀態欄的高度  
 
                        switch (event.getAction()) { 
                        case MotionEvent.ACTION_DOWN: 
                            // 獲取相對View的坐標,即以此View左上角為原點  
                            mTouchStartX = event.getX(); 
                            mTouchStartY = event.getY(); 
 
                            break; 
                        case MotionEvent.ACTION_MOVE: 
                            updateViewPosition(); 
 
                            break; 
                        // 判斷手勢點擊起來的時候:  
                        case MotionEvent.ACTION_UP: 
                            updateViewPosition(); 
                            mTouchStartX = mTouchStartY = 0; 
 
                            if (curDate != null) { 
                                Date dateNow = new Date(System 
                                        .currentTimeMillis()); 
                                // date轉成毫秒  
                                long diff = dateNow.getTime() 
                                        - curDate.getTime(); 
 
                                // 這樣得到的差值是微秒級別  
 
                                if (diff < 400) { 
                                    // 雙擊  
                                    try { 
                                        if (wm != null) { 
                                            wm.removeView(tv); 
                                            MyApplication.isCloses=0; 
                                        } 
                                    } catch (Exception e) { 
 
                                    } 
 
                                    curDate = null; 
 
                                    Intent service = new Intent(); 
                                    service.setClass(context, 
                                            TeleLogContent.class); 
                                    service.putExtra("PhoneNumer", 
                                            MyApplication.PhoneNum); 
                                    service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                                    context.startActivity(service); 
 
                                } else { 
                                    // 單擊  
 
                                    curDate = new Date(System 
                                            .currentTimeMillis()); 
                                } 
                                // 這裡也是單擊  
                            } else { 
                                // 單擊  
 
                                curDate = new Date(System.currentTimeMillis()); 
                            } 
 
                            break; 
                        } 
                        return true; 
                    } 
                }); 
            } 
            break; 
 
        } 
 
    } 

import java.util.Date;


import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;

import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;

import android.widget.Button;


//監聽來電
public class TelListener extends PhoneStateListener {
 private Context context;
 private float mTouchStartX;
 private float mTouchStartY;
 private float x;
 private float y;
 int type = 0;
 private WindowManager wm;
 WindowManager.LayoutParams params;
 private Button tv;
 int listview = 0;

 int calli = 1;

 public TelListener(Context context) {
  this.context = context;
 }

 private void updateViewPosition() {
  // 更新浮動窗口位置參數
  params.x = (int) (x - mTouchStartX);
  params.y = (int) (y - mTouchStartY);
  wm.updateViewLayout(tv, params);

 }

 Date curDate = null;
 String phoneNumber = "";

 @Override
 public void onCallStateChanged(int state, String incomingNumber) {

  super.onCallStateChanged(state, incomingNumber);

  switch (state) {
  case TelephonyManager.CALL_STATE_IDLE:
   // 掛斷
   
   break;
  case TelephonyManager.CALL_STATE_OFFHOOK:
   // 接聽
   

   break;
  case TelephonyManager.CALL_STATE_RINGING:
   // 來電
   MyApplication.NumberType = 2;
   if (MyApplication.isCloses == 0) {
    MyApplication.isCloses = 1;
    MyApplication.PhoneNum = incomingNumber;
    wm = null;
    wm = (WindowManager) context.getApplicationContext()
      .getSystemService(Context.WINDOW_SERVICE);
    params = new WindowManager.LayoutParams();
    params.type = LayoutParams.TYPE_PHONE;
    params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
      | LayoutParams.FLAG_NOT_FOCUSABLE;
    params.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角
    // 以屏幕左上角為原點,設置x、y初始
    params.x = 20;
    params.y = 5;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.format = PixelFormat.RGBA_8888;
    tv = new Button(context);
    tv.setText("雙擊查看\n" + incomingNumber + "的記錄");
    wm.addView(tv, params);
    tv.setOnTouchListener(new OnTouchListener() {

     @Override
     public boolean onTouch(View v, MotionEvent event) {

      // 獲取相對屏幕的坐標,即以屏幕左上角為原點
      x = event.getRawX();
      y = event.getRawY() - 25; // 25是系統狀態欄的高度

      switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
       // 獲取相對View的坐標,即以此View左上角為原點
       mTouchStartX = event.getX();
       mTouchStartY = event.getY();

       break;
      case MotionEvent.ACTION_MOVE:
       updateViewPosition();

       break;
      // 判斷手勢點擊起來的時候:
      case MotionEvent.ACTION_UP:
       updateViewPosition();
       mTouchStartX = mTouchStartY = 0;

       if (curDate != null) {
        Date dateNow = new Date(System
          .currentTimeMillis());
        // date轉成毫秒
        long diff = dateNow.getTime()
          - curDate.getTime();

        // 這樣得到的差值是微秒級別

        if (diff < 400) {
         // 雙擊
         try {
          if (wm != null) {
           wm.removeView(tv);
           MyApplication.isCloses=0;
          }
         } catch (Exception e) {

         }

         curDate = null;

         Intent service = new Intent();
         service.setClass(context,
           TeleLogContent.class);
         service.putExtra("PhoneNumer",
           MyApplication.PhoneNum);
         service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(service);

        } else {
         // 單擊

         curDate = new Date(System
           .currentTimeMillis());
        }
        // 這裡也是單擊
       } else {
        // 單擊

        curDate = new Date(System.currentTimeMillis());
       }

       break;
      }
      return true;
     }
    });
   }
   break;

  }

 }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved