如何检测来电与广播接收器的帮助? [英] How to detect incoming call with the help of Broadcast Receiver?
问题描述
我想认识通广播接收来电。我不能这样做!逸岸,我无法'触发'广播!
下面是我的code:
activate.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
Toast.makeText(getApplicationContext(),点击,1).show();
最后弦乐BROADCAST_ACTION_NAME =.BroadcastMM;
意向意图=新的意图();
intent.setAction(BROADCAST_ACTION_NAME);
sendBroadcast(意向);
}
}
我不知道,如果这sendBroadcast是以往任何时候都触发!在我的广播接收机文件:
公共无效的onReceive(上下文的背景下,意图意图)
{
如果(intent.getAction()==android.intent.action.PHONE_STATE){
Toast.makeText(C,广播被解雇了!,1).show();}
捆绑额外= intent.getExtras();
字符串状态= extras.getString(TelephonyManager.EXTRA_STATE);
如果(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Toast.makeText(上下文中,振铃,1).show();
}
}
我的清单文件:
<接收机器人:名称=。BroadcastMM>
<意向滤光器>
<作用机器人:名称=android.intent.action.PHONE_STATE>
< /作用>
&所述; /意图滤光器>
< /接收器>
<使用-权限的Android:名称=android.permission.READ_PHONE_STATE/>
有一些逻辑,我失踪?我很新至Android,所以请帮助我。
intent.getAction()==android.intent.action.PHONE_STATE
应
TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction());
因为这是你如何比较字符串(使用等于()
)。
此外,C用来播放$ C $,不应该播出 - 没有BroadcastMM。
的行动。试着做一个显性化,而不是:
意向意图=新的意图(v.getContext(),BroadcastMM.class);
sendBroadcast(意向);
这也可能是您不能播放 android.intent.action.PHONE_STATE
,所以你的如果
如果你让一个明确的意图将不会被执行。
如果你真的想检查您的BroadcastReceiver是工作,把打印输出/所有IFS外敬酒。然后,一旦你建立了BroadcastReceiver的回应,做你的支票。但请记住,既然你只能听一个意向过滤器时,如果检查如果目的是 PHONE_STATE
意图是有点多余。
I'm trying to recognize incoming calls in thru a broadcast receiver. I'm UNABLE to do so! Infact, I'm unable to 'trigger' the broadcast!
Here's my code:
activate.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Clicked",1).show();
final String BROADCAST_ACTION_NAME = ".BroadcastMM";
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION_NAME);
sendBroadcast(intent);
}
}
I dunno if this 'sendBroadcast' is ever triggered! In my Broadcast Receiver file:
public void onReceive(Context context, Intent intent)
{
if(intent.getAction()=="android.intent.action.PHONE_STATE"){
Toast.makeText(c,"BroadCast fired!",1).show();}
Bundle extras = intent.getExtras();
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Ringing", 1).show();
}
}
My manifest file:
<receiver android:name=".BroadcastMM" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Is there some logic I'm missing? I'm very new to Android, so please help me out.
intent.getAction()=="android.intent.action.PHONE_STATE"
should be
TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction());
Since this is how you compare Strings (with equals()
).
Also, the code you use to broadcast, should never broadcast - there is no ".BroadcastMM"
action. Try making an explicit one instead:
Intent intent = new Intent(v.getContext(),BroadcastMM.class);
sendBroadcast(intent);
It is also likely that you can't broadcast android.intent.action.PHONE_STATE
, so your if
won't be executed if you make an explicit Intent.
If you really want to check that your BroadcastReceiver is working, put printouts/Toasts outside all ifs. Then once you establish that the BroadcastReceiver responds, do your check. Keep in mind though, that since you only listen for one Intent-Filter, the if checking if the Intent is a PHONE_STATE
Intent is a bit redundant.
这篇关于如何检测来电与广播接收器的帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!