Проверка GPS включена или нет - PullRequest
0 голосов
/ 09 июля 2019

Я делаю приложение, которое показывает значения long long, я получил весь код, но мне нужен метод возобновления onPost, в котором он проверяет, включен ли GPS, если он включен, то он должен работать, если нет, он должен оставаться на То же самое, пока не включите GPS, пожалуйста, помогите мне, если кто-нибудь может.

Мне просто нужен метод и метод onResume, чтобы проверить, включен ли GPS или нет, чтобы использовать метод onPost резюме, пожалуйста, помогите мне

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    textView1 = (TextView) findViewById(R.id.location_view);
    button1 = (Button) findViewById(R.id.camera);
    button2 = (Button) findViewById(R.id.upload);
    editText1 = (EditText) findViewById(R.id.remarks);
    imageView11 = (ImageView) findViewById(R.id.image1);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);

    locationText = (TextView) findViewById(R.id.location_view);

    if (ContextCompat.checkSelfPermission(getApplicationContext(),
            android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat
            .checkSelfPermission(getApplicationContext(),
                    android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission
                .ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
    }

    getLocation();
}


@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.item1:
            Toast.makeText(getApplicationContext(), "Account Clicked", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.item2:
            Toast.makeText(getApplicationContext(), "Account Clicked", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.item3:

            AlertDialog.Builder alerDialogbuilder = new AlertDialog.Builder(Main3Activity.this);
            alerDialogbuilder.setTitle("Confirm Logout");
            alerDialogbuilder.setIcon(R.drawable.ic_error_black_24dp);
            alerDialogbuilder.setMessage("Are You Sure You Want to Logout ");
            alerDialogbuilder.setMessage("Logingout will need id password again");
            alerDialogbuilder.setCancelable(false);
            alerDialogbuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intents = new Intent(Main3Activity.this, MainActivity.class);
                    startActivity(intents);
                    Toast.makeText(getApplicationContext(), "Successfull Logout", Toast.LENGTH_SHORT).show();
                }
            });
            alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(getApplicationContext(), "Logout Canceled", Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog alertDialog = alerDialogbuilder.create();
            alertDialog.show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void camera() {
    Intent intents = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intents, CAMERA_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView11.setImageBitmap(photo);
    }
}

private void upload() {
    AlertDialog.Builder alerDialogbuilder = new AlertDialog.Builder(Main3Activity.this);
    alerDialogbuilder.setTitle("Confirm Upload ?");
    alerDialogbuilder.setIcon(R.drawable.ic_error_black_24dp);
    alerDialogbuilder.setMessage("Are You Sure You Want to Upload Data");
    alerDialogbuilder.setCancelable(false);
    alerDialogbuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(getApplicationContext(), "File Uploading...", Toast.LENGTH_SHORT).show();
        }
    });
    alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(getApplicationContext(), "Recheck Data", Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog alertDialog = alerDialogbuilder.create();
    alertDialog.show();
}

@Override
public void onClick(View view) {

    if (view == button1) {
        camera();
    } else if (view == button2) {
        upload();
    }
}

public void getLocation() {
    try {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, (LocationListener) this);
    } catch (SecurityException e) {
        e.printStackTrace();
    }
}

@Override
public void onLocationChanged(Location location) {

    double lati = location.getLatitude();
    double longi = location.getLongitude();
    locationText.setText("Latitude: " + lati + "\n Longitude: " + longi);

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

    Toast.makeText(this, "Please Enable GPS and Internet", Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {
    AlertDialog.Builder alerDialogbuilder = new AlertDialog.Builder(Main3Activity.this);
    alerDialogbuilder.setTitle("Enable Gps to Continue");
    alerDialogbuilder.setIcon(R.drawable.ic_error_black_24dp);
    alerDialogbuilder.setMessage("If You Want To Enable Gps Go To Settings");
    alerDialogbuilder.setCancelable(false);
    alerDialogbuilder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            Intent intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent1);
            Toast.makeText(getApplicationContext(), "Enable Gps..", Toast.LENGTH_SHORT).show();
            finish();
        }
    });


    alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(getApplicationContext(), "Uploading Failed,Enable Gps", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Main3Activity.this, Main2Activity.class);
            startActivity(intent);
        }
    });
    AlertDialog alertDialog = alerDialogbuilder.create();
    alertDialog.show();
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...