BroadCastReceiver 监听来电并在时间间隔之间工作
我想开发一个接收器类来监听电话状态。我想使用传入的电话号码来处理 doSomething()。
我的需要是我的接收器应该在某个时间间隔(例如 12:30PM 到 01:00PM)之间工作,而不是这个时间间隔我的接收器不应该工作。
我做了什么:
这是我的 BroadcastReceiver 类:
public class MyTest extends BroadcastReceiver {
protected AudioManager audioManager;// = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
protected Context context;
private ITelephony telephonyService;
@Override
public void onReceive(Context context, Intent intent) {
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
this.context = context;
String action = intent.getAction();
if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE")){
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
//Incoming call
doSomething(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
}
}
else {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage message = SmsMessage.createFromPdu((byte[])pdus[0]);
if(!message.isEmail())
doSomething(message.getOriginatingAddress());
}
}
}
我在我的活动中使用它:
registerReceiver(new MyTest(), new IntentFilter("android.intent.action.PHONE_STATE"));
在清单中我将其声明为
<receiver android:name=".MyTest"></receiver>
问题:
如果我的应用程序在前台运行,它可以工作,但是当我关闭我的应用程序时,它无法工作。 我没有在 onPause() 或 Activity 生命周期的其他方法上编写任何代码。
我做错了什么?还有其他方法吗?
I want to develope a receiver class which will listen Phone state. And I want to work on doSomething() using incoming phone number.
And my need is that my receiver should work between some time interval (like 12:30PM to 01:00PM), other than this time interval my receiver should not work.
What I did yet :
This is my BroadcastReceiver class :
public class MyTest extends BroadcastReceiver {
protected AudioManager audioManager;// = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
protected Context context;
private ITelephony telephonyService;
@Override
public void onReceive(Context context, Intent intent) {
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
this.context = context;
String action = intent.getAction();
if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE")){
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
//Incoming call
doSomething(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
}
}
else {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage message = SmsMessage.createFromPdu((byte[])pdus[0]);
if(!message.isEmail())
doSomething(message.getOriginatingAddress());
}
}
}
And I am using it as in my activity :
registerReceiver(new MyTest(), new IntentFilter("android.intent.action.PHONE_STATE"));
In manifest I declaired it as
<receiver android:name=".MyTest"></receiver>
Problem :
Its working if my application is running on foreground, but when I closes my application its not working.
I didn't write any code on onPause() or other methods of Activity life cycle.
What am I doing wrong? Is there any other way to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在清单文件中注册您的接收器,如下所示:
Register your receiver in your manifest file like this: