2

海每个人实际上都在尝试创建一个应用程序,该应用程序支持基于 SIP 在互联网上进行音频呼叫,这里使用的是本地 sip,我遇到了来电问题,我已经完成了服务的注册部分,但是在接听电话时,我无法接听电话,请帮帮我

Service file

package example.com.myapplication;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipRegistrationListener;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.text.ParseException;

import static android.content.Intent.FILL_IN_ACTION;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import example.com.myapplication.splash.SplashScreen;

public class MyService extends Service {
public static final int notifyID = 9001;
public IncomingReceiver callReceiver=new IncomingReceiver();
public SipProfile mSipProfile = null;
public SipProfile.Builder builder = null;
public SipAudioCall call = null;
public SipManager mSipManager = null;
public static SipManager sipManager2;
public String username,password,domain;
public String Status;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void load()
{
    Context ctx = getApplicationContext();
    SharedPreferences   sharedPreferences=getSharedPreferences("Login",Context.MODE_PRIVATE);
   username= sharedPreferences.getString("username1","Error" );
    password= sharedPreferences.getString("password1","Error" );
    domain= sharedPreferences.getString("domain1","Error" );
}
@Override
public void onCreate()
{   super.onCreate();
    IntentFilter filter = new IntentFilter();
    filter.addAction("example.com.myapplication.INCOMING_CALL");
    callReceiver = new IncomingReceiver();
    this.registerReceiver(callReceiver, filter);
  if(mSipManager == null) {
       mSipManager = SipManager.newInstance(getApplicationContext());
   }

}
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
    load();
    Log.d("Username",username);
    Log.d("Password", password);
    Log.d("Domain",domain);
    Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_SHORT).show();
    try {
        builder = new SipProfile.Builder(username, domain);
        builder.setPassword(password);
        builder.setPort(5060);
        builder.setOutboundProxy("10.0.2.51");
        builder.setProtocol("UDP");
        builder.setAutoRegistration(true);
        builder.setSendKeepAlive(true);
        mSipProfile = builder.build();
        Intent intent1 = new Intent();
        intent1.setAction("example.com.myapplication.INCOMING_CALL");
        PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent1, FILL_IN_ACTION);
        mSipManager.open(mSipProfile, pi, null);
        Toast.makeText(getApplication(),"listener Started",Toast.LENGTH_SHORT).show();
        mSipManager.setRegistrationListener(mSipProfile.getUriString(), new SipRegistrationListener() {

            @Override
            public void onRegistering(String localProfileUri) {
                Log.d("Trying", "Register");
            }

            @Override
            public void onRegistrationDone(String localProfileUri, long expiryTime) {
                Status = "done";
                sendNotification();
                sipManager2=mSipManager;
                Log.d("Manager",mSipManager.toString());
                Log.d("Success", "complete");
            }

            @Override
            public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage) {
                Log.d("Failed", Integer.toString(errorCode));
                sendNotification1();
            }
        });
    } catch (SipException e) {
        e.printStackTrace();
        Toast.makeText(getApplication(),"SipListener failed",Toast.LENGTH_SHORT).show();
        Log.d("reg","error");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return super.onStartCommand(intent,flags,startId);
}
public void onDestory()
{
    super.onDestroy();
    sipManager2=null;
    if (call != null) {
      call.close();
    }

   closeLocalProfile();

    if (callReceiver != null) {
        this.unregisterReceiver(callReceiver);
    }
}
public void closeLocalProfile() {
    if (mSipManager == null) {
        return;
    }
    try {
        if (mSipProfile != null) {
            mSipManager.close(mSipProfile.getUriString());
        }
    } catch (Exception ee) {
        Log.d("Sip Profile Error", ee.toString());
    }
}
private void sendNotification() {
    NotificationCompat.Builder mNotifyBuilder;
    NotificationManager mNotificationManager;
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Account is Ready")
            .setContentText("Registration Success")

            .setSmallIcon(R.drawable.success);
    mNotifyBuilder.setContentText("Ready");
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());
}
private void sendNotification1() {

    NotificationCompat.Builder mNotifyBuilder;
    NotificationManager mNotificationManager;
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Account is Not Ready")
            .setContentText("Registration Failed")

            .setSmallIcon(R.drawable.failed);

    mNotifyBuilder.setContentText("Failed");
    mNotifyBuilder.setAutoCancel(true);
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());
}
}

