Skip to main content

Getting Latitude & Longitude while launching SplashScreen

public class Splash extends Activity {

    private static final int SPLASH_DURATION = 7000;
    private static final int PERMISSION_REQUEST_CODE = 1;
    private static final int CPERMISSION_REQUEST_CODE = 2;
    public static double lng;
    public static double lat;
    Context context;
    SharedPreferences pref;
    private boolean mIsBackButtonPressed;
    private Handler myhandler;
    private LocationManager mLocationManager = null;
    private String provider = null;
    private Marker mCurrentPosition = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        context = this;

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


        if (checkPermission_GPS() == false) requestPermissionCurrLocation();
        else if (!callCheckPermissionCurrLocation()) requestPermission_GPS();

        if (isProviderAvailable() && (provider != null)) {
            locateCurrentPosition();
        }

        myhandler = new Handler();
        myhandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
                if (!mIsBackButtonPressed) {
                    Intent intent = new Intent(Splash.this, LogInActivity.class);
                    Splash.this.startActivity(intent);
                }
            }

        }, SPLASH_DURATION);

    }

    private boolean callCheckPermissionCurrLocation() {
        int result = ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION);
        if (result == PackageManager.PERMISSION_GRANTED)
            return true;
        else
            return false;
    }

    private boolean checkPermission_GPS() {
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION);
        if (result == PackageManager.PERMISSION_GRANTED)
            return true;
        else
            return false;
    }

    private void requestPermissionCurrLocation() {
        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_CODE);
    }

    private void requestPermission_GPS() {
        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, CPERMISSION_REQUEST_CODE);
    }

    private void locateCurrentPosition() {

        int status = context.getPackageManager().checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION,
                context.getPackageName());

        if (status == PackageManager.PERMISSION_GRANTED) {

            Location location = mLocationManager.getLastKnownLocation(provider);
            updateWithNewLocation(location);
        }
    }

    private void updateWithNewLocation(Location location) {

        if (location != null && provider != null) {

            lng = location.getLongitude();
            lat = location.getLatitude();
            Log.e("Long & Lat", String.valueOf(lng) + "  " + lat);
        }
    }

    private boolean isProviderAvailable() {

        mLocationManager = (LocationManager) context.getSystemService(
                Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        provider = mLocationManager.getBestProvider(criteria, true);
        if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            provider = LocationManager.NETWORK_PROVIDER;

            return true;
        }

        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            provider = LocationManager.GPS_PROVIDER;
            return true;
        }

        if (provider != null) {
            return true;
        } else {
            Toast.makeText(context, "Please enable GPS in your mobile", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        mIsBackButtonPressed = true;
        super.onBackPressed();
    }
}


activity_splash.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#F0F0F0">

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/splash_screen" />

</RelativeLayout>

Use PERMISSIONS IN AndroidManifest.xml


Comments

Popular posts from this blog

SearchView using CursorAdapter

Here  MYFRAGMENT is nothing but name of  a Fragment @Override public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {     inflater.inflate(R.menu.popup, menu);     final SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);     final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();     searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));     searchView.setSubmitButtonEnabled(false);     searchView.setIconifiedByDefault(true);     //       searchView.setOnCloseListener(this);     SearchView. OnQueryTextListener textChangeListener = new SearchView. OnQueryTextListener () {         @Override         public boolean onQueryTextSubmit(String query) {             cartAdapt...

Fragment

To enable/disable menu items in Different fragments in same Activity: @Override public void onCreate(@Nullable Bundle savedInstanceState)  {     super.onCreate(savedInstanceState);     setHasOptionsMenu(true);  } @Override public void onPrepareOptionsMenu(Menu menu) {     //Disable the SearchView which is enabled in Product Chart     menu.findItem(R.id.action_search).setVisible(false);   // true to enable } ====================================================

To make a ListView to be scrolled inside of ScrollView

To make a ListView to be scrolled inside of ScrollView: lv . setOnTouchListener ( new ListView . OnTouchListener () { @Override public boolean onTouch ( View v , MotionEvent event ) { int action = event . getAction (); switch ( action ) { case MotionEvent . ACTION_DOWN : // Disallow ScrollView to intercept touch events. v . getParent (). requestDisallowInterceptTouchEvent ( true ); break ; case MotionEvent . ACTION_UP : // Allow ScrollView to intercept touch events. v . getParent (). requestDisallowInterceptTouchEvent ( false ); break ; } // Handle ListView touch events. v . onTouchEvent ( event ); return true ; } }); Refer: http://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android