Android 无法通过react本机网桥运行服务
我用Java创建了一个本机模块,负责在后台运行服务 我在Android 无法通过react本机网桥运行服务,android,react-native,Android,React Native,我用Java创建了一个本机模块,负责在后台运行服务 我在ClassA中有这个方法: @ReactMethod public void startService(final Promise promise) { Intent intent = new Intent(getCurrentActivity(), BackgroundService.class); intent.putExtra(BackgroundService.FILENAME, "te
ClassA
中有这个方法:
@ReactMethod
public void startService(final Promise promise) {
Intent intent = new Intent(getCurrentActivity(), BackgroundService.class);
intent.putExtra(BackgroundService.FILENAME, "test123.html");
intent.putExtra(BackgroundService.URL,
"http://www.vogella.com/index.html");
Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
promise.reject("Activity doesnt exist");
} else {
getCurrentActivity().startService(intent);
}
}
以下是BackgroundService
中的代码:
public class BackgroundService extends IntentService {
private int result = Activity.RESULT_CANCELED;
public static final String URL = "urlpath";
public static final String FILENAME = "filename";
public static final String FILEPATH = "filepath";
public static final String RESULT = "result";
public static final String NOTIFICATION = "com.vogella.android.service.receiver";
public BackgroundService() {
super("BackgroundService");
}
public String getName() {
return "BackgroundService";
}
@Override
protected void onHandleIntent(Intent intent) {
//do stuff
ClassA.servicePromise.resolve("done with service!");
}
servicePromise
在classA中的定义如下:
公共静态承诺服务承诺=null
如果我理解正确,问题是getCurrentActivity.startService(intent)
没有正确执行。在getCurrentActivity.startService(intent)
之后,我能够在startService
内成功返回一个承诺,因此我知道问题不在于如何调用startService
。但是,我无法从内部后台服务
返回我想要的承诺
在onHandleIntent
中,我尝试调用ClassA.serviceconomise.resolve(“使用服务完成!”)在执行任何其他代码之前,只是为了测试它是否工作,但它不工作
在我的清单中,我添加了服务
,如下所示:
<service android:name="com.smaplePakcage.name.BackgroundService"
android:exported="false">
</service>