unregisterReceiver()不工作 - android

时间:2014-08-23 02:57:05

标签: android

我正在尝试使用应用来控制所有来电,我按照了如何停止所有来电的教程"现在"我稍后会编辑它,但问题是,当我测试时,它实际上并没有注册接收器的接收器,所以它会在那里,所以我不能得到任何即使我试图取消接收器也要打电话:

控制通话的按钮代码:

mControlCalls.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(mIsActive)
                    mIsActive = false;
                else
                    mIsActive = true;

                if(mIsActive){

                    mControlCalls.setText(R.string.DeActivate_blockingUnknown);
                    getActivity().startService
                    (intent);
                    Log.d("Blocking" , "started service");

                }
                else if(!mIsActive){

                    mControlCalls.setText(R.string.Activate_blockingUnknown);

                    getActivity().stopService
                    (intent);

                    Log.d("Blocking" , "stopped service");

                }

            }

然后我有一项服务:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Service_Call_Reciver extends Service {


    BroadcastReceiver mCallBlocker;

    TelephonyManager mTelephonyManager;

    ITelephony mTelephonyService;

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

    @Override
    public int onStartCommand(Intent inent , int flags , int startId){


        mCallBlocker = new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {

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

                Class c = null;

                try{

                    c = Class.forName(mTelephonyManager.getClass().getName());

                }catch(ClassNotFoundException e){

                    Log.d("Blocking" , e + "");

                }

                Method m = null;

                try{

                    m = c.getDeclaredMethod("getITelephony");

                }catch(SecurityException e){

                    Log.d("Blocking" , e + "");

                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    Log.d("Blocking" , e + "");
                }

                m.setAccessible(true);

                try{

                    mTelephonyService = (ITelephony)m.invoke(mTelephonyManager);

                }
catch(IllegalArgumentException | IllegalAccessException | InvocationTargetException e){

                    Log.d("Blocking" , e + "");

                }

                mTelephonyManager.listen
                (callBlockListener , PhoneStateListener.LISTEN_CALL_STATE);

            }

            PhoneStateListener callBlockListener = new PhoneStateListener(){

                public void onCallStateChanged(int state , String incomingNumber){

                    if(state == TelephonyManager.CALL_STATE_RINGING){

                        try{

                            mTelephonyService.endCall();
                            Log.d("Blocking" ,mTelephonyManager.getLine1Number());

                        }catch(RemoteException e)
                        {

                            Log.d("Blocking" , e + "");

                        }

                    }

                }

            };

        }; // broadcastreciver end

        IntentFilter filter = new IntentFilter("android.intent.action.PHONE_STATE");
        registerReceiver(mCallBlocker, filter);

        //

        return START_STICKY;

    }

    @Override
    public void onDestroy()
    {

        super.onDestroy();

        if(mCallBlocker != null)
        {
        unregisterReceiver(mCallBlocker);
        mCallBlocker = null;
        Log.d("Blocking" , "it's not null");
        }
        else
            Log.d("Blocking" , "it's null");

    }

}

0 个答案:

没有答案