가능한 중복은 :
내 컴퓨터 과학 클래스 중 하나를 들어
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;
}
}
움직이는 점에 손가락을 올려 놓는 것처럼 육체적 인 기민성을 필요로하는 무언가를하지 마십시오. – JohnFx