I am an Android Developer

Friday, July 16, 2010

Phone a telephone number via intent in Android

//Present you with the dialler
Uri telUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_DIAL, telUri);

// Start the call
Uri callUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_CALL, callUri);

How to Make an Activity Fullscreen

public class FullScreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.main);
}
}

Get the phone's last known location using LocationManager

private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List providers = lm.getProviders(true);

/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;

for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}

double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}

A method to check presence of SD Card in Android device

public static boolean isSdCardPresent() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}

Internet Connection Check in Android Devices

Here is how you can check the internet connection in your Android testing device. This works for G1, Hero, Droid etc.

ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo netInfo = mgr.getActiveNetworkInfo();

if (netInfo != null) {
if (netInfo.isConnected()) {

// do something

} else {
AlertDialog.Builder alertbuilder = new AlertDialog.Builder(
MyActivity.this;

alertbuilder.setTitle("Internet");
alertbuilder.setMessage("Internet is not available");
alertbuilder.setNeutralButton("Ok", okClickListener);
//alertbuilder.setIcon(R.drawable.warning);
alertbuilder.show();
}
} else {
AlertDialog.Builder alertbuilder = new AlertDialog.Builder(
MyActivity.this);

alertbuilder.setTitle("Internet");
alertbuilder.setMessage("Internet is not available");
alertbuilder.setNeutralButton("Ok", okClickListener);

alertbuilder.show();
}

Getting dimension of Android device's screen

If you want the the display dimensions in pixels you can use:

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();