4

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();
            }
        }
    }
}
user990009
  • 51
  • 1
  • 4
  • 5
    Be sure not to block 911 calls. – Emile Cormier Oct 11 '11 at 17:29
  • 4
    @EmileCormier: or 112, or 999, or (01, 02 or 03) - depending on where in the world you are :) – Aleks G Oct 11 '11 at 17:31
  • see my answer this will help you http://stackoverflow.com/questions/7595092/how-to-block-outgoing-calls-and-text-sms/7595480#7595480 – Dharmendra Oct 11 '11 at 17:38
  • or here http://stackoverflow.com/questions/599443/android-how-to-hang-up-outgoing-call – Rafael T Oct 11 '11 at 17:41
  • 1
    Are you sure drunk people will be sober enough to disable airplane mode but still wasted enough to not be able to do `1+1`? – Marc B Oct 11 '11 at 17:49
  • @Aleks G: Please forgive my North American chauvinism. :-) – Emile Cormier Oct 11 '11 at 20:31
  • @Rafael I have implemented the broadcast receiver as shown in the link you provided, but it doesn't seem to be blocking any calls. Also, how would I make sure the Receiver is only listening once the user presses the button to block calls? Here is the code for my BroadcastReceiver class: '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); } } } }' – user990009 Oct 12 '11 at 05:27
  • @user990009 I think your broadcast reciever is not the LAST one, recieving this event. Does it have a Priority of 0? The Broadcast NEW_OUTGOING_CALL should be fired only once for any outgoing call. so your receiver gets fired each time it is registered (user clicked on 'block' button) and has to block it every time – Rafael T Oct 12 '11 at 08:58
  • Please tell me you not only disabled outgoing texts but incoming as well. That would be pure torture when you turn it on and now you're getting a million texts you can't respond to. What if someone texts asking if you want to get down? If this app gets popular then payphones will be all the rage again. – o_O Aug 24 '12 at 15:37

1 Answers1

0

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.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
mattdonders
  • 1,328
  • 1
  • 19
  • 42