I got this problem: getSystemService defines in Context class, so I assume it is called when context.getSystemService. I can't understand the following code where getSystemService is called directly in BroadcastReceiver. I run the code and no error show!
Codes:
public class MainActivity extends Activity {
……
class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectionManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable())
{
Toast.makeText(context, "network is available",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "network is unavailable",
Toast.LENGTH_SHORT).show();
}
}
}
}
getSystemService() is part of the Context. You need to use the Context you receive in your onReceive() method:
@Override
public void onReceive(Context context, Intent i) {
UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With