2011-10-12 86 views
0

可能重复:
How to block outgoing calls and texts to contacts (Prevent drunk dialing)阻止传出呼叫和文本(广播接收器或服务?)

对于我的计算机科学课程之一,我想提出一个应用程序,以防止醉酒的人拨号。基本上,用户在开始喝酒之前点击一个按钮,关闭所有呼出/文本功能,直到他们通过清醒测试(回答数学问题)。我被困在如何阻止他们按下“开始”按钮之前的电话/短信,直到他们正确回答问题。到目前为止,大多数人似乎都建议使用BroadcastReceiver来阻止通话,但我认为这不适用于我的情况,因为当按下“开始”按钮时您无法启动和停止广播接收机,或者正确的文本被输入。我错过了什么吗?是否有可能使用后台服务来完成此任务,或者有没有一种以这种方式使用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 Matt 
      * @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 Matt 
      * @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 Matt 
    * @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 Matt 
    * @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; 
    } 


} 
+0

忘记数学问题做一些需要身体灵巧的东西,比如让你的手指保持移动点。 – JohnFx

回答