Android Broadcast Receiver For Call Not Working? (marshmallow)
Solution 1:
In Case of Marshmallow Version, We have a concept called Runtime permission which is to be made inside Activity in order to work with the permission. Runtime permission provides a way to ask user for particular permission at runtime while he runs activity for first time.
This are two things you have to specify :
//specify any constant number for permission
publicfinalstaticint MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 11;
// Specify following bit of code in OnCreate method
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Here, thisActivity is the current activityif (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_CONTACTS)) {
} else {
ActivityCompat.requestPermissions(this,
newString[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
}
}
//specify this method which will popup window asking user for permission at runtime
@OverridepublicvoidonRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
// If request is cancelled, the result arrays are empty.if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
return;
}
}
}
this will provide a way to work with Marshmallow devices
Solution 2:
You have given wrong package name in the receiver.
You should define receiver as below:
<receiverandroid:name="com.suhas.callreceiver.MyCallReceiver"><intent-filter><actionandroid:name="android.intent.action.PHONE_STATE" /></intent-filter></receiver>
Solution 3:
In target API 23 or higher as per the Marshmallow the applications needs run time permission or manual in your device setting>> apps>> select your app>> permission
Solution 4:
I successfully implemented in our App. Get the reference from here.
Call Receive Method
publicclassCallReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
//Log.w("intent " , intent.getAction().toString());TelephonyManagertelephony= (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneStateListenercustomPhoneListener=newMyPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundlebundle= intent.getExtras();
Stringphone_number= bundle.getString("incoming_number");
StringstateStr= intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
// String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);intstate=0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
state = TelephonyManager.CALL_STATE_IDLE;
}
elseif(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
state = TelephonyManager.CALL_STATE_OFFHOOK;
}
elseif(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
state = TelephonyManager.CALL_STATE_RINGING;
}
if (phone_number == null || "".equals(phone_number)) {
return;
}
customPhoneListener.onCallStateChanged(context, state, phone_number);
Toast.makeText(context, "Phone Number " + phone_number , Toast.LENGTH_SHORT).show();
}}
Listener Method
publicclassMyPhoneStateListenerextendsPhoneStateListener {
privatestatic int lastState = TelephonyManager.CALL_STATE_IDLE;
privatestaticDate callStartTime;
privatestaticboolean isIncoming;
publicvoidonCallStateChanged(Context context, int state, String phoneNumber){
if(lastState == state){
//No change, debounce extrasreturn;
}
System.out.println("Number inside onCallStateChange : " + phoneNumber);
switch(state){
caseTelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = newDate();
Toast.makeText(context, "Incoming Call Ringing " + phoneNumber, Toast.LENGTH_SHORT).show();
break;
caseTelephonyManager.CALL_STATE_OFFHOOK:
if(lastState != TelephonyManager.CALL_STATE_RINGING){
isIncoming = false;
callStartTime = newDate();
Toast.makeText(context, "Outgoing Call Started " + phoneNumber, Toast.LENGTH_SHORT).show();
}
break;
caseTelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)if(lastState == TelephonyManager.CALL_STATE_RINGING){
//Ring but no pickup- a missToast.makeText(context, "Ringing but no pickup" + phoneNumber + " Call time " + callStartTime +" Date " + newDate() , Toast.LENGTH_SHORT).show();
}
elseif(isIncoming){
Toast.makeText(context, "Incoming " + phoneNumber + " Call time " + callStartTime , Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "outgoing " + phoneNumber + " Call time " + callStartTime +" Date " + newDate() , Toast.LENGTH_SHORT).show();
}
break;
}
lastState = state;
}} }
Get the reference for full solution
Post a Comment for "Android Broadcast Receiver For Call Not Working? (marshmallow)"