我有一个使用Google PlacePicker和Google StreetView的应用程序。我必须在飞机模式下使用设备测试应用程序。
如果飞行模式处于活动状态,我希望应用程序检查它何时到达此活动,如果它未激活,我希望显示Toast
以下是活动的代码活动,其中有2个按钮,其中一个按钮加载PlacePicker,另一个加载另一个包含StreetView的活动。
我已经看到了这个How can one detect airplane mode on Android?的答案,这就是我在答案中得到的代码,但它不起作用
有人可以告诉我如何使这个工作
public class PlacePickerActivity extends AppCompatActivity {
int PLACE_PICKER_REQUEST = 1;
TextView tvPlace;
private Button buttonStepsActivity;
private Button buttonStreet;
private boolean isAirplaneModeOn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_picker);
tvPlace = (TextView)findViewById(R.id.tvPlace);
//StreetView
buttonStreet = (Button) findViewById(R.id.buttonStreet);
if(isAirplaneModeOn) {
Toast.makeText(this,"Aeroplane mode active",Toast.LENGTH_LONG).show();
}
}
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}
public void goPlacePicker(View v) {
// Construct an intent for the place picker
try {
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
Intent intent = intentBuilder.build(this);
// Start the intent by requesting a result,
// identified by a request code.
startActivityForResult(intentBuilder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
String toastMsg = String.format("You have chosen: %s", place.getName());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
使用这种方式
查找网络启用
public static boolean isNetworkAvailable(Context mContext) {
ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
启用飞行模式启用
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}
添加此权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />