android - Show in activity message from BroadcastReceiver when change connectivity -
i have broadcastreceiver control connectivity. would, when lost connection, show message, not generic toast, "layout" designed. broadcast generic broadcast connection:
public class connectivitychangereceiver extends broadcastreceiver { @override public void onreceive(final context context, final intent intent) { connectivitymanager cm =(connectivitymanager) context.getsystemservice(context.connectivity_service); if (cm.getactivenetworkinfo()!=null){ toast.maketext(context, "connected internet", toast.length_long).show(); } else{ /** here instead of toast, iwould display message on activity **/ toast.maketext(context, "not connection", toast.length_long).show(); log.i("internet", "---------------------> internet disconnected. "); } } }
and "no_connection_layout"
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:layout_width="match_parent" android:layout_height="52dp" android:text="no internet" android:id="@+id/no_connection" android:layout_weight="1" android:layout_gravity="center|top" android:gravity="center" android:background="@android:color/holo_green_dark" android:textstyle="bold" android:textcolor="#ffffff" /> </framelayout>
is there way show layout indistinctly current activity?
@override public void onreceive(final context context, final intent intent) { //obviously cleanup stuff, make efficient //if context fragment need cast fragment , getactivity instead final view noconnectionbanner = ((activity) context).findviewbyid(r.id.no_connection) connectivitymanager cm = (connectivitymanager) context.getsystemservice(context.connectivity_service); if (cm.getactivenetworkinfo() != null) { noconnectionbanner.setvisibility(view.gone); } else { noconnectionbanner.setvisibility(view.visible); } }
Comments
Post a Comment