React Native Script TelephonyManager not working

599 Views Asked by At

Here is my NativScript code for android

package com.chetti; // replace com.your-app-name with your app’s name
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import java.util.Map;
import java.util.HashMap;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;    
import android.app.Activity;


public class CalendarModule extends ReactContextBaseJavaModule {
    CalendarModule(ReactApplicationContext context) {
        super(context);
    }

    @Override
    public String getName() {
        return "CalendarModule";
    }

    @ReactMethod
    public String createCalendarEvent() {
        //  I am not sure about how to get TelephonyManager instance!
        TelephonyManager  manager=(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        String phoneNumber1 = manager.getLine1Number();
        return phoneNumber1;

    }
}

Error throwing are follows->

error: cannot find symbol

TelephonyManager  manager=(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
                                                    ^
  symbol:   method getActivity()
  location: class CalendarModule

My requirement is to get the mobile numbers using Native Module for android?.

My specs: 1, React Native 0.68.0 2, node 17

1

There are 1 best solutions below

1
On

Just putting here a function so that you can get an idea:

 @Override
    public void onReceive(final Context context, final Intent intent) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    
 TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String phoneNumber) {
                    Log.d("RINGER_IID", phoneNumber);
                    onCustomCallStateChanged(context, state, phoneNumber);
            }
        }, PhoneStateListener.LISTEN_CALL_STATE);
}

https://developer.android.com/reference/android/telephony/TelephonyManager

Got through this. Hope this helps.