如何阻止向联系人拨出电话和短信(防止醉酒拨号)

时间:2011-10-11 17:26:39

标签: android

我正在制作一个Android应用程序,以防止用户在喝醉时打电话或发短信。基本上,在他们开始饮酒之前,他们启动应用程序,这将阻止所有拨出电话和文本给他们的联系人,直到他们回答数学问题来证明他们的清醒。我试图阻止呼叫和文本时遇到了麻烦,因为我似乎无法让BroadcastReceivers完成此任务。基本上当用户点击“开始”按钮时,我希望BroadcastReceiver开始监听和阻止拨出呼叫,直到用户通过清醒测试(输入正确的数学问题答案)。有没有办法实现这个目标?

非常感谢! 马特

这是我的主要活动:

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;
    }


}

这是我的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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

由于CyanogenMod是开源的,您可以通过查看Phone Goggles代码找到答案:http://www.cyanogenmod.com/features/phone-goggles

由于它完成了同样的事情,你试图将其作为一个单独的应用程序编写,这对于没有运行CM7的人来说非常好。