简体   繁体   中英

android 'PhoneStateListener' can't listen the call in?

This is the AddressService.java: I don't know why i can't receive the boradcast of the call in. I have registe it in the AndroidManifest,but it can't work' Don't know why, I can't run the program, I can't find any error in it.

package com.example.mobilesafe.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.mobilesafe.R;
import com.example.mobilesafe.db.dao.NumberAddressQueryUtils;


public class AddressService extends Service {

    /**
     * 窗体管理者
     */
    private WindowManager wm;
    private View view;

    /**
     * 电话服务
     */

    private TelephonyManager tm;
    private MyListenerPhone listenerPhone;

    private OutCallReceiver receiver;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    // 服务里面的内部类
    //广播接收者的生命周期和服务一样
    class OutCallReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // 这就是我们拿到的播出去的电话号码
            String phone = getResultData();
            // 查询数据库
            String address = NumberAddressQueryUtils.queryNumberAddress(phone);

//          Toast.makeText(context, address, 1).show();
            myToast(address);
        }

    }

    private class MyListenerPhone extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            // state:状态,incomingNumber:来电号码
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:// 来电铃声响起
                    // 查询数据库的操作
                    String address = NumberAddressQueryUtils
                            .queryNumberAddress(incomingNumber);

//              Toast.makeText(getApplicationContext(), address, 1).show();
                    myToast(address);

                    break;

                case TelephonyManager.CALL_STATE_IDLE://电话的空闲状态:挂电话、来电拒绝
                    //把这个View移除
                    if(view != null ){
                        wm.removeView(view);
                    }


                    break;

                default:
                    break;
            }
        }

    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        // 监听来电
        listenerPhone = new MyListenerPhone();
        tm.listen(listenerPhone, PhoneStateListener.LISTEN_CALL_STATE);

        //用代码去注册广播接收者
        receiver = new OutCallReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
        registerReceiver(receiver, filter);

        //实例化窗体
        wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    }

    /**
     * 自定义土司
     * @param address
     */
    public void myToast(String address) {
        view =   View.inflate(this, R.layout.address_show, null);
        TextView textview  = (TextView) view.findViewById(R.id.tv_address);

        //"半透明","活力橙","卫士蓝","金属灰","苹果绿"
        int [] ids = {R.drawable.call_locate_white,R.drawable.call_locate_orange,R.drawable.call_locate_blue
                ,R.drawable.call_locate_gray,R.drawable.call_locate_green};
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        view.setBackgroundResource(ids[sp.getInt("which", 0)]);
        textview.setText(address);
        //窗体的参数就设置好了
        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;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_TOAST;
        wm.addView(view, params);

    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        // 取消监听来电
        tm.listen(listenerPhone, PhoneStateListener.LISTEN_NONE);
        listenerPhone = null;

        //用代码取消注册广播接收者
        unregisterReceiver(receiver);
        receiver = null;

    }

}

The part of AndroidMainfest.xml is following: I have added the aciton in it.

        <service android:name="com.example.mobilesafe.service.AddressService">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </service>

slm;I think you have to add permissions in your manifest :

android:name="android.permission.READ_PHONE_STATE" /> and :

android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM