このコードを試してください
Button btnWiFi = (Button)findViewById(R.id.btnWiFi);
btnWiFi.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WifiManager wifi =(WifiManager)getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled())
{
Toast.makeText(getBaseContext(), "WiFi is Enabled", Toast.LENGTH_LONG).show();
}
else
{
AlertDialog builder = new AlertDialog.Builder(MainActivity.this).create();
builder.setTitle("Caution");
builder.setMessage("WiFi is Disabled."+"\n"+"Press Ok To Turn On WiFi.");
builder.setIcon(R.drawable.caution);
builder.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int which) {
// Write your code here to execute after dialog closed
final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
}
});
builder.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
}
});