rahul shrivastava rahul shrivastava - 2 months ago 11x
Android Question

Why Toast.maketoast is not working in the BroadcastReceiver class

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.

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{
public void onReceive(Context context, Intent intent) {
Toast.makeText(this,"test", Toast.LENGTH_SHORT).show();


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

 public void onReceive(Context context, Intent intent) {

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