本文介绍了如何阻止呼出电话和短信联系人(prevent醉拨号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出为prevents用户打电话或发短信,而他们都醉了一类Android应用程序。基本上,他们开始饮用之前,他们启动应用程序,它会阻止所有传出的电话和短信,以自己的人脉,直到他们回答数学问题来证明自己的清醒。我已经打了一个障碍,同时试图阻止电话和短信,因为我似乎无法得到BroadcastReceivers做到这一点。基本上,当用户点击开始按钮,我想要的BroadcastReceiver开始监听和拦截拨出电话,直到用户通过清醒测试(输入正确答案的数学问题)。有没有办法做到这一点?

由于一吨!马特

下面是我的主要活动:

 公共类MainActivity延伸活动{

    INT的答案;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        最后的EditText userInput =(EditText上)findViewById(R.id.answerinput);
        userInput.setEnabled(假);

        //此为开始喝按钮,创建一个按钮对象
        最终按钮leftbutton =(按钮)findViewById(R.id.leftbutton);

        //此为拨打电话按钮,创建一个按钮对象
        最终按钮rightbutton =(按钮)findViewById(R.id.rightbutton);
        rightbutton.setEnabled(假);

        leftbutton.setOnClickListener(新View.OnClickListener(){
            / **
             *说明:此方法侦听的拨打电话按钮的点击。一旦
             *单击该按钮,此方法将调用其他方法,以创建一个
             *随机数学题,显示此问题给用户,并检查用户看到
             *正确地回答。如果他们这么做,那么这个方法将调用拨号器,使
             *一个电话。
             * @author马修
             * @params一个视图对象看到按钮
             * @返回无效
             * @throws无
             * /
            公共无效的onClick(视图v){
                rightbutton.setEnabled(真正的);
                leftbutton.setEnabled(假);
            }
        });


        rightbutton.setOnClickListener(新View.OnClickListener(){
            / **
             *说明:此方法侦听发送文本按钮的点击。一旦
             *单击该按钮,此方法将调用其他方法,以创建一个
             *随机数学题,显示此问题给用户,并检查用户看到
             *正确地回答。如果他们这么做,那么这个方法将调用短信服务
             *以允许用户发送文本。
             * @author马修
             * @params一个视图对象看到按钮
             * @返回无效
             * @throws无
             * /
            公共无效的onClick(视图v){
                答案= createMathProblem();
                userInput.setEnabled(真正的);
            }
        });

        userInput.setOnKeyListener(新OnKeyListener(){

            公共布尔onKey(视图V,INT关键code,KeyEvent的事件){
                字符串文本= userInput.getText()的toString()。
                //如果事件是对输入按钮的键 - 向下事件
                如果((event.getAction()== KeyEvent.ACTION_DOWN)
                        &功放;&安培; (钥匙code == KeyEvent.KEY code_ENTER)){
                    如果(matchAnswer(答案的Integer.parseInt(文本))){
                        leftbutton.setEnabled(真正的);
                        rightbutton.setEnabled(假);
                        userInput.setEnabled(假);
                        //飞机();
                        Toast.makeText(MainActivity.this,正确!,Toast.LENGTH_LONG).show();
                    }
                    返回true;
                }
                返回false;
            }
        });

    }

    / **
     *说明:此方法检查,如果用户回答数学题
     *正确。
     * @author马修
     * @params正确答案的数学问题,用户的输入答案
     * @返回真或假取决于用户是否正确回答
     * @throws无
     * /
    公共布尔matchAnswer(INT correctAnswer,诠释userAnswer){
        返回(correctAnswer == userAnswer);
    }

    / **
     *说明:此方法为用户创建一个随机数学题
     *答案,并将其显示在屏幕上。
     * @author马修
     * @params无
     返回:正确答案的数学题。
     * @throws无
     * /
    公众诠释createMathProblem(){
        INT random1 =(int)的(的Math.random()* 100);
        INT random2 =(int)的(的Math.random()* 100);
        INT答案= random1 + random2;
        Toast.makeText(MainActivity.this,random1 +++ random2 +?=,Toast.LENGTH_LONG).show();
        返回的答案;
    }


}
 

下面是我的BroadcastReceiver的:

 公共类CallTextReceiver扩展的BroadcastReceiver {

    公共无效的onReceive(上下文的背景下,意图意图){
        如果(intent.getAction()。等于(Intent.ACTION_NEW_OUTGOING_CALL)){
            如果(getResultData()!= NULL){
                setResultData(空);

                Toast.makeText(背景下,阻止,Toast.LENGTH_LONG).show();
            }
        }
    }
}
 

