[英]Run and get event from Broadcast Receiver even when app is killed
我想制作像 TrueCaller 这样的应用程序。 为此,我需要获取来电事件。 为此,我制作了广播接收器,它可以正常工作,直到应用程序将被杀死。
所以即使应用程序被杀死,我也想让我的广播接收器运行。
以前我使用Service来实现这一点,但在 After Android O Service 不起作用。 我以为我可以使用 WorkManager 将其存档,但我不明白该怎么做。 所以我想要服务替代或正确的方式来存档。
请帮我做。
提前致谢。
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);
}
}
尝试这样的事情。 不幸的是,我只能在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)
}
}
}
}
和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())
}
}
在您的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.