簡體   English   中英

android'PhoneStateListener'無法收聽呼叫?

[英]android 'PhoneStateListener' can't listen the call in?

這是AddressService.java:我不知道為什么我無法收到呼叫的廣播。我已經在AndroidManifest中注冊了它,但是它無法正常工作'不知道為什么,我不能運行該程序,我找不到任何錯誤。

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;

    }

}

AndroidMainfest.xml的一部分如下:我已經在其中添加了密碼子。

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

slm;我認為您必須在清單中添加權限:

android:name =“ android.permission.READ_PHONE_STATE” />和:

android:name =“ android.permission.PROCESS_OUTGOING_CALLS” />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM