【问题标题】:Check if there is network/aeroplane mode active?检查是否有网络/飞行模式激活?
【发布时间】:2017-12-10 23:18:59
【问题描述】:

我有一个使用 Google PlacePicker 和 Google StreetView 的应用程序。我必须在飞行模式下使用设备测试应用程序。

如果飞行模式处于活动状态,我希望应用程序检查它何时到达此活动,如果它未处于活动状态,我希望显示 Toast

下面是 Activity 的代码,其中有 2 个按钮,其中一个按钮加载 PlacePicker,另一个加载另一个包含 StreetView 的 Activity。

我已经看到了这个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();
    }
}
}

【问题讨论】:

标签: java android android-studio android-sensors


【解决方案1】:

这样使用

找到网络启用

    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" />

【讨论】:

  • 我已将此添加到我的活动中,我将如何显示一条消息,以便如果设备处于飞行模式,则会弹出一条消息告诉用户设备处于飞行模式?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
  • 2015-07-27
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 2012-02-21
  • 1970-01-01
相关资源
最近更新 更多