rahul shrivastava rahul shrivastava - 1 year ago 127
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();

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download