官網(wǎng)地址
注:本文寫作時,安卓發(fā)布的最新版本為Android 10 (Q)
正文:
- 這個接口在Android 6.0之后添加力九,就是說如果是使用這個接口耍铜,應(yīng)用支持的最低版本api為23
- 根據(jù)官網(wǎng)介紹,這個類的作用只有一個:替換默認(rèn)通話應(yīng)用
- 使用
RoleManager
這個類來實現(xiàn)替換默認(rèn)通話應(yīng)用(Android 8.0以上) - 應(yīng)用要在通話狀態(tài)時提供UI界面(駕駛模式除外)
- 應(yīng)用必須滿足以下條件:
- 必須接受
Intent#ACTION_DIAL
的Intent
跌前,就是說棕兼,應(yīng)用必須提供撥號盤界面以便撥打電話 - 必須繼承實現(xiàn)
InCallService
,并且同時提供接電話和通話中的UI
- 必須接受
注意:如果應(yīng)用成為了默認(rèn)的通話應(yīng)用抵乓,然后在通話過程中崩潰了(crash)伴挚,系統(tǒng)會自動切換上一個默認(rèn)通話應(yīng)用,同時會通知用戶你的應(yīng)用崩潰了灾炭。然后從此以后系統(tǒng)會使用上一個默認(rèn)通話應(yīng)用來撥打緊急電話
如何替換茎芋?
1、如何接受Intent#ACTION_DIAL的Intent
創(chuàng)建一個Activity
接受Intent
<activity android:name="your.package.YourDialerActivity"
android:label="@string/yourDialerActivityLabel">
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel" />
</intent-filter>
</activity>
2蜈出、如何繼承InCallService
創(chuàng)建一個Service
繼承它
class
public class MyService extends InCallService{}
/********************************************************************************/
Manifest.xml
<service android:name="your.package.YourInCallServiceImplementation"
android:permission="android.permission.BIND_INCALL_SERVICE">
<meta-data android:name="android.telecom.IN_CALL_SERVICE_UI" android:value="true" />
<meta-data android:name="android.telecom.IN_CALL_SERVICE_RINGING"
android:value="true" />
<intent-filter>
<action android:name="android.telecom.InCallService"/>
</intent-filter>
</service>
android.telecom.IN_CALL_SERVICE_UI
:表明會替換內(nèi)置的默認(rèn)UI(測試發(fā)現(xiàn)田弥,如果設(shè)置為false,這不會調(diào)用自己的Service)
android.telecom.IN_CALL_SERVICE_RINGING
:表明來電時會播放鈴聲(經(jīng)測試掏缎,若設(shè)置為false皱蹦,會調(diào)用自己的Service煤杀,會調(diào)用系統(tǒng)播放鈴聲的功能)
3眷蜈、如何使用RoleManager
private static final int REQUEST_ID = 1;
public void requestRole() {
RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE);
Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_DIALER);
startActivityForResult(intent, REQUEST_ID);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ID) {
if (resultCode == android.app.Activity.RESULT_OK) {
// Your app is now the default dialer app
} else {
// Your app is not the default dialer app
}
}
}
上面的代碼會彈出一個Dialog
詢問用戶是否替換
替換后要怎么做
來電時
通過
InCallSevice#onCallAdded(Call)
接收來電時,你需要展示來電的UI——通過NotificationManager
接口展示一個來電通知如果你的應(yīng)用聲明了
android.telecom.IN_CALL_SERVICE_RINGING
沈自,來電時你還要播放鈴聲酌儒,通過創(chuàng)建一個NotificationChannel
來指定播放的鈴聲,例如:
NotificationChannel channel = new NotificationChannel(YOUR_CHANNEL_ID, "Incoming Calls",
NotificationManager.IMPORTANCE_MAX);
// other channel setup stuff goes here.
// We'll use the default system ringtone for our incoming call notification channel. You can
// use your own audio resource here.
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
channel.setSound(ringtoneUri, new AudioAttributes.Builder()
// Setting the AudioAttributes is important as it identifies the purpose of your
// notification sound.
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
NotificationManager mgr = getSystemService(NotificationManager.class);
mgr.createNotificationChannel(channel);
來電時枯途,創(chuàng)建一個通知忌怎,并且把它和你創(chuàng)建的通知渠道關(guān)聯(lián)起來
- 您可以在通知上指定一個
PendingContent
籍滴,該通知將啟動你提供的全屏來電UI,例如:
// Create an intent which triggers your fullscreen incoming call user interface.
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, YourIncomingCallActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, 0);
// Build the notification as an ongoing high priority item; this ensures it will show as
// a heads up notification which slides down over top of the current content.
final Notification.Builder builder = new Notification.Builder(context);
builder.setOngoing(true);
builder.setPriority(Notification.PRIORITY_HIGH);
// Set notification content intent to take user to the fullscreen UI if user taps on the
// notification body.
builder.setContentIntent(pendingIntent);
// Set full screen intent to trigger display of the fullscreen UI when the notification
// manager deems it appropriate.
builder.setFullScreenIntent(pendingIntent, true);
// Setup notification content.
builder.setSmallIcon( yourIconResourceId );
builder.setContentTitle("Your notification title");
builder.setContentText("Your notification content.");
// Use builder.addAction(..) to add buttons to answer or reject the call.
NotificationManager notificationManager = mContext.getSystemService(
NotificationManager.class);
notificationManager.notify(YOUR_CHANNEL_ID, YOUR_TAG, YOUR_ID, builder.build());
- 如果用戶正在使用手機(jī)榴啸,系統(tǒng)會展示一個通知孽惰,如果用戶沒有在使用手機(jī),系統(tǒng)會展示你提供的全屏來電UI
官網(wǎng)就這么多鸥印,更多的大家自己挖掘吧