gpt4 book ai didi

android - IntentService 中未收到广播接收器

转载 作者:行者123 更新时间:2023-11-29 19:05:43 24 4
gpt4 key购买 nike

我正在构建一个能够从 FirebaseMessagingService 捕获 Intent 的服务。

我使用的第一个版本是在主要 Activity 的应用程序级别执行此操作,但我不想再在这个级别处理它。我创建了一个意向服务,如下所示:

package com.seb.sebastien.appservices;

import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;
import android.telephony.TelephonyManager;
import android.util.Log;

public class appsControlIntentService extends IntentService {
public static final String REQUEST_MSG = "request";
public static final String RESPONSE_MSG = "response";
public static final String TAG = "appsControlIntentService";
private String type_sim;


public appsControlIntentService() {
super("appsControlIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {

boolean request = intent.getBooleanExtra(REQUEST_MSG, false);

if(request == true) {
sendResponse("Enable");
handleAction(getCarrier());
registerAllReceivers();
}
else if (request == false) {
sendResponse("Disable");
unRegisterAllReceivers();
disableAction();
}
else
Log.e(TAG, "Error msg");

}
...

/* Firebase and SIM */
private BroadcastReceiver serviceBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Broadcast received");
if (intent.getExtras().getString(appFirebaseMessagingService.FIREBASE).equals(appFirebaseMessagingService.FIREBASE))
type_sim = intent.getExtras().getString(appFirebaseMessagingService.SIM_TYPE);
else if(intent.getExtras().getString(SimNotificationService.EXTRA_SIM_STATE) != null)
Log.d(TAG, "Get something from Sim");
else
type_sim = null;

if(type_sim != null){
handleAction(type_sim);
}
}
};

private void registerAllReceivers(){
Log.d(TAG, "Broadcast receiver register");
registerReceiver(serviceBroadcastReceiver, new IntentFilter(appFirebaseMessagingService.INTENT_FILTER));
}

private void unRegisterAllReceivers() {
if (serviceBroadcastReceiver != null) {
unregisterReceiver(serviceBroadcastReceiver);
serviceBroadcastReceiver = null;
}
}

@Override
public void onDestroy() {
unRegisterAllReceivers();
super.onDestroy();
}

}

此 Intent 服务注册了一个广播接收器,以便能够接收 firebase 广播,如下所示:

package com.seb.sebastien.appservices;

public class appFirebaseMessagingService extends FirebaseMessagingService {

public static final String TAG = "appFirebaseMessagingService";
public static final String INTENT_FILTER = "INTENT_FILTER";
public static final String FIREBASE = "firebase";
public static final String SIM_TYPE = "simtype";

public appFirebaseMessagingService() {
}

/* @Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
} */

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

String sim = null;

Log.d(TAG, "From: " + remoteMessage.getFrom());

if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
sim = remoteMessage.getNotification().getBody();
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

Intent intent = new Intent(INTENT_FILTER);
intent.putExtra(FIREBASE, FIREBASE);
intent.putExtra(SIM_TYPE, sim );

sendBroadcast(intent);

}
}

在调试时,我看到我们一直走到 sendBroadcast,但如果我已经注册了接收器,“appControlIntentService”类事件中似乎什么也没有收到。

知道为什么在 intentservice 中没有收到广播吗?

最佳答案

首先,不确定您实际在哪里创建了 appsControlIntentService 的实例。

其次,IntentService 在 onHandleIntent 完成时停止。它旨在完成一项短期任务并完成。所以基本上,即使您创建了一个收听广播的对象,它也只会在非常有限的时间内这样做,直到它的 onDestroy 被调用。

关于用于收听自定义 Intent 的 BroadcastReceiver 解决方案:

1) 创建一个在后台运行的服务(不是 IntentService,常规服务),并让它注册一个 BroadcastListener。您可以在 Activity 首次唤醒时创建此服务,并且可能在 Boot BroadcastReceiver 中创建。决定您是希望它成为前台服务还是后台服务。由于内存问题,后台服务更有可能被操作系统杀死。但前台服务要求您在状态栏中显示一个图标。

2) 只需将 BroadcastReceiver 放入 list 中即可。这样,操作系统甚至会在广播到达时唤醒应用程序来处理广播。即使该应用程序当前未运行。查看“Manifest-declared receivers”:https://developer.android.com/guide/components/broadcasts.html .

要禁用此接收器,请查看:Android - how to unregister a receiver created in the manifest? .或者,您可以在共享首选项中保存是否希望它对事件执行某些操作,或者更好的是,如果它首先被禁用,请不要从 FirebaseMessagingService 发送 Intent 。

关于android - IntentService 中未收到广播接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47323870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com