下面是我用于在来电期间显示提醒的代码。但问题是它不能用于我的Redmi 3s prime或任何在6.0上运行的设备。是否有我必须要求的任何许可或任何其他方式。任何帮助都会更有帮助。
CallBarring.java
public class CallBarring extends BroadcastReceiver {
// This String will hold the incoming phone number
private String number;
CustomDialog dialog;
TelephonyManager telephonyManager;
PhoneStateListener listener;
Context context;
@Override
public void onReceive(final Context context, Intent intent) {
// If, the received action is not a type of "Phone_State", ignore it
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
// Else, try to do some action
else {
this.context = context;
if(dialog == null){
dialog = new CustomDialog(context);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
// Fetch the number of incoming call
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
dialog.dismiss();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
dialog.dismiss();
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
dialog.show();
break;
}
Toast.makeText(context, stateString,Toast.LENGTH_LONG).show();
}
};
// Register the listener with the telephony manager
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
// Check, whether this is a member of "Black listed" phone numbers
// stored in the database
/*if (MainActivity.blockList.contains(new Blacklist(number))) {
// If yes, invoke the method
disconnectPhoneItelephony(context);
return;
}*/
}
}
// Method to disconnect phone automatically and programmatically
// Keep this method as it is
@SuppressWarnings({ "rawtypes", "unchecked" })
private void disconnectPhoneItelephony(Context context) {
ITelephony telephonyService;
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
class CustomDialog extends Dialog implements OnClickListener {
public CustomDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
Button btnEndCall = (Button) findViewById(R.id.end_call);
//btnEndCall.set
btnEndCall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
disconnectPhoneItelephony(context);
}
}
下面是我的 Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="--------------"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".CallBarring" >
<intent-filter android:priority="100" >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
</manifest>
答案 0 :(得分:0)
您可以将运行时权限视为:
private static final int READ_PHONE_STATE = 1;
private static String[] PERMISSIONS = {
Manifest.permission.READ_PHONE_STATE,
};
private boolean checkPermissions() {
int result;
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p : PERMISSIONS) {
result = ContextCompat.checkSelfPermission(mContext, p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), READ_PHONE_STATE);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case READ_PHONE_STATE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted.
} else {
//code for deny
}
break;
}
}