在这里,我附上了我的广播接收器文件广播接收器文件

package example.com.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipProfile;
import android.util.Log;

public class IncomingReceiver extends BroadcastReceiver {
public  SipAudioCall incomingCall=null;
@Override
public void onReceive(Context context, Intent intent) {

    try {
        SipAudioCall.Listener listener = new SipAudioCall.Listener() {
            @Override
            public void onRinging(SipAudioCall call, SipProfile caller) {
                try {
                    call.answerCall(30);
                    Log.d("Incoming", call.toString());
                    Log.d("Name",call.getPeerProfile().getProfileName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //incomingCall=call;
            }
        };
      //  showIncomingCallGui(intent, context);
        incomeScreen wtActivity = (incomeScreen)context;
        incomingCall = wtActivity.mSipManager.takeAudioCall(intent, listener);
        wtActivity.call = incomingCall;
        Log.d("Manager", wtActivity.mSipManager.toString());
        if(incomingCall.isMuted()) {
            incomingCall.toggleMute();
        }
       showIncomingCallGui(intent,context);

    } catch (Exception e) {
        if (incomingCall != null) {
            incomingCall.close();
        }
    }
}
public void accept()
{
    Log.d("Receiver","Answer");
    try {
            incomingCall.answerCall(30);
            incomingCall.startAudio();
            incomingCall.setSpeakerMode(true);
    } catch (SipException e) {
        Log.d("Call Exception",e.toString());
        e.printStackTrace();
    }
    catch (Exception e)
    {
        Log.d("Exception",e.toString());
    }
}
public void showIncomingCallGui(Intent intent,Context context) {
    Intent incomingCall=new Intent(context,incomeScreen.class);
   incomingCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(incomingCall);
}
}

在这里我附上了我的传入屏幕源代码incomingscreen

package example.com.myapplication;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipRegistrationListener;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.ParseException;

public class incomeScreen extends Activity {
public SipProfile mSipProfile = null;
public SipProfile.Builder builder = null;
public MyService service;
public SipAudioCall call = null;
public SipManager mSipManager;
public EditText username,password,domain;
public IncomingReceiver callReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_income_screen);
    //service=new MyService();
    mSipManager=MyService.sipManager2;
    IntentFilter filter = new IntentFilter();
    filter.addAction("example.com.myapplication.INCOMING_CALL");
    callReceiver = new IncomingReceiver();
    this.registerReceiver(callReceiver, filter);
    if(callReceiver!=null)
    {
        unregisterReceiver(callReceiver);
    }
    Button b1 = (Button) findViewById(R.id.Accept);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            callReceiver.accept();
            Log.d("ManagerSIp",mSipManager.toString());
        }
    });

}
}

这是我的错误日志

02-15 12:01:03.334 3921-3921/example.com.myapplication D/Receiver: Answer
02-15 12:01:03.334 3921-3921/example.com.myapplication D/Exception:    java.lang.NullPointerException

请帮我解决

4

2 回答 2

1

我参加聚会迟到了,但这可能对其他人有所帮助。

虽然我正在处理类似的问题,但据我了解,与 Android 中的前台应用程序(活动)所需的权限相比,后台服务需要单独的权限。

如果您在 Android Manifest 中具有特定权限类型,则不足以让您在服务中使用相同的代码。例如,当我使用代码注册 SIP 服务器时,我的 USES_SIP 权限会出错。相同的代码可以在活动中正常工作。

现在我正在启动一个活动来接收呼叫并从该活动注册 BroadcastReceiver ,因此已授予权限。

您的 NullPointerException 不是很详细,但很可能是因为权限。

高温高压

于 2017-05-18T11:28:11.140 回答
-1

当您写“native sip”时,您是指与 Android 一起提供的预打包 SIP,还是与您的应用程序捆绑在一起并可能由您构建的本地 SIP 二进制文件(例如 PJSIP、Linphone 等)?

如果是后者,您将无法通过 Android API 执行您正在执行的操作。您需要使用 SIP 堆栈的 API。

于 2016-05-27T18:47:42.013 回答