Reputation: 137
I have done a code exploiting the Broadcast receiver in order to call the method on Receive when the status of connection change and show a toast indicating the change. In that sense I have a separated class in which is implemented the broadcast receiver in that way:
public class Receiver extends BroadcastReceiver {
private static final String DEBUG_TAG = "NetworkStatusExample";
@Override
public void onReceive(Context context, Intent intent) {
// Shows a toast when the connectivity state change
Toast.makeText(context, "stato wifi cambiato",
Toast.LENGTH_LONG).show();
Log.d("prova", "action: "
+ intent.getAction())
}
}
it works recalling correctly the method OnReceive() when there is a network state change and showing the toast.
In the main file I have put a code that once a button is pressed it verifies if the Wifi is connected or not also in this case showing a toast with a message "Wifi connected: true/false" depending on the presence or not of connectivity. The code that works also in this case is:
public class MainActivity extends Activity {
private static final String DEBUG_TAG = "NetworkStatusExample";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final boolean isWifiConn = networkInfo.isConnected();
String wifiStateText = "No State";
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Button prova = (Button) findViewById(R.id.button1);
prova.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence text = "Wifi connected: " + isWifiConn;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
}
Now I'm trying to embedding the last part of the code in the method OnReceive() defined above in order to let's do all in automatic once there is a network connectivity change ando so I the resulting code of the Broadcast receiver is that one:
public class Receiver extends BroadcastReceiver {
private static final String DEBUG_TAG = "NetworkStatusExample";
// Verifica se il wifi è connesso
@Override
public void onReceive(Context context, Intent intent) {
// Shows a toast when the connectivity state change
Toast.makeText(context, "stato wifi cambiato",
Toast.LENGTH_LONG).show();
Log.d("prova", "action: "
+ intent.getAction());
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final boolean isWifiConn = networkInfo.isConnected();
String wifiStateText = "No State";
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
// Toast that shows if the wifi is connected
CharSequence text = "Wifi connected: " + isWifiConn;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
In that case I get this error:
the method getsystemservice(string) is undefined for the type Receiver
on this line:
getSystemService(Context.CONNECTIVITY_SERVICE);
What's wrong? What have I to do to fix it?
Upvotes: 0
Views: 697
Reputation: 19790
You need to use the 'context'
parameter: context.getSystemService(Context.CONNECTIVITY_SERVICE);
The BroadcastReceiver
is not derived from Context
.
Upvotes: 1
Reputation: 2297
You can try this :
context.getSystemService(Context.CONNECTIVITY_SERVICE);
Upvotes: 0