更改连接时在来自 BroadcastReceiver 的 activity 消息中显示
Show in activity message from BroadcastReceiver when change connectivity
我有控制连接的 BroadCastReceiver。
当我失去连接时,我会显示消息,但不是通用 toast,而是我设计的 "layout"。
我的广播是用于连接的通用广播:
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 to 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. ");
}
}
}
还有我的"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>
有没有办法让这个布局模糊显示当前 activity?
创建一个全局布尔值,并在有互联网连接时将其设置为真,在未连接时将其设置为假。
然后创建一个异步任务 class doinBackground 方法并在变量更改时连续检查它,并且从 postExecute 方法您可以在 UI 中进行更改。如果您需要更多帮助,请联系我。
@Override
public void onReceive(final Context context, final Intent intent) {
//Obviously cleanup where you do this stuff, to make it efficient
//If your context is a fragment you need to cast to fragment and then do 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);
}
}
只需触发一个函数,然后编写您要显示的视图元素即可。
public class ConnectionBroadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
} else {
Visibler();
}
}
}
@Override
protected void onResume() {
this.connectionBroadReceiver = new ConnectionBroadReceiver();
registerReceiver(this.connectionBroadReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(this.connectionBroadReceiver);
super.onPause();
}
然后在主程序里面,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatingActionButton = (FloatingActionButton) findViewById(R.id.floater);
}
private void Visibler() {
floatingActionButton.setVisibility(View.VISIBLE);
}
我有控制连接的 BroadCastReceiver。 当我失去连接时,我会显示消息,但不是通用 toast,而是我设计的 "layout"。 我的广播是用于连接的通用广播:
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 to 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. ");
}
}
}
还有我的"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>
有没有办法让这个布局模糊显示当前 activity?
创建一个全局布尔值,并在有互联网连接时将其设置为真,在未连接时将其设置为假。 然后创建一个异步任务 class doinBackground 方法并在变量更改时连续检查它,并且从 postExecute 方法您可以在 UI 中进行更改。如果您需要更多帮助,请联系我。
@Override
public void onReceive(final Context context, final Intent intent) {
//Obviously cleanup where you do this stuff, to make it efficient
//If your context is a fragment you need to cast to fragment and then do 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);
}
}
只需触发一个函数,然后编写您要显示的视图元素即可。
public class ConnectionBroadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
} else {
Visibler();
}
}
}
@Override
protected void onResume() {
this.connectionBroadReceiver = new ConnectionBroadReceiver();
registerReceiver(this.connectionBroadReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(this.connectionBroadReceiver);
super.onPause();
}
然后在主程序里面,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatingActionButton = (FloatingActionButton) findViewById(R.id.floater);
}
private void Visibler() {
floatingActionButton.setVisibility(View.VISIBLE);
}