Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

android - How to Block outgoing calls and Text SMS

I am developing an App in which I need to block the teenager from using mobile while driving, I need to block the call and sms. please help . any small hints and clues will do. please help me!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code will block your all call (INCOMING AND OUTGOING)

import java.lang.reflect.Method;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import com.CallLogApp.helper.BlockNumberHelper;
import com.CallLogApp.util.UDF;
import com.android.internal.telephony.ITelephony;

public class CustomPhoneStateListener extends PhoneStateListener {

    //private static final String TAG = "PhoneStateChanged";
    Context context;
    public CustomPhoneStateListener(Context context) {
        super();
        this.context = context;
    }

    @Override
    public void onCallStateChanged(int state, String outGoingNumber) {
        super.onCallStateChanged(state, outGoingNumber);

        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                endCallIfBlocked(outGoingNumber);
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                break;
            default:
                break;
        }

     }

     private void endCallIfBlocked(String outGoingNumber) {
        try {
            // Java reflection to gain access to TelephonyManager's
            // ITelephony getter
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> c = Class.forName(tm.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);  

            if (new BlockNumberHelper(context).isBlocked(outGoingNumber))
            {
                telephonyService = (ITelephony) m.invoke(tm);
                telephonyService.silenceRinger();
                telephonyService.endCall();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here CALL_STATE_OFFHOOK state will call each time when your call going to connect and when you received incoming call

There is no any method that you can know weather it is incoming call or outgoing call

But you can end call which is going to connect in both scenario


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...