gpt4 book ai didi

java - 如何从 Android 中的 BroadcastReceiver 调用 onPrepareOptionsMenu() ?

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:04 26 4
gpt4 key购买 nike

I am beginner to android,I have included 2 icons in internet.xml ,based on the network connection(Example if network is connected it should show connected icon or else it should show not connected icon on the toolbar),for that I have created onPrepareOptionsMenu() in activity as well as BroadcastReceiver to broadcast the present network status based on that the icon has to change dynamically.

Here I have used Intent to broadcast the current network status,it is again relaunching the activity,I don't know how to call onPrepareOptionsMenu() inside the BroadcastReceiver class to automatically update menu items in toolbar based on the network status.

Can anyone help me to do this?.

**ConnectionActivity**

public class ConnectionActivity extends AppCompatActivity {
Menu menu;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parser);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.internet, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean online = false;
MenuItem netMenuItem = menu.findItem(R.id.network_signal);
MenuItem nonetMenuItem = menu.findItem(R.id.network_signal_off);
int status = NetworkUtil.getConnectivityStatus(this);
if (status == 1 || status == 2)
online = true;
else
online = false;
netMenuItem.setVisible(online);
nonetMenuItem.setVisible(!online);

return true;
}

}

**NetworkUtil.java**


public class NetworkUtil {
public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;


public static int getConnectivityStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
Log.d("WIFI CONNECTED", activeNetwork.getType() + "");
return TYPE_WIFI;
}
if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
Log.d("MOBILE CONNECTED", activeNetwork.getType() + "");
return TYPE_MOBILE;
}
}
Log.d("NOT CONNECTED", TYPE_NOT_CONNECTED + "");
return TYPE_NOT_CONNECTED;
}

public static String getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
String status = null;
if (conn == NetworkUtil.TYPE_WIFI) {
status = "Wifi enabled";
} else if (conn == NetworkUtil.TYPE_MOBILE) {
status = "Mobile data enabled";
} else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
status = "Not connected to Internet";
}
return status;
}
}

**NetworkChangeReceiver**


public class NetworkChangeReceiver extends BroadcastReceiver {

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onReceive(Context context, Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
Toast.makeText(context, status, Toast.LENGTH_LONG).show();
Intent activityIntent = new Intent(context, ParserActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}

**Menifest.xml**

<receiver
android:name=".NetworkChangeReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>

**internet.xml**


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="signal_off"
android:id="@+id/network_signal_off"
android:icon="@drawable/ic_signal_wifi_off_black_24dp"
app:showAsAction="always"></item>
<item
android:id="@+id/network_signal"
android:icon="@drawable/ic_signal_wifi_4_bar_black_24dp"
android:title="Signal"
app:showAsAction="always"></item>
</menu>

最佳答案

如果您调用invalidateOptionsMenu();,这将触发

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}

所以你可以在这里做出改变

@Override
public void onReceive(Context context, Intent intent) {
//your logic
//invalidate the options menu
invalidateOptionsMenu();
}

关于java - 如何从 Android 中的 BroadcastReceiver 调用 onPrepareOptionsMenu() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48180672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com