18、高级工具--显示来电与外拨电话号码的归属地

新建包com.example.mobilesafe.service,在该包下创建服务ShowCallLocationService,继承自service。

package com.example.mobilesafe.service;

import android.app.ActionBar;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.example.mobilesafe.R;
import com.example.mobilesafe.db.NumberAddressDao;

/**
 * Created by sing on 14-1-13.
 * desc:监听电话呼入的服务
 */
public class ShowCallLocationService extends Service {

    //电话管理器
    private TelephonyManager tm;

    //窗体管理器
    private WindowManager windowManager;

    //电话状态改变的监听器
    private MyPhoneListener listener;

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        listener = new MyPhoneListener();
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    }

    private class MyPhoneListener extends PhoneStateListener {

        private View view;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                //电话铃声正在响
                case TelephonyManager.CALL_STATE_RINGING:
                    //查询出电话号码的归属地
                    NumberAddressDao numberAddressDao = new NumberAddressDao(getApplicationContext());
                    String address = numberAddressDao.getAddress(incomingNumber);

                    //通过布局填充器将布局转换为view
                    view = View.inflate(getApplicationContext(), R.layout.show_address, null);
                    //LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_show_address);
                    TextView tv = (TextView) view.findViewById(R.id.tv_show_address);
                    tv.setText(address);

                    final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
                    params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    //显示在窗体上的style为半透明
                    params.format = PixelFormat.TRANSLUCENT;
                    //窗体view的类型为吐司
                    params.type = WindowManager.LayoutParams.TYPE_TOAST;
                    windowManager.addView(view, params);
                    break;
                //电话空闲状态
                case TelephonyManager.CALL_STATE_IDLE:
                    //将窗体上的吐司移除
                    if (view != null) {
                        windowManager.removeView(view);
                        view = null;
                    }
                    break;
                //电话接通状态
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //将窗体上的吐司移除
                    if (view != null) {
                        windowManager.removeView(view);
                        view = null;
                    }
                    break;
            }

            super.onCallStateChanged(state, incomingNumber);
        }

    }
}
其中显示电话号码归属地的布局文件show_address.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:gravity="center_vertical"
    android:id="@+id/ll_show_address" android:background="@drawable/call_locate_white">

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/notification"/>
    <TextView style="@style/content_text"
        android:id="@+id/tv_show_address"
        android:textColor="@android:color/white"
        android:text="归属地"/>

</LinearLayout>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值