解决方案

由于CyanogenMod的是开源的,你也许能够找出这个问题的答案通过查看电话护目镜code:的

由于它实现了你试图写一个单独的应用程序,这是很好的人没有运行CM7同样的事情。

I am making an android app for a class that prevents users from calling or texting while they are drunk. Basically, before they start drinking, they start the app which would block all outgoing calls and texts to their contacts until they answer a math problem to prove their sobriety. I've hit a snag while trying to block calls and texts because I can't seem to get BroadcastReceivers to accomplish this. Basically when the user hits a "Start" button, I'd like the BroadcastReceiver to start listening for and blocking outgoing calls until the user passes a sobriety test (inputs correct answer to math problem). Is there any way to accomplish this?

Thanks a ton!Matt

Here is my main activity:

public class MainActivity extends Activity {

    int answer;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText userInput = (EditText) findViewById(R.id.answerinput);
        userInput.setEnabled(false);

        //This creates the button object for the "Start Drinking" button
        final Button leftbutton = (Button) findViewById(R.id.leftbutton);

        //This creates the button object for the "Make Call" button
        final Button rightbutton = (Button) findViewById(R.id.rightbutton);
        rightbutton.setEnabled(false);

        leftbutton.setOnClickListener(new View.OnClickListener() {
            /**
             * Description: This method listens for a click on the "Make Call" button. Once
             * the button is clicked, this method will call other methods in order to create a 
             * random math problem, display this problem to the user, and check to see if the user
             * answered correctly. If they did, then this method will call the dialer to make
             * a phone call.
             * @author Matthew
             * @params A view object to see the button
             * @return void
             * @throws None
             */
            public void onClick(View v) {
                rightbutton.setEnabled(true);
                leftbutton.setEnabled(false);
            }
        });


        rightbutton.setOnClickListener(new View.OnClickListener() {
            /**
             * Description: This method listens for a click on the "Send Text" button. Once
             * the button is clicked, this method will call other methods in order to create a 
             * random math problem, display this problem to the user, and check to see if the user
             * answered correctly. If they did, then this method will call the text messaging service
             * to allow the user to send a text. 
             * @author Matthew
             * @params A view object to see the button
             * @return void
             * @throws None
             */
            public void onClick(View v) {
                answer = createMathProblem();
                userInput.setEnabled(true);
            }
        });

        userInput.setOnKeyListener(new OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                String text = userInput.getText().toString();
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    if (matchAnswer(answer, Integer.parseInt(text))) {
                        leftbutton.setEnabled(true);
                        rightbutton.setEnabled(false);
                        userInput.setEnabled(false);
                        //airplane();
                        Toast.makeText(MainActivity.this, "Correct!", Toast.LENGTH_LONG).show();
                    }
                    return true;
                }
                return false;
            }
        });

    }

    /**
     * Description: This method checks to see if the user answered the math problem
     * correctly.
     * @author Matthew
     * @params The correct answer to the math problem and the user's input answer
     * @return True or false depending on if the user answered correctly
     * @throws None
     */
    public boolean matchAnswer(int correctAnswer, int userAnswer) {
        return (correctAnswer == userAnswer);
    }

    /**
     * Description: This method creates a random math problem for the user to 
     * answer and displays it to the screen. 
     * @author Matthew
     * @params None
     * @return The correct answer to the math problem.
     * @throws None
     */
    public int createMathProblem() {
        int random1 = (int) (Math.random() * 100);
        int random2 = (int) (Math.random() * 100);
        int answer = random1 + random2;
        Toast.makeText(MainActivity.this, random1 + " + " + random2 + " = ?", Toast.LENGTH_LONG).show();
        return answer;
    }


}

Here is my BroadcastReceiver:

public class CallTextReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            if (getResultData() != null) {
                setResultData(null);

                Toast.makeText(context, "Blocked", Toast.LENGTH_LONG).show();
            }
        }
    }
}
解决方案

Since CyanogenMod is open source, you might be able to find out the answer to this by looking through the Phone Goggles code: http://www.cyanogenmod.com/features/phone-goggles

Since it accomplishes the same thing you are trying to write as a separate app, which is nice for people not running a CM7.

这篇关于如何阻止呼出电话和短信联系人(prevent醉拨号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 21:39