阻止传出呼叫和文本(广播接收器或服务?) [英] Blocking outgoing calls and texts (BroadcastReceiver or Service?)
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/7729991/how-to-block-outgoing-calls-and-texts-to-contacts-$p$pvent-drunk-dialing\">How阻止传出来电和短信的联系人(prevent醉拨号)
块引用>有关我的计算机科学课程之一,我想提出一个应用程序来prevent人自醉拨号。基本上,用户便开始喝酒,直到他们通过清醒测试(回答数学问题),它关闭所有呼出/文功能之前,点击一个按钮。我被困在如何阻止,直到他们正确地回答这个问题时,他们preSS开始按钮,呼叫/文本。到目前为止,似乎大多数人建议使用一个BroadcastReceiver来阻止呼叫,但我不认为这会在我的情况下工作,因为你不能启动和停止广播接收器,当一个按钮开始按钮被按下或正确的文本输入。我缺少的东西吗?是否有可能使用后台服务,而不是实现这一目标还是有以这种方式使用一个BroadcastReceiver的好办法?
下面是我的code:
公共类MainActivity延伸活动{ INT的答案; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 最终的EditText userInput =(EditText上)findViewById(R.id.answerinput);
userInput.setEnabled(假); //这对于开始喝按钮创建按钮对象
最终按钮leftbutton =(按钮)findViewById(R.id.leftbutton); //这个为拨打电话按钮创建按钮对象
最终按钮rightbutton =(按钮)findViewById(R.id.rightbutton);
rightbutton.setEnabled(假); leftbutton.setOnClickListener(新View.OnClickListener(){
/ **
*说明:此方法用于监听的拨打电话按钮的点击。一旦
*单击该按钮,此方法将调用其他方法,以创建一个
*随机数学题,显示此问题给用户,并检查用户是否
*回答正确。如果他们这么做,那么这个方法将调用拨号器,使
* 一个电话。
* @author马特
* @params一个视图对象看到按钮
* @返回无效
*抛出:无
* /
公共无效的onClick(视图v){
rightbutton.setEnabled(真);
leftbutton.setEnabled(假);
}
});
rightbutton.setOnClickListener(新View.OnClickListener(){
/ **
*说明:此方法侦听发送文本按钮的点击。一旦
*单击该按钮,此方法将调用其他方法,以创建一个
*随机数学题,显示此问题给用户,并检查用户是否
*回答正确。如果他们这么做,则此方法将调用短信服务
*以允许用户发送文本。
* @author马特
* @params一个视图对象看到按钮
* @返回无效
*抛出:无
* /
公共无效的onClick(视图v){
答案= createMathProblem();
userInput.setEnabled(真);
}
}); userInput.setOnKeyListener(新OnKeyListener(){ 公共布尔安其(视图V,INT键code,KeyEvent的事件){
字符串文本= userInput.getText()的toString()。
//如果事件上的Enter键一键按下事件
如果((event.getAction()== KeyEvent.ACTION_DOWN)
&功放;&安培; (关键code == KeyEvent.KEY code_ENTER)){
如果(matchAnswer(答案的Integer.parseInt(文本))){
leftbutton.setEnabled(真);
rightbutton.setEnabled(假);
userInput.setEnabled(假);
//飞机();
Toast.makeText(MainActivity.this,正确!,Toast.LENGTH_LONG).show();
}
返回true;
}
返回false;
}
}); } / **
*说明:此方法检查是否用户回答数学题
*正确。
* @author马特
* @params正确答案的数学题和用户的输入答案
* @返回真或假的取决于如果用户正确回答
*抛出:无
* /
公共布尔matchAnswer(INT correctAnswer,诠释userAnswer){
返回(correctAnswer == userAnswer);
} / **
*说明:此方法为用户创建一个随机数学题
*答案,它显示在屏幕上。
* @author马特
* @params无
返回:正确答案的数学题。
*抛出:无
* /
公众诠释createMathProblem(){
INT random1 =(INT)(的Math.random()* 100);
INT random2 =(INT)(的Math.random()* 100);
INT答案= random1 + random2;
Toast.makeText(MainActivity.this,random1 +++ random2 +?=,Toast.LENGTH_LONG).show();
返回的答案;
}
}
解决方案您可以帮助从下面的链接:
有关检测呼出电话:
的http://www.krvarma.com/2010/08/detecting-incoming-and-outgoing-calls-in-android/
和阻止传出呼叫:
<一个href=\"http://$c$c.google.com/p/unlocking-android/source/browse/chapter7/trunk/src/com/msi/manning/telephonyexplorer/OutgoingCallReceiver.java?spec=svn78&r=78\" rel=\"nofollow\">http://$c$c.google.com/p/unlocking-android/source/browse/chapter7/trunk/src/com/msi/manning/telephonyexplorer/OutgoingCallReceiver.java?spec=svn78&r=78
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 */ 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:
这篇关于阻止传出呼叫和文本(广播接收器或服务?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!