我正在开发使用react-native-pjsip
进行本地响应的voip移动应用。它在ios
上运行良好,但是在android
上从后台恢复一段时间后崩溃。
我在BackgroundService
中创建了PusherReceiver
和android
,以便在收到react native activity
的推送以在后台显示呼叫时启动android
。
BackgroundService.java
public class BackgroundService extends IntentService {
public static final String EXTRA_ISFROMPUSH = "com.fonality.hudmobile/com.voximplantdemo.isFromPush";
public BackgroundService() {
super("BackgroundService");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Intent i = new Intent(getBaseContext(), MainActivity.class);
// is from push param
i.putExtra(EXTRA_ISFROMPUSH, true);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (intent != null) {
startActivity(i);
PusherReceiver.completeWakefulIntent(intent);
}
}
}
PusherReceiver.java
public class PusherReceiver extends WakefulBroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
if (!isAppOnForeground((context))) {
String custom = intent.getStringExtra("custom");
try {
if (custom != null) {
JSONObject notificationData = new JSONObject(custom);
}
// This is the Intent to deliver to our service.
Intent service = new Intent(context, BackgroundService.class);
// Put here your data from the json as extra in in the intent
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, service);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
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;
}
}
谢谢