[Android]InCallService詳解

官網(wǎng)地址
注:本文寫作時,安卓發(fā)布的最新版本為Android 10 (Q)
正文:

  1. 這個接口在Android 6.0之后添加力九,就是說如果是使用這個接口耍铜,應(yīng)用支持的最低版本api為23
  2. 根據(jù)官網(wǎng)介紹,這個類的作用只有一個:替換默認(rèn)通話應(yīng)用
  3. 使用RoleManager這個類來實現(xiàn)替換默認(rèn)通話應(yīng)用(Android 8.0以上)
  4. 應(yīng)用要在通話狀態(tài)時提供UI界面(駕駛模式除外)
  5. 應(yīng)用必須滿足以下條件:
    1. 必須接受Intent#ACTION_DIALIntent跌前,就是說棕兼,應(yīng)用必須提供撥號盤界面以便撥打電話
    2. 必須繼承實現(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詢問用戶是否替換


替換后要怎么做

來電時
  1. 通過InCallSevice#onCallAdded(Call)接收來電時,你需要展示來電的UI——通過NotificationManager接口展示一個來電通知

  2. 如果你的應(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)起來

  1. 您可以在通知上指定一個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());
  1. 如果用戶正在使用手機(jī)榴啸,系統(tǒng)會展示一個通知孽惰,如果用戶沒有在使用手機(jī),系統(tǒng)會展示你提供的全屏來電UI

官網(wǎng)就這么多鸥印,更多的大家自己挖掘吧

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末勋功,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子库说,更是在濱河造成了極大的恐慌狂鞋,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件潜的,死亡現(xiàn)場離奇詭異骚揍,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)啰挪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進(jìn)店門信不,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人脐供,你說我怎么就攤上這事浑塞。” “怎么了政己?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵酌壕,是天一觀的道長。 經(jīng)常有香客問我歇由,道長卵牍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任沦泌,我火速辦了婚禮糊昙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘谢谦。我一直安慰自己释牺,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布回挽。 她就那樣靜靜地躺著没咙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪千劈。 梳的紋絲不亂的頭發(fā)上祭刚,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼涡驮。 笑死暗甥,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的捉捅。 我是一名探鬼主播撤防,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼棒口!你這毒婦竟也來了即碗?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤陌凳,失蹤者是張志新(化名)和其女友劉穎剥懒,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體合敦,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡初橘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了充岛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片保檐。...
    茶點故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖崔梗,靈堂內(nèi)的尸體忽然破棺而出夜只,到底是詐尸還是另有隱情,我是刑警寧澤蒜魄,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布扔亥,位于F島的核電站,受9級特大地震影響谈为,放射性物質(zhì)發(fā)生泄漏旅挤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一伞鲫、第九天 我趴在偏房一處隱蔽的房頂上張望粘茄。 院中可真熱鬧,春花似錦秕脓、人聲如沸柒瓣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽芙贫。三九已至,卻和暖如春诵肛,著一層夾襖步出監(jiān)牢的瞬間屹培,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工怔檩, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留褪秀,地道東北人。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓薛训,卻偏偏與公主長得像媒吗,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子乙埃,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,619評論 2 354