beck
beck

Reputation: 3027

Native interface for auto run on Boot in cn1

I am not quite sure how to implement the native android code in cn1 through native interface, although I've done few successfully. Below is the native android code and a try I've done in cn1 native interface. I have a couple of questions:

1) How can I call a class in nativeImpl class? eg: I've created YourActivityRunOnStartup class inside native/android folder in the project. How to call it in nativeImpl class? The receiver in manifest has this class not found exception.

2) There is intent in onReceive method of BroadcastReceiver where I've MainActivity.class, so that each time the device boots, it runs MainActivity class in native android. How can I call specific form or class of cn1 in it through native interface?

Native android code:

**MainActivity**
public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.e("bootDeviceValue", " aaaaa");
    }
}

**YourActivityRunOnStartup class**
public class YourActivityRunOnStartup extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Log.e("bootDeviceValue", " bbbbb");
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

What I've tried in cn1:

Try 1: YourActivityRunOnStartup class inside Native/android folder

public class YourActivityRunOnStartup extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Log.e("bootDeviceValue", " bbbbb");
            Intent i = new Intent(context, null);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

Build hint:

1)android.xapplication: <receiver   android:enabled="true"   android:exported="true"   android:name=".YourActivityRunOnStartup"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter>  </receiver>

2)android.xpermissions:  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

How to call YourActivityRunOnStartup class in NativeImpl class and what to do in cn1 code?

NativeImpl class

public class NativeImpl {
    public boolean isSupported() {
        return true;
    }

    //what to do here? . . . . . . . . . . . . . . . . . . 
}

Nokia.class

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    na = (Native) NativeLookup.create(Native.class);
    if (na != null && na.isSupported()) {
        **// what to do here??? . . . . . . . . . . . . . . . . . .** 
    }
    new Home(theme).show();
}

Error on debug:

09-04 14:28:00.805 26852-26852/com.zzzz.nokiaApp E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.zzzz.nokiaApp, PID: 26852
 java.lang.RuntimeException: Unable to instantiate receiver com.zzzz.nokiaApp.YourActivityRunOnStartup: java.lang.ClassNotFoundException: Didn't find class "com.zzzz.nokiaApp.YourActivityRunOnStartup" on path: DexPathList[[zip file "/data/app/com.zzzz.nokiaApp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.zzzz.nokiaApp-1/lib/arm, /vendor/lib, /system/lib]]
     at android.app.ActivityThread.handleReceiver(ActivityThread.java:3682)
     at android.app.ActivityThread.access$2000(ActivityThread.java:230)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1904)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:7409)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
  Caused by: java.lang.ClassNotFoundException: Didn't find class "com.zzzz.nokiaApp.YourActivityRunOnStartup" on path: DexPathList[[zip file "/data/app/com.zzzz.nokiaApp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.zzzz.nokiaApp-1/lib/arm, /vendor/lib, /system/lib]]
     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
     at android.app.ActivityThread.handleReceiver(ActivityThread.java:3677)
     at android.app.ActivityThread.access$2000(ActivityThread.java:230) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1904) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:7409) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
    Suppressed: java.lang.ClassNotFoundException: com.zzzz.nokiaApp.YourActivityRunOnStartup
     at java.lang.Class.classForName(Native Method)
     at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
     at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 10 more
  Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

enter image description here

enter image description here

enter image description here

New Error Log:

09-07 11:54:05.462 8116-8116/com.thecapitaleyenepal.nokiaApp D/AndroidRuntime: Shutting down VM
09-07 11:54:05.462 8116-8116/com.thecapitaleyenepal.nokiaApp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.thecapitaleyenepal.nokiaApp, PID: 8116
java.lang.RuntimeException: Unable to start receiver com.thecapitaleyenepal.nokiaApp.YourActivityRunOnStartup: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.thecapitaleyenepal.nokiaApp/com.thecapitaleyenepal.nokiaApp.Nokia}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3708)
at android.app.ActivityThread.access$2000(ActivityThread.java:230)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1904)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.thecapitaleyenepal.nokiaApp/com.thecapitaleyenepal.nokiaApp.Nokia}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1855)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1546)
at android.app.ContextImpl.startActivity(ContextImpl.java:752)
at android.app.ContextImpl.startActivity(ContextImpl.java:734)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:345)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:345)
at com.thecapitaleyenepal.nokiaApp.YourActivityRunOnStartup.onReceive(YourActivityRunOnStartup.java:25)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3701)
at android.app.ActivityThread.access$2000(ActivityThread.java:230) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1904) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7409) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
09-07 11:54:45.302 8116-8116/com.thecapitaleyenepal.nokiaApp I/Process: Sending signal. PID: 8116 SIG: 9

AndroidManifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:enabled="true"
        android:exported="true"
        android:name=".YourActivityRunOnStartup">

        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>

    </receiver>
</application>

Upvotes: 2

Views: 85

Answers (1)

Shai Almog
Shai Almog

Reputation: 52770

Notice that within the build hints you included strings that you need to update such as YourActivityRunOnStartup etc. make sure they point to the right package/class names.

Upvotes: 0

Related Questions