我有一个Cordova插件,它在设备上运行激光扫描仪,其Main.java如下所示:
package com.example.plugin;
import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import java.security.PublicKey;
import android.os.Bundle;
import android.app.Activity;
import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
public class Hello extends CordovaPlugin {
public static final int REQUEST_CODE = 0x0ba7c0de;
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
if (action.equals("scan")) {
scan();
return true;
} else {
return false;
}
}
public void scan() {
Intent intentService = new Intent("com.hyipc.core.service.barcode.BarcodeService2D");
intentService.putExtra("KEY_ACTION", "UP");
this.cordova.startActivityForResult((CordovaPlugin) this, intentService, REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.i("scan", "everything works fine");
}
}
07-11 12:03:33.541: E/AndroidRuntime(5258): FATAL EXCEPTION: main
07-11 12:03:33.541: E/AndroidRuntime(5258): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ionicframework.curierapp266167/com.hyipc.core.service.barcode.BarcodeService2D}: java.lang.classNotFoundException: Didn't find class "com.hyipc.core.service.barcode.BarcodeService2D" on path: DexPathList[[zip file "/data/app/com.ionicframework.curierapp266167-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.ionicframework.curierapp266167-2, /vendor/lib, /system/lib]]
07-11 12:03:33.541: E/AndroidRuntime(5258): at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2269)
07-11 12:03:33.541: E/AndroidRuntime(5258): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
07-11 12:03:33.541: E/AndroidRuntime(5258): at android.app.ActivityThread.access$600(ActivityThread.java:162)
07-11 12:03:33.541: E/AndroidRuntime(5258): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
07-11 12:03:33.541: E/AndroidRuntime(5258): at android.os.Handler.dispatchMessage(Handler.java:107)
07-11 12:03:33.541: E/AndroidRuntime(5258): at android.os.Looper.loop(Looper.java:194)
07-11 12:03:33.541: E/AndroidRuntime(5258): at android.app.ActivityThread.main(ActivityThread.java:5392)
07-11 12:03:33.541: E/AndroidRuntime(5258): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 12:03:33.541: E/AndroidRuntime(5258): at java.lang.reflect.Method.invoke(Method.java:525)
07-11 12:03:33.541: E/AndroidRuntime(5258): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-11 12:03:33.541: E/AndroidRuntime(5258): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-11 12:03:33.541: E/AndroidRuntime(5258): at dalvik.system.NativeStart.main(Native Method)
07-11 12:03:33.541: E/AndroidRuntime(5258): Caused by: java.lang.classNotFoundException: Didn't find class "com.hyipc.core.service.barcode.BarcodeService2D" on path: DexPathList[[zip file "/data/app/com.ionicframework.curierapp266167-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.ionicframework.curierapp266167-2, /vendor/lib, /system/lib]]
我已将此添加到我的AndroidManifest.xml文件中,但我很确定它有什么问题.
<activity android:label="@string/share_name" android:name="com.hyipc.core.service.barcode.BarcodeService2D">
<intent-filter>
<action android:name="com.hyipc.core.service.barcode.BarcodeService2D" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
编辑
如果我编辑我的清单文件,我找到No Activity来处理Intent错误.
<activity android:label="@string/share_name" android:name="com.hyipc.core.service.barcode.BarcodeService2D">
<intent-filter>
<action android:name="android.intent.action.LAUNCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
另外,如果我尝试像这样开始活动:
cordova.getActivity().startService(intentService);
扫描仪启动没有错误,但我不知道如何得到它的结果.
解决方法:
从评论列表中的讨论中我们知道BarcodeService2D是一个服务,您可以使用startService成功启动组件.现在问题变成“如何将数据从我的服务传递到Activity实例”? StackOverflow中有很多答案,请参阅this和this.只需谷歌即可.
对于“所以服务与活动不同?”这个问题,答案是肯定的.活动通常代表应用中的屏幕.它包含各种View,Drawable和许多其他内容,您可以直接与它进行交互.服务通常用于在后台执行长时间运行的任务,而无需任何UI.有关详细信息,请参阅the developer site