0
votes

I am working on an application where I want to register a new broadcast receiver. However, when the intent is triggered the application is not able to find the receiver class and fails with the below error:

System.err: Unable to instantiate receiver org.nativescript.ntngcallerid.PhoneCallReceiver: java.lang.ClassNotFoundException: Didn't find class "org.nativescript.ntngcallerid.PhoneCallReceiver" on path: DexPathList[[zip file "/data/app/org.nativescript.ntngcallerid-1v-8qMmEKBejsoEoL26_7Q==/base.apk"],nativeLibraryDirectories=[/data/app/org.nativescript.ntngcallerid-1v-8qMmEKBejsoEoL26_7Q==/lib/x86, /data/app/org.nativescript.ntngcallerid-1v-8qMmEKBejsoEoL26_7Q==/base.apk!/lib/x86, /system/lib, /system/product/lib]]

Phone call receiver class:

import * as Toast from "nativescript-toast";

declare var android: any;

@JavaProxy("com.tns.PhoneCallReceiver")
export class PhoneCallReceiver extends android.content.BroadcastReceiver {

    constructor() {
        super();

        return global.__native(this);
    }

    // @ts-ignore
    onReceive(context: android.content.Context, intent: android.content.Intent): void {
        // @ts-ignore

        console.log("Receiver start");
        Toast.makeText(" Receiver start ", "2000").show();
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="__PACKAGE__"
          android:versionCode="10000"
          android:versionName="1.0">

    <supports-screens android:smallScreens="true"
                      android:normalScreens="true"
                      android:largeScreens="true"
                      android:xlargeScreens="true"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_CALL_LOG"/>
    <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:name="com.tns.NativeScriptApplication"
                 android:allowBackup="true"
                 android:icon="@drawable/icon"
                 android:label="@string/app_name"
                 android:theme="@style/AppTheme">

        <activity android:name="com.tns.NativeScriptActivity"
                  android:label="@string/title_activity_kimera"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|locale|uiMode"
                  android:theme="@style/LaunchScreenTheme">

            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme"/>

            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity"/>


        <receiver android:name="com.tns.PhoneCallReceiver"
                  android:enabled="true"
                  android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>


    </application>
</manifest>
1
Have you included the receiver class in Webpack? - Manoj
Hi @Manoj , could you please provide an example of how it should be added in the Webpack? - Andrei Maimas

1 Answers

3
votes

You will have to add an entry of the Broadcast Receiver in webpack.config.js

// Add your custom Activities, Services and other Android app components here.
const appComponents = [
      "tns-core-modules/ui/frame", 
      "tns-core-modules/ui/frame/activity",
      "path/to/phone-call-receiver.ts"
];