File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
Phone-Call-Detection-Java
app/src/main/java/com/tokbox/sample/phonecalldetection Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ This is the code responsible for listening for the call status:
17
17
18
18
``` java
19
19
TelephonyManager telephonyManager = (TelephonyManager ) getSystemService(Context . TELEPHONY_SERVICE );
20
+ // Phone state permissions are required from Api 31. Add check of permission to avoid crash.
20
21
telephonyManager. listen(phoneStateListener, PhoneStateListener . LISTEN_CALL_STATE );
21
22
22
23
// ...
Original file line number Diff line number Diff line change @@ -71,8 +71,27 @@ private void startVideoPublish(Session session) {
71
71
session .publish (publisher );
72
72
}
73
73
74
+ private boolean hasPhoneStatePermission () {
75
+ if (Build .VERSION .SDK_INT >= 31 ) {
76
+ if (context .checkSelfPermission (Manifest .permission .READ_PHONE_STATE )
77
+ != PackageManager .PERMISSION_GRANTED ) {
78
+ log .e ("Some features may not be available unless the phone permissions has been granted explicitly " +
79
+ "in the App settings." );
80
+ return false ;
81
+ }
82
+ }
83
+ return true ;
84
+ }
85
+
74
86
private void registerPhoneListener () {
75
87
TelephonyManager telephonyManager = (TelephonyManager ) getSystemService (Context .TELEPHONY_SERVICE );
88
+
89
+ if (!hasPhoneStatePermission ()) {
90
+ log .d ("No Phone State permissions. Register phoneStateListener cannot " +
91
+ "be completed." );
92
+ return ;
93
+ }
94
+
76
95
telephonyManager .listen (phoneStateListener , PhoneStateListener .LISTEN_CALL_STATE );
77
96
}
78
97
You can’t perform that action at this time.
0 commit comments