我遇到了SIP API的問題。我按照d.android.com的指南,從sip2sip.info獲得一個免費的SIP帳戶來測試我的應用程序。 更新:我試過ekiga.net,而且我自己也設置了一個Kamailio服務器,但是這些也沒有幫助。SIP註冊失敗
每當我嘗試註冊我的配置文件時,都會失敗並顯示錯誤消息「0」。以下是代碼:
package com.mysys.mysip;
import java.text.ParseException;
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.app.PendingIntent;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
public SipManager mSipManager = null;
public SipProfile mSipProfile = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (mSipManager == null) {
mSipManager = SipManager.newInstance(this);
}
Button connectButton = (Button) findViewById(R.id.buttonConnect);
connectButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
buildProfile();
if (mSipProfile != null) {
openProfile();
}
}
});
}
public void buildProfile() {
try {
SipProfile.Builder builder = new SipProfile.Builder("censored", "sip2sip.info");
builder.setPassword("censored");
mSipProfile = builder.build();
} catch (ParseException e) {
Log.e("SipProfile.Builder", "Parse error!");
e.printStackTrace();
}
}
public void openProfile() {
Intent intent = new Intent();
intent.setAction("com.mysys.sip.INCOMING_CALL");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA);
SipRegistrationListener listener = new SipRegistrationListener() {
@Override
public void onRegistering(String localProfileUri) {
Log.i("SIP Registration", "Registering.");
updateStatus("Registering...");
}
@Override
public void onRegistrationDone(String localProfileUri, long expiryTime) {
Log.i("SIP Registration", "Done!");
updateStatus("Registered! Yay!");
}
@Override
public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage) {
Log.e("SIP Registration", "Failed! URI: " + localProfileUri + " Reason: " + errorMessage);
updateStatus("Registration failed.");
}
};
try {
mSipManager.open(mSipProfile, pendingIntent, null);
mSipManager.setRegistrationListener(mSipProfile.getUriString(), listener);
} catch (SipException e) {
e.printStackTrace();
}
}
public void updateStatus(final String status) {
this.runOnUiThread(new Runnable() {
public void run() {
TextView statusView = (TextView) findViewById(R.id.textViewStatus);
statusView.setText(status);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}
與Android SDK自己的演示相比,我查了兩次代碼,但沒有發現任何東西。我在查詢之前也進行了搜索,唯一相關的是我發現將協議設置爲TCP,但它並未解決我的問題。
怎麼樣的manifest.xml?你使用正確的權限? INTERNET和USE_SIP – kichik 2012-07-25 22:54:23
@kichik,是的,我是。 – Utku 2012-07-25 23:25:58
你使用模擬器還是真實的設備,如果是的話,哪個設備? – Merlin 2012-07-26 10:51:48