Posts

Showing posts from October, 2018

Android - Check if Location is Enabled

< uses-permission android :name= "android.permission.ACCESS_COARSE_LOCATION" /> public boolean checkLocation() { LocationManager lm = (LocationManager)getSystemService(Context. LOCATION_SERVICE ); boolean gps_enabled = false ; try { gps_enabled = lm.isProviderEnabled(LocationManager. GPS_PROVIDER ); } catch (Exception ex) {} if (gps_enabled) { return true ; } else { return false ; } }

Android - Check if the bluetooth is supported and enabled

public boolean isBluetoothEnabled() { BluetoothAdapter bAdapter = BluetoothAdapter. getDefaultAdapter (); // Device does not support Bluetooth return bAdapter != null && bAdapter.isEnabled(); }

Android - Check for Network Connectivity

public boolean checkInternet() { ConnectivityManager connectivityManager = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if(networkInfo != null) { if(networkInfo.isAvailable() && networkInfo.isConnected() || networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { Toast.makeText(getApplicationContext(), "Available", Toast.LENGTH_SHORT).show(); return true; } else { Toast.makeText(getApplicationContext(), "Not Available", Toast.LENGTH_SHORT).show(); return false; } } Toast.makeText(getApplicationContext(), "Not Available", Toast.LENGTH_SHORT).show(); return false; }

Android - Using KeyStore to encrypt and decrypt the data

import android.content.Context; import android.os.Build; import android.security.keystore.KeyGenParameterSpec; import android.security.keystore.KeyProperties; import android.support.annotation. RequiresApi ; import android.util.Base64; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.UnrecoverableKeyException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.GCMParameterSpec; public class KeyStoreUtil { private static final String FIXED_IV = "467096543987" ; KeyStore keyStore ; Contex...

Android - Starting a Timer using Handler

private final static int DELAY = 30000 ; private final Handler handler = new Handler(); private Timer timer ; private void launchTimer() { TimerTask timerTask = new TimerTask() { @Override public void run() { handler .post( new Runnable() { public void run() { // TODO // write the code to be executed after timer ends timer .cancel(); } }); } }; timer = new Timer(); timer .schedule(timerTask, DELAY ); }

Android - Disable Screen Capture

protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // To disable user from taking the screen shot of the activity getWindow().setFlags(WindowManager.LayoutParams. FLAG_SECURE , WindowManager.LayoutParams. FLAG_SECURE ); }

Android - Check for Fingerprint Hardware

public int checkFingerPrintHardware() { int status = 0; // Check if we're running on Android 6.0 (M) or higher if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //Fingerprint API only available on from Android 6.0 (M) // FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE); if (!fingerprintManager.isHardwareDetected()) { // Device doesn't support fingerprint authentication // status = 0; } else if (!fingerprintManager.hasEnrolledFingerprints()) { // User hasn't enrolled any fingerprints to authenticate with // status = 1; } else { status = 2; // Everything is ready for fingerprint authentication } return status; } }

Android - Check for NFC Hardware Availability

public boolean checkNFCHardware() { NfcManager nfcManager = (NfcManager) getSystemService(Context. NFC_SERVICE ); NfcAdapter nfcAdapter = nfcManager.getDefaultAdapter(); if (nfcAdapter == null ) { // Device not compatible for NFC support return false ; } else if (!nfcAdapter.isEnabled()) { // If NFC not enabled, application can request Settings UI allowing the user to toggle/enable NFC from startActivity( new Intent(android.provider.Settings. ACTION_WIRELESS_SETTINGS )); return false ; } else { return true ; } }