我正在尝试使用addIncomingCall in Android TelecomManager not doing anything中的代码触发Android上的来电用户界面,但我无法显示来电用户界面。
成功调用 onCreateOutgoingConnection
因为我能够在该回调中记录消息。
MainActivity.java
package com.example.test.voip;
import android.content.Context;
import android.content.ComponentName;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telecom.ConnectionService;
import android.net.Uri;
import android.content.Intent;
import android.util.Log;
import com.example.test.voip.*;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import android.telecom.VideoProfile;
import android.Manifest;
import android.support.v4.app.ActivityCompat;
@RequiresApi(api = Build.VERSION_CODES.M)
public class MainActivity extends AppCompatActivity {
private static String TAG = "MainActivity";
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
TelecomManager tm = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
new ComponentName(this.getApplicationContext(), MyService.class),
"example");
Log.i(TAG, tm.toString());
PhoneAccount phoneAccount = PhoneAccount.builder(phoneAccountHandle, "abc").setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER).build();
tm.registerPhoneAccount(phoneAccount);
Bundle extras = new Bundle();
Log.i(TAG, tm.toString());
Uri uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, "3924823201", null);
extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS, uri);
extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
tm.addNewIncomingCall(phoneAccountHandle, extras);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
// public void sendIncomingCallIntent(Context context, Uri handle,
// boolean isVideoCall) {
// PhoneAccountHandle phoneAccount = new PhoneAccountHandle(
// new ComponentName(context, MyService.class),
// "testapps_TestConnectionService_SIM_SUBSCRIPTION_ID");
//
// // For the purposes of testing, indicate whether the incoming call is a video call by
// // stashing an indicator in the EXTRA_INCOMING_CALL_EXTRAS.
// Bundle extras = new Bundle();
// extras.putBoolean("abcd",
// true);
// if (handle != null) {
// extras.putParcelable("abc", handle);
// }
//
// TelecomManager tmabc = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
// tmabc.addNewIncomingCall(phoneAccount,
// extras);
// }
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test.voip">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.telecom.ConnectionService"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:label="example"
android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
<intent-filter>
<action android:name="android.telecom.ConnectionService"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
MyService.java
package com.example.test.voip;
import android.support.annotation.RequiresApi;
import android.telecom.ConnectionService;
import android.telecom.PhoneAccountHandle;
import android.telecom.ConnectionRequest;
import android.telecom.Connection;
import android.os.Build;
import android.util.Log;
@RequiresApi(api = Build.VERSION_CODES.M)
public class MyService extends ConnectionService {
private static String TAG = "MyService";
public MyService() {
}
@Override
public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
Log.i(TAG,"onCreateIncomingConnection60");
Log.i(TAG,connectionManagerPhoneAccount.toString());
Log.i(TAG,request.toString());
return super.onCreateIncomingConnection(connectionManagerPhoneAccount, request);
}
public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
return super.onCreateOutgoingConnection(connectionManagerPhoneAccount,request);
}
}