簡體   English   中英

應用程序被殺時無法接收SIP呼叫

[英]Unable to receive SIP calls when app is killed

即使應用程序處於后台,退出或重新啟動,我該怎么做才能接收SIP呼叫? 目前我們正在運行帶有粘性通知的前台服務,以便與我們的SIP服務器保持連接。 這個解決方案還沒有完全正常工作請幫我實現這個,提前謝謝!

首先,您需要一個永不停止的Mainservice(即使設備在啟動接收器的幫助下啟動)。 您必須初始化您的sip管理器,sip配置文件並在MainService中調用相關代碼,而不是在MainActivity中。

然后,您需要一個接收傳入SIP呼叫的BroadCast接收器。 見下面的例子,

公共類YourReceiver擴展BroadcastReceiver {

SipAudioCall incomingCall   = null;
private static YourReceiver instance;
MainService mainService;
public Intent incomingCallIntent;

public static YourReceiver getInstance() {
    return instance;
}

/**
 * Processes the incoming call, answers it, and hands it over to the
 * MainActivity.
 * @param context The context under which the receiver is running.
 * @param intent The intent being received.
 */
@Override
public void onReceive(final Context context, Intent intent) {
    Log.i(TAG, "onReceive: ");
    instance    = this;
    mainService = MainService.getInstance();

    try {
        SipAudioCall.Listener listener = new SipAudioCall.Listener() {
            @Override
            public void onRinging(SipAudioCall call, SipProfile caller) {
                Log.i(TAG, "onRinging: ");


            }

            // extra added
            @Override
            public void onRingingBack(SipAudioCall call) {
                Log.i(TAG, "onRingingBack: ");
                super.onRingingBack(call);
            }

            @Override
            public void onReadyToCall(SipAudioCall call) {
                Log.i(TAG, "onReadyToCall: ");
                super.onReadyToCall(call);
            }

            @Override
            public void onError(SipAudioCall call, int errorCode, String errorMessage) {
                Log.e(TAG, "onError: errorCode = " + errorCode + ", errorMessage = " + errorMessage);
                super.onError(call, errorCode, errorMessage);
            }

            @Override
            public void onChanged(SipAudioCall call) {
                Log.i(TAG, "onChanged: ");
                super.onChanged(call);
            }

            @Override
            public void onCalling(SipAudioCall call) {
                Log.i(TAG, "onCalling: ");
                super.onCalling(call);
            }

            @Override
            public void onCallHeld(SipAudioCall call) {
                Log.i(TAG, "onCallHeld: ");
                super.onCallHeld(call);
            }

            @Override
            public void onCallBusy(SipAudioCall call) {
                Log.i(TAG, "onCallBusy: ");
                super.onCallBusy(call);
            }

            @Override
            public void onCallEnded(SipAudioCall call) {
                Log.i(TAG, "onCallEnded: ");


            }

            @Override
            public void onCallEstablished(SipAudioCall call) {
                Log.i(TAG, "onCallEstablished: ");
                super.onCallEstablished(call);
            }
        };


        mainService  = MainService.getInstance();
        incomingCall = mainService.manager.takeAudioCall(intent,listener);
        if(mainService.manager.isIncomingCallIntent(intent)){
            incomingCallIntent = intent;
        }

        //starting call screen activity when the receiver receives incoming call
        Intent i    = new Intent(context, CallActivity.class);
        i.putExtra("name", peerName);
        i.putExtra("number", peerNumber);
        i.putExtra("callType", "Incoming");
        context.startActivity(i);

//將YourReceiver收到的SIP調用分配給MainService.call,以便MainService可以執行進一步的處理。

        mainService.call = incomingCall;

    } catch (Exception e) {
        if (incomingCall != null) {
            incomingCall.close();
        }
    }
}

}

運行服務是正確的方法。 您只需啟動將處理來自服務的來電的活動。 然后,活動可以綁定到您的服務並請求接管呼叫所需的全部內容。 這實際上就是你所需要的一切。

要使服務連續運行,請啟動Job scheduler以使此作業成為android系統作業的一部分。

package com.xxx;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class XXJobService extends JobService {
    private String TAG = "XXJobService";
    private Context context;

    public XXJobService() {
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        context = this;

        try {
            if (!MyApp.isServiceRunning(context, MainService.class.getName())) {
                Log.i(TAG, "onStartJob : MainService not running so start");

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.getApplicationContext()
                            .startForegroundService(new Intent(context, MainService.class)
                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                } else {
                    context.getApplicationContext()
                            .startService(new Intent(context, MainService.class)
                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception - MainService not running: " + e.getLocalizedMessage());
        }


        Log.i(TAG, "onStartJob, returnValue = " + returnValue);
        return returnValue;
    }

    @Override
    public boolean onStopJob(JobParameters params) {

        boolean returnValue = true;

        Log.i(TAG, "onStopJob, returnValue = " + returnValue);
        return returnValue;
    }
}

在項目中創建一個擴展Application的Application類,並在清單中定義它。

import android.app.ActivityManager;
import android.app.Application;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import java.util.Arrays;
import java.util.List;

public class MyApp extends Application {

    Context context;
    private static MyApp instance;
    static String TAG = "MyApp";
    MainService mainService;
    JobScheduler jobScheduler;
    private static final int JOB_ID = 1;

    public static MyApp getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context  = this;
        instance = this;
        Log.i(TAG, "onCreate: ");

        // Job scheduler for android version Lolipop(Android 5.0) and above
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                jobScheduler             = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
                ComponentName jobService = new ComponentName(getPackageName(), XXJobService.class.getName());
                Log.e(TAG, "onCreate: ComponentName : " + jobService );
                JobInfo jobInfo          = new JobInfo.Builder(JOB_ID, jobService)
                        .setPersisted(true)
                        .setPeriodic(5000)
                        .build();
                int jobId                = jobScheduler.schedule(jobInfo);

                if (jobId == JobScheduler.RESULT_SUCCESS) {
                    Log.e(TAG, "JobScheduler RESULT_SUCCESS");
                    // Toast.makeText(context, "Successfully scheduled job : " + jobId, Toast.LENGTH_SHORT).show();
                } else {
                    Log.e(TAG, "JobScheduler RESULT_FAILURE: " + jobId);
                    // Toast.makeText(context, "RESULT_FAILURE: " + jobId, Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                Log.e(TAG, "JobScheduler Exception : " + e.getLocalizedMessage());
            }
        }

        if (!isServiceRunning(context, MainService.class.getName())) {
            try {
                Intent intent = new Intent(context, MainService.class);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    startForegroundService(intent);
                } else {
                    startService(intent);
                }
            } catch (Exception e) {
                Log.e(TAG, "Exception startService : " + e.getLocalizedMessage());
            }
        }

        mainService = MainService.getInstance();
        Log.e(TAG," MainSerivice : " + mainService);

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                handleUncaughtException(t, e);
            }
        });
    }


    public static boolean isServiceRunning(Context context, String serviceClassName) {
        if (context == null || serviceClassName.equalsIgnoreCase("")) {
            Log.i(TAG, "isServiceRunning called with context = null or service name blank");
            return false;
        }

        ActivityManager activityManager                     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> services   = activityManager.getRunningServices(Integer.MAX_VALUE);

        for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
            if (runningServiceInfo.service.getClassName().equals(serviceClassName))
                return true;
        }

        return false;
    }

}

最后,將BootUpReceiver置於手機重啟時自動啟動通知和服務。 它應該有權限

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

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


<receiver
            android:name=".StartupReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.xx.MainService" />
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />

                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM