Show Complex Toast From BroadcastReceiver

我的未来我决定 提交于 2019-12-03 15:42:31

You can use LayoutInflater.from(context) to get your LayoutInflater. Like this:

LayoutInflater mInflater = LayoutInflater.from(context);
View myView = mInflater.inflate(R.layout.customToast, null);
TextView sender = (TextView) myView.findViewById(R.id.sender);
TextView message = (TextView) myView.findViewById(R.id.message);

Your compile errors are because BroadcastReceiver does not inherit from Context. Use the Context that is passed into onReceive() (and get rid of getApplicationContext() -- just use the Context you are passed).

Now, this may still not work, as I am not sure if you can raise a Toast from a BroadcastReceiver in the first place, but this will at least get you past the compile errors.

A toast can be created and displayed from an Activity or Service, not Broadcast receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!