Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29457

React Native usage Headless js application crash

$
0
0

I do everything according to the documentation, but my application crashes. Function triggering when there is a phone call I want to make. I did catch the call with Broadreciver. I did show on the screen with a toast message. But I can not send it to the service I created for headless js and run it. I can't debug or know the method

index.js

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';

const Fnc = async (taskData) => {
  console.log('data',taskData);
}

AppRegistry.registerComponent(appName, () => App);
AppRegistry.registerHeadlessTask('CallListener', () => Fnc);

AndroidManifest.xml

<application>
...
 <receiver android:name=".CallReceiver">
    <intent-filter>
      <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
 </receiver>

 <service android:name=".CallService" android:enabled="true"  />
</application>

CallReceiver.java

public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
   try {
       if (!isAppOnForeground((context))) {
           if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
               String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
               Intent serviceIntent = new Intent(context, CallService.class);

               //Bundle bundle = new Bundle();
               //bundle.putString("even", "okkk");
               showText(context, "Callsss Ring...No: " + number);
                 serviceIntent.putExtra("event", "Incoming");
                // serviceIntent.putExtra("event", "Incoming");
              // serviceIntent.putExtras(bundle);
               context.startService(serviceIntent);
               HeadlessJsTaskService.acquireWakeLockNow(context);
           }
       }
   }catch (Exception ex){
       Log.i("ex", ex.getMessage());
   }

}

private boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses =
            activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
        return false;
    }
    final String packageName = context.getPackageName();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.importance ==
                ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
                appProcess.processName.equals(packageName)) {
            return true;
        }
    }
    return false;
}
void showText(Context context, String message){
    Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
 }
}

CallService.java

public class CallService extends HeadlessJsTaskService {

@Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Bundle extras = intent.getExtras();
    WritableMap data = extras != null ? Arguments.fromBundle(extras) : null;

    return new HeadlessJsTaskConfig(
            "CallListener",
            data,
            5000, // timeout for the task
            false // optional: defines whether or not  the task is allowed in foreground. Default is false,
    );
 }
}

I've been researching for days, but I can't find the problem


Viewing all articles
Browse latest Browse all 29457

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>