Java – Grants activity permissions in AndroidManifest

Grants activity permissions in AndroidManifest… here is a solution to the problem.

Grants activity permissions in AndroidManifest

I have a Cordova plugin running a laser scanner on my device whose Main.java looks like this:

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");
    }
}

I get this in the error log when I run the plugin :

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]]

I’ve added it to my AndroidManifest.xml file, but I’m pretty sure there’s something wrong with it.

<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>

Edit

If I edit my list file to this, I get a No Activity found to handle Intent error.

<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>

Also, if I try to start the activity like this:

cordova.getActivity().startService(intentService);

The scanner starts without errors, but I don’t know how to get its results.

Solution

We know from the discussion in the comment list that BarcodeService2D, is a service that can be successfully started using startService. Now the question becomes “how do I pass data from my service to an activity instance”? There are many answers in StackOverflow, see this and this.

For the question “So the service is different from the activity?” To this question, the answer is yes. An activity typically represents a screen in an app. It contains various views, drawables, and many other things that you can interact with directly. Services are typically used to perform long-running tasks in the background without any UI. For more information, see the developer site

Related Problems and Solutions