0

I am trying to display toast after receiving an call, I have implemented all necessary things needed to register broadcast receiver but it is not displaying toast. I am trying to run this program on Marshmallow device

MyCallReceiver.java -

package com.suhas.callreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class MyCallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        // This code will execute when the phone has an incoming call

        // get the phone number
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
        Log.d("MyTrack call", "call receive");

    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_IDLE))
    {
        Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
    }
    else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        // This code will execute when the call is disconnected

    }
}
}

AndroidManifest.xml -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.suhas.msgmanager">

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/msgis"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <activity android:name="com.example.suhas.msgmanager.MyDialog" android:launchMode="singleTask"
        android:theme="@android:style/Theme.Translucent" />

    <service android:name="com.example.suhas.msgmanager.ChatHeadService"></service>

    <receiver android:name=".MyCallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

    <activity android:name=".AddMessageActivity">
    </activity>
</application>

</manifest>

I have one MainActivity with one default label saying Hello World.

Suhas Gawde
  • 161
  • 2
  • 9
  • Have a look here: http://www.tutorialforandroid.com/2009/01/get-phone-state-when-someone-is-calling_22.html – Shaishav Aug 11 '16 at 17:53
  • Did you check the package name. Your app package name is com.example.suhas.msgmanager and you have defined receiver in com.suhas.callreceiver package – Sandeep Aug 12 '16 at 06:29
  • i am running on marshmallow device, so how can i specify runtime permission in marshmallow. cause marshmallow requires runtime permission – Suhas Gawde Aug 12 '16 at 06:58

4 Answers4

7

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

  public final static int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 11;

// Specify following bit of code in OnCreate method

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Here, thisActivity is the current activity
    if (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,
                    new String[]{Manifest.permission.READ_PHONE_STATE},
                    MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
        }
    }
}

//specify this method which will popup window asking user for permission at runtime

@Override
public void onRequestPermissionsResult(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

Suhas Gawde
  • 161
  • 2
  • 9
0

You have given wrong package name in the receiver.

You should define receiver as below:

<receiver android:name="com.suhas.callreceiver.MyCallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
Sandeep
  • 1,814
  • 21
  • 25
  • I just tested your code with proper package name and its working fine. – Sandeep Aug 12 '16 at 06:41
  • i am running on marshmallow device, so how can i specify runtime permission in marshmallow. cause marshmallow requires runtime permission – Suhas Gawde Aug 12 '16 at 06:59
  • Check the link for runtime permission https://developer.android.com/training/permissions/requesting.html – Sandeep Aug 12 '16 at 11:06
0

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

this link can help you

0

I successfully implemented in our App. Get the reference from here.

Call Receive Method

public class CallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    //Log.w("intent " , intent.getAction().toString());
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    MyPhoneStateListener customPhoneListener = new MyPhoneStateListener();

    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    Bundle bundle = intent.getExtras();
    String phone_number = bundle.getString("incoming_number");

    String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
    // String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
    int state = 0;
    if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
        state = TelephonyManager.CALL_STATE_IDLE;
    }
    else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
        state = TelephonyManager.CALL_STATE_OFFHOOK;
    }
    else if(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

public class MyPhoneStateListener extends PhoneStateListener {

private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;

public void onCallStateChanged(Context context, int state, String phoneNumber){
    if(lastState == state){
        //No change, debounce extras
        return;
    }

    System.out.println("Number inside onCallStateChange : "  + phoneNumber);
    switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
            isIncoming = true;
            callStartTime = new Date();

            Toast.makeText(context, "Incoming Call Ringing " + phoneNumber, Toast.LENGTH_SHORT).show();
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            if(lastState != TelephonyManager.CALL_STATE_RINGING){
                isIncoming = false;
                callStartTime = new Date();
                Toast.makeText(context, "Outgoing Call Started " + phoneNumber, Toast.LENGTH_SHORT).show();
            }
            break;

        case TelephonyManager.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 miss
                Toast.makeText(context, "Ringing but no pickup" + phoneNumber + " Call time " + callStartTime +" Date " + new Date() , Toast.LENGTH_SHORT).show();
            }
            else if(isIncoming){

                Toast.makeText(context, "Incoming " + phoneNumber + " Call time " + callStartTime  , Toast.LENGTH_SHORT).show();
            }
            else{

                Toast.makeText(context, "outgoing " + phoneNumber + " Call time " + callStartTime +" Date " + new Date() , Toast.LENGTH_SHORT).show();

            }

            break;
    }
    lastState = state;
}} }

Get the reference for full solution

Abhay Anand
  • 592
  • 4
  • 12