San*_*ani 6 android call telephonymanager android-phone-call
有没有办法在没有root权限的情况下以编程方式应答Android 7.0中的来电?我尝试了以下方式接听来电。
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
Object telephonyService = m.invoke(tm);
Class<?> telephonyServiceClass = Class.forName(telephonyService.getClass().getName());
Method endCallMethod = telephonyServiceClass.getDeclaredMethod("answerRingingCall");
endCallMethod.invoke(telephonyService);
Run Code Online (Sandbox Code Playgroud)
您可以使用通知侦听器执行一个奇怪的技巧,通过创建服务来侦听所有通知,每当有电话打来时,它肯定会显示一条通知,询问用户是否接听。
创建服务
class AutoCallPickerService : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification?) {
super.onNotificationPosted(sbn)
sbn?.let {
it.notification.actions?.let { actions ->
actions.iterator().forEach { item ->
if (item.title.toString().equals("Answer", true)) {
val pendingIntent = item.actionIntent
pendingIntent.send()
}
}
}
}
}
override fun onNotificationRemoved(sbn: StatusBarNotification?) {
super.onNotificationRemoved(sbn)
}
}
Run Code Online (Sandbox Code Playgroud)
这里我们假设通知有一个标签为answer的操作,因此我们进行匹配并调用与其相关的相应意图。在启动器活动中请求访问通知的权限
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
startActivity(intent)
}
}
Run Code Online (Sandbox Code Playgroud)
最后注册监听通知的服务
<service
android:name=".AutoCallPickerService"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action
android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
如果手机型号不显示来电通知,则此代码将完全失败。
| 归档时间: |
|
| 查看次数: |
380 次 |
| 最近记录: |