[英]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.