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
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
Post a Comment