-2

I am getting the Error message while i am trying to call the Toast.makeText function inside the onReceive function of the BroadcastReceiver type class. This is not a duplicate of another Question because i am calling the show() method and this is compile time issue Error is : cannot resolve method makeText.

The same function working fine if i am calling it inside any other Activity Type Class.

Here is my code that i am trying to run .

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class CallStateReceiver  extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(this,"test", Toast.LENGTH_SHORT).show();
    }
}
Community
  • 1
  • 1
  • 3
    Possible duplicate of [Toast message from Broadcast Receiver](http://stackoverflow.com/questions/7900976/toast-message-from-broadcast-receiver) – greenmarker Aug 06 '16 at 09:37

3 Answers3

2

I guess its having trouble with the context. Change it to:

Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
Shaishav
  • 5,282
  • 2
  • 22
  • 41
2

Pass Context from BroadcastReceiver class to Toast.change your toast onReceive method like below

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

  Toast.makeText(context,"test", Toast.LENGTH_SHORT).show();
 }
sasikumar
  • 12,540
  • 3
  • 28
  • 48
1

Change this

Toast.makeText(this,"test", Toast.LENGTH_SHORT).show();

to this

Toast.makeText(context,"test", Toast.LENGTH_SHORT).show();

and try

Rosemol J
  • 183
  • 4
  • 19