简体   繁体   English

即使应用程序被杀死,也可以从广播接收器运行并获取事件

[英]Run and get event from Broadcast Receiver even when app is killed

I want to make application like TrueCaller.我想制作像 TrueCaller 这样的应用程序。 For that i need to get event of incoming call.为此,我需要获取来电事件。 For that i make Broadcast Receiver and it works fine till app is going to be killed.为此,我制作了广播接收器,它可以正常工作,直到应用程序将被杀死。

So i want to make run my Broadcast receiver even if app is killed.所以即使应用程序被杀死,我也想让我的广播接收器运行。

Previously i used Service for achieve this but in After Android O Service is not working.以前我使用Service来实现这一点,但在 After Android O Service 不起作用。 I thought that i can archive it by using WorkManager but i couldn't understand how to do it.我以为我可以使用 WorkManager 将其存档,但我不明白该怎么做。 So i want Service alternative or right way to archive this.所以我想要服务替代或正确的方式来存档。

Please help me to do it.请帮我做。

Thanks in advance.提前致谢。

CallEventBroadcastReceiver.java CallEventBroadcastReceiver.java

public class CallEventBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
  TelephonyManager telephonyManager = (TelephonyManager) 
context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String phoneNumber) {
        super.onCallStateChanged(state, phoneNumber);
        try {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                Log.e("phoneNumber", phoneNumber);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
      }
    }, PhoneStateListener.LISTEN_CALL_STATE);
  }
}

Try something like this.尝试这样的事情。 Unfortunately, I can offer you a possible solution for your issue only in Kotlin .不幸的是,我只能在Kotlin为您的问题提供可能的解决方案。

private lateinit var workManager: WorkManager

override fun onReceive(context: Context, intent: Intent) {
        super.onReceive(context, intent)
        val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
        state?.let { it ->
            if (it == TelephonyManager.EXTRA_STATE_RINGING) {
                val incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)
                incomingNumber.let { number ->
                            val data = Data.Builder()
                                .putString("phoneNumber", number)
                                .build()
                            workManager = WorkManager.getInstance(context)
                            val notificationBuilder = OneTimeWorkRequest.Builder(NotifyWorker::class.java)
                                .setInputData(data)
                                .build()
                            workManager.enqueue(notificationBuilder)
                }
            }
       }
}

And the NotifyWorker class:NotifyWorker类:

class NotifyWorker(context: Context, params: WorkerParameters) : Worker(context, params) {

    companion object {
        const val CHANNEL_ID = "NotificationChannel"
        const val CHANNEL_NAME = "Notification"
    }

    private val mContext = context

    override fun doWork(): Result {
        triggerNotification()
        return Result.success()
    }

    private fun triggerNotification() {
        val notificationManager =
            mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                CHANNEL_ID,
                CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(channel)
        }

        val notificationIntent = Intent(mContext, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(mContext, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
        val notification = NotificationCompat.Builder(mContext, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_phone_call)
            .setContentTitle("incoming call")
            .setContentText(inputData.getString("phoneNumber"))
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setCategory(NotificationCompat.CATEGORY_CALL)
            .setFullScreenIntent(pendingIntent, true)
            .setAutoCancel(true)

        notification.setContentIntent(pendingIntent)
        notificationManager.notify(1, notification.build())
    }
}

Add the receiver in your AndroidManifest file.在您的AndroidManifest文件中添加receiver

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

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

相关问题 手动终止应用程序时如何获取广播接收器? - How to get broadcast receiver when an app is killed manually? 当应用程序被杀时,Android广播接收器无法正常工作 - Android Broadcast Receiver not working when the app is killed 当应用程序被杀死时使广播接收器活着 - Make Broadcast Receiver Alive when app is killed 当应用程序被杀死时,互联网广播接收器不工作 - Internet broadcast receiver is not working when app is killed 广播接收器即使在app被用户杀死后仍然工作 - Broadcast receiver working even after app is killed by user 应用被杀死时,广播接收器未在Oreo上收听 - Broadcast receiver not listening on Oreo when app gets killed 当应用程序在奥利奥中被杀死时,广播接收器不工作为什么? - Broadcast Receiver not working when the App killed in Oreo Why? 当应用程序在 android 版本 9 中被杀死时收听呼叫广播接收器 - listen to call broadcast receiver when app gets killed in android version 9 我希望我的服务和广播接收器能够运行,即使从应用列表中清除了该应用或强制停止了该应用 - I want my service and broadcast receiver to run even if app is cleared from app list or forced stop 应用程序被杀死时广播接收器不工作 - Broadcast receiver is not working when application is killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM