无论应用程序当前是否正在运行,我都需要能够接收传入的 Twilio 调用。
一旦用户启动应用程序并登录到我们的服务器,我将启动如下所示的服务。
该服务是粘性启动的,并且从未调用过stopService
或stopSelf
等,因此该服务在应用程序关闭后仍应运行。
当应用程序运行时,
IncomingCallActivity
正常启动以响应 Twilio 调用。如果应用程序在后台,
IncomingCallActivity
仍然可以正常启动以响应 Twilio 调用。如果应用程序关闭,
IncomingCallActivity
将不再启动以响应 Twilio 调用。
为什么应用已经关闭,IncomingCallActivity
没有启动??
public class CallService extends Service implements Twilio.InitListener, DeviceListener, ConnectionListener {
private Device mDevice;
private Connection mConnection;
@Override
public void onCreate() {
super.onCreate();
registerBroadcastReceiver();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Initialize the Twilio SDK if required
if (!Twilio.isInitialized()) {
Twilio.initialize(getApplicationContext(), this);
} else {
getCapabilityToken("CallService", getUser());
}
...
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
// Unregister broadcast receiver
final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.unregisterReceiver(mBroadcastReceiver);
super.onDestroy();
}
@Override
public void onInitialized() {
getCapabilityToken("CallService", getUser());
}
@Override
public void onError(Exception e) {
}
private void getCapabilityToken(String string, User user) {
// Request the capability token from the server.
...
}
protected void setCapabilityToken() {
// Create device using the capability token
mDevice = Twilio.createDevice(getUser().capabilityToken, this);
// Set pending intent for Twilio device
Intent intent = new Intent(this, IncomingCallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mDevice.setIncomingIntent(pendingIntent);
// Broadcast that CallService is ready, to any registered receivers
Intent broadcastIntent = new Intent(App.ACTION__TWILIO_SERVICE_READY);
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
}
public void connect() {
mConnection = mDevice.connect(null /* parameters */, null /* ConnectionListener */);
if (mConnection == null) {
...
} else {
...
}
}
private void answerCall(Device device, Connection connection) {
if (mConnection != null) {
mConnection.disconnect();
}
mConnection = connection;
mConnection.accept();
}
/**
* BroadcastReceiver
*/
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
switch (action) {
case App.ACTION__CAPABILITY_TOKEN_OBTAINED:
setCapabilityToken();
break;
case App.ACTION__CONNECT:
connect();
break;
}
}
};
@Override
public void onStartListening(Device device) {
}
@Override
public void onStopListening(Device device) {
}
@Override
public void onStopListening(Device device, int i, String s) {
}
@Override
public boolean receivePresenceEvents(Device device) {
return false;
}
@Override
public void onPresenceChanged(Device device, PresenceEvent presenceEvent) {
}
@Override
public void onConnecting(Connection connection) {
}
@Override
public void onConnected(Connection connection) {
}
@Override
public void onDisconnected(Connection connection) {
}
@Override
public void onDisconnected(Connection connection, int i, String s) {
}
}
编辑:
为了阐明我是如何声明我的服务等的,这是我的 AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="au.com.encall.encall"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".IncomingCallActivity"
android:screenOrientation="portrait"/>
<service android:name=".services.CallService"/>
<service android:name=".services.DownloadService"/>
<service
android:name="com.twilio.client.TwilioClientService"
android:exported="false"
android:stopWithTask="false" />
<meta-data
... />
...
</application>
</manifest>
最佳答案
如果您使用的是 twilio 演示,那么您需要在 Androidmenifest 中提供服务
<service android:name="com.twilio.client.TwilioClientService" android:exported="false" android:stopWithTask="true"/>
它对我有用。
twilio provide他们自己的服务。所以你只需要在 menifest 上声明它。所以不需要创建新服务。
jusr 删除此服务并将其放在 android menifest 上。它会在应用程序关闭后自动启动。
关于android - 如果应用关闭,粘性服务中定义的 Twilio 设备不会启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35595666/