-3

I saw this

But not works for me.

I am going to display Toast, in my code:

private class MyPhoneStateListener extends PhoneStateListener {

    public void onCallStateChanged(int state, String incomingNumber) {

        Log.d("MyPhoneListener", state + "   incoming no:" + incomingNumber);

        if (state == 1) {

            String msg = "New Phone Call Event. Incomming Number : " + incomingNumber;
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(getApplicationContext(), msg, duration);
            toast.show();

        }
    }
}

But i have compile time exception neither in getApplicationContext() or in MainActivity.this , or getActivity().

What is the solution?

Community
  • 1
  • 1
Ghkjn sadxzcx
  • 21
  • 1
  • 8

5 Answers5

0

Try this way:

Toast.makeText(getApplicationContext(), "message", Toast.LENGTH_SHORT).show();
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0

You need to pass context from the activity in which you are calling the method,you need to change your code something like below

public void onCallStateChanged(Context context, int state, String incomingNumber) {

    Log.d("MyPhoneListener", state + "   incoming no:" + incomingNumber);

    if (state == 1) {

        String msg = "New Phone Call Event. Incomming Number : " + incomingNumber;
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, msg, duration);
        toast.show();
    }
}

now you need to pass context along with int state and string number like youractivityname.this

karan
  • 8,637
  • 3
  • 41
  • 78
0

You can all getApplicationContext() like that in your class MyPhoneStateListener. This method is not static so getApplicationContext() is like calling this.getApplicationContext(). And your class MyPhoneStateListener does not extends a class with such method.

So the solution will be to add a field Context mContext to your class. Then add a construtor

public MyPhoneStateListener(Context context, ...some other arguments if you need then ...) {
     super();
     mContext = context;
     ...some other stuff if you need then...
}

Then you can make your toast :

enterToast toast = Toast.makeText(mContext, msg, duration);
sonic
  • 1,894
  • 1
  • 18
  • 22
  • then what are you doing her check your code. you are passing `"context"`, along with some other arguments – karan Nov 19 '15 at 10:40
  • Yes in the constructor. Then reference this context in a field. The method `onCallStateChanged` is define in the super class `PhoneStateListener`. If you change its signature, it will not override the method from super class. – sonic Nov 19 '15 at 10:43
  • check the question, method is not overridden – karan Nov 19 '15 at 10:45
  • Well check documentation, http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onCallStateChanged%28int,%20java.lang.String%29 – sonic Nov 19 '15 at 10:49
0

instead of getApplicationContext() try with

Toast toast = Toast.makeText(Youractivity.this, msg, duration);

if you have contentprovider then

getContext()
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
0

You have to run on ui Thread:

  1. get a reference to your activity
  2. do this

            activity.runOnUiThread(new Runnable() { public void run() {
                Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
            }
        });
    
McflyDroid
  • 177
  • 10