我一直在尝试将 BroadcastReceiver 与我的 react-native 应用程序集成。每当有人打电话给我时,我都想展示 ToastAndroid。当应用程序运行时(在前台和后台),它工作得很好,但是当我终止应用程序时,它不再触发 ToastAndroid。
我看过有人用本机代码执行此操作的视频(YT 链接),即使应用程序被终止,它也能正常工作。
我怎样才能用 react-native 来完成它?
我的服务代码:
public class CallService extends HeadlessJsTaskService {
@Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
return new HeadlessJsTaskConfig(
"Call",
Arguments.fromBundle(extras),
5000,
true
);
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我的接收器代码:
public final class CallReceiver extends BroadcastReceiver {
@Override
public final void onReceive(Context context, Intent intent) {
Intent callIntent = new Intent(context, CallService.class);
if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
String phoneState = intent.getStringExtra("state"); …Run Code Online (Sandbox Code Playgroud)