BroadCastReceiver 监听来电并在时间间隔之间工作

发布于 2024-12-07 15:51:36 字数 1889 浏览 0 评论 0原文

我想开发一个接收器类来监听电话状态。我想使用传入的电话号码来处理 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2024-12-14 15:51:36

在清单文件中注册您的接收器,如下所示:

<receiver android:name=".PhoneStateReceiver">
   <intent-filter>
       <action android:name="android.intent.action.PHONE_STATE" />
   </intent-filter>
</receiver>

Register your receiver in your manifest file like this:

<receiver android:name=".PhoneStateReceiver">
   <intent-filter>
       <action android:name="android.intent.action.PHONE_STATE" />
   </intent-filter>
</receiver>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文