简体   繁体   English

应用被杀死时,Android BroadcastReceiver无法正常工作

[英]Android BroadcastReceiver Not Working When App Killed

I'm trying to do an app that gets the caller number, send it to my web api, get the response and show it response as pop up on the screen which includes additional information about call. 我正在尝试做一个获取呼叫者号码的应用程序,将其发送到我的网络api,获取响应并将响应显示为屏幕上弹出的窗口,其中包括有关呼叫的其他信息。 This won't be a public app. 这不会是公共应用。 When I'm testing on emulator it works in all cases as expected. 当我在模拟器上进行测试时,它可以在所有情况下按预期工作。 I installed app on my mobile phone and tested it as I do on emulator and worked as expected. 我在手机上安装了应用程序,并像在模拟器上一样对其进行了测试,并按预期工作。 When i press "Recent Apps" button and swipe it out, it suddenly stops receiving call and display popup. 当我按“最近使用的应用程序”按钮并将其滑出时,它突然停止接听电话并显示弹出窗口。 I have proper AndroidManifest entries. 我有正确的AndroidManifest条目。

AndroidManifest.xml AndroidManifest.xml中

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />

//other parts of manifest file


<receiver
        android:name=".CallReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

BroadcastReceiver Class BroadcastReceiver类

public class PhonecallReceiver extends BroadcastReceiver  {

//some declarations

@Override
public void onReceive(Context context, Intent intent) {

    Date currentTime = Calendar.getInstance().getTime();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("OnReceive");
    myRef.setValue("PhonecallReceiver onReceive.");
    //other codes

I added Firebase to see whats going on when a call comes. 我添加了Firebase,以查看来电时发生的情况。 On emulator it puts an entry to Firebase in all cases(App swiped out, screen locked etc.) but when on my phone it only adds record to Firebase when app is active in Recent apps. 在模拟器上,在所有情况下(应用程序被刷出,屏幕锁定等),它都会在Firebase中放置一个条目,但是在我的手机上时,仅当应用程序在“最近的”应用程序中处于活动状态时,它才会向Firebase添加记录。

I need to receive incoming call data even when app is swiped out from recent apps. 即使从最近的应用程序中刷出了应用程序,我也需要接收来电数据。

Phone: Xiaomi Redmi Note 4 on Android 7.0 with latest available updates applied. 电话:Android 7.0上的小米Redmi注意事项4,应用了最新的可用更新。

Fixed the problem with another approach. 使用另一种方法解决了该问题。 Instead registering BroadcastReceiver in the AndroidManifest file, created a Foreground service and programmatically created BroadcastReceiver there. 而是在AndroidManifest文件中注册BroadcastReceiver,创建一个Foreground服务,并在那里以编程方式创建BroadcastReceiver。 Also when Activity get destroyed it may kill the service as well. 同样,当Activity被销毁时,它也可能会终止该服务。 I added restart service code at onTaskRemoved method to restart service when it gets killed along with app. 我在onTaskRemoved方法上添加了重新启动服务代码,以便在与应用程序一起被杀死时重新启动服务。 Now it works in all cases. 现在,它适用于所有情况。 As downside, it has to show ongoing notification unless user blocks it which is not a problem in my case. 不利的一面是,除非用户阻止它,否则它必须显示正在进行的通知,这对我而言不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:BroadcastReceiver重启已杀死/崩溃的应用程序 - Android: BroadcastReceiver to restart killed/crashed app 当App被杀死时,BroadcastReceiver没有被调用 - BroadcastReceiver not getting called when App is killed 当应用程序被杀时,Android广播接收器无法正常工作 - Android Broadcast Receiver not working when the app is killed 当应用程序被杀死时,Android 警报管理器不起作用 - Android Alarm Manager Not working when app is Killed 在杀死应用程序的同时调用BroadcastReceiver时是否启动了android.intent.action.MAIN活动? - Is the android.intent.action.MAIN Activity launched when a BroadcastReceiver is called while the app is killed? Android BroadcastReceiver 使应用程序无法运行时崩溃 - Android BroadcastReceiver makes app crash when app is not working BroadcastReceiver无法在我的Android应用程序中运行 - BroadcastReceiver is not working in my android app 使广播接收器监听事件,即使应用无服务而被杀死 - Make a broadcastreceiver listening for events even when app is killed without service 从Play商店安装应用程序时,Android BroadcastReceiver无法正常工作 - Android BroadcastReceiver not working when app is installed from play store 应用程序未运行时 BroadcastReceiver 不工作 - BroadcastReceiver not working when app is not running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM