Blocking outgoing calls and texts (BroadcastReceiver or Service?) [duplicate]
Possible Duplicate:
How to block outgoing calls and texts to contacts (Prevent drunk dialing)
For one of my computer science classes, I am making an app to prevent people from drunk dialing. Basically the user clicks a button before they start drinking which turns off all outgoing call/text functionality until they pass a sobriety test (answer a math question). I'm stuck at how to block calls/texts from when they press the "Start" button until they answer the question correctly. So far it seems that most people recommend using a BroadcastReceiver to block calls, but I don't think this will work in my case because you can't start and stop a broadcast receiver when a button the "Start" button is pushed or the correct text is inputted. Am I missing something? Would it be possible to use a background service to accomplish this instead or is there a good way to use a BroadcastReceiver in this manner?
Here is my code:
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
开发者_StackOverflow社区 */
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;
}
}
You can take help from the following links:
For detecting outgoing calls:
http://www.krvarma.com/2010/08/detecting-incoming-and-outgoing-calls-in-android/
and for blocking outgoing calls:
http://code.google.com/p/unlocking-android/source/browse/chapter7/trunk/src/com/msi/manning/telephonyexplorer/OutgoingCallReceiver.java?spec=svn78&r=78
精彩评论