Monday, August 13, 2012

Android Google Map Integration

Below are the steps for Integrating google map into your android application

1.Getting the Google Api(Libraries)

In Eclipse Goto Window->Android SDK Manager , then select Google Api's(if not installed) then select Install packeges . 
This will give you the jar files required to integrate Google map

2.Obtaining a Maps API Key


Beginning with the Android SDK release v1.0, you need to apply for a free Google Maps API key before you can integrate Google Maps into your Android application. To apply for a key, you need to follow the series of steps outlined below. You can also refer to Google's detailed documentation on the process at http://code.google.com/android/toolbox/apis/mapkey.html
First, if you are testing the application on the Android emulator, locate the SDK debug certificate located in the default folder of "C:\Documents and Settings\<username>\Local Settings\Application Data\Android". The filename of the debug keystore is debug.keystore. For deploying to a real Android device, substitute the debug.keystore file with your own keystore file. In a future article I will discuss how you can generate your own keystore file.
For simplicity, copy this file (debug.keystore) to a folder in C:\ (for example, create a folder called "C:\Android").
Using the debug keystore, you need to extract its MD5 fingerprint using the Keytool.exe application included with your JDK installation. This fingerprint is needed to apply for the free Google Maps key. You can usually find the Keytool.exefrom the "C:\Program Files\Java\<JDK_version_number>\bin" folder.
Issue the following command to extract the MD5 fingerprint.
keytool.exe -list -alias androiddebugkey -keystore "C:\android\debug.keystore" -storepass android -keypass android

3.Include jar Files into your Project for maps

      Right click on your project and go to build path and then add external archives.
      Locate your sdk.  
      android-sdk-windows\add-ons\addon_google_apis_google_inc_8\libs\maps

4.Displaying the Map
Below are the code for integrating Google Map into android app

Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.learn2develop.GoogleMaps"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
 
    <uses-library android:name="com.google.android.maps" />  
 
        <activity android:name=".MapsActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
    <uses-permission android:name="android.permission.INTERNET" />
 
</manifest>
</xml>


Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
 
    <com.google.android.maps.MapView 
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="0l4sCTTyRmXTNo7k8DREHvEaLar2UmHGwnhZVHQ"
        />
 
</RelativeLayout>

MapsActivityDemo.java (your Activity class)
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
 
public class MapsActivityDemo extends MapActivity 
{    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
 
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

Thursday, April 19, 2012

Sending SMS programatically

There are two ways to send sms in android

1.Using smsManager
2.Using Intent

1.Using smsManager
private void sendSMS(String phoneNumber, String message)
    {        
        PendingIntent pi = PendingIntent.getActivity(this, 0,
 new Intent(this, SMS.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    } 
 Add this permission in manifest file 
<uses-permission android:name="android.permission.SEND_SMS">
 
2.Using Intent 
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
       sendIntent.putExtra("sms_body", "Content of the SMS goes here..."); 
       sendIntent.setType("vnd.android-dir/mms-sms");
       startActivity(sendIntent); 
 
 
You can get more details from the following site
http://mobiforge.com/developing/story/sms-messaging-android
   
 

Tuesday, April 17, 2012

Content Provider and Content Resolver

A ContentProvider is a generic mechanism for an application to provide
access to data. This is done by subclassing ContentProvider,
implementing certain methods, and publishing information in the manifest.

To access data provided by ContentProvider(s), you use ContentResolver.
There is no need to subclass.

http://developer.android.com/guide/topics/providers/content-providers...

This mechanism is used in certain Google applications.

Some of those, but not all, are considered to be part of the SDK, and
can be used by other applications as well:

http://developer.android.com/reference/android/provider/package-summa...

Some others are internal, and should not be used by other applications.

These internal providers can change at any time: with a new Android
release, a new version of a Google application, or even when Android is
customized by a device manufacturer.

These include Calendar and GMail, and perhaps more. It's not a good idea
to use them in your application.

If you do, and make it work somehow, be aware that your code can break
at any time.

Friday, April 13, 2012

Calling finish() on an Android activity doesn't actually finish

You should put a return statement after that finish, because the method that called finish will be executed completely otherwise.
 
The method that called finish() will run to completion. The finish() operation will not even begin until you return control to Android.

suppose you have a method xyz like below

xyz()
{
statment1
statement2
.
.
finish();
statement6
statement7
}

Then the finish() function will be called after statement7 is executed and the control has returned to android.

So , if you want your app to finish() immediately then put a return statement after finish(), Like below
xyz()
{
statment1
statement2
.
.
finish();
return;
statement6
statement7

}

Statement6 and statement7 will not be executed any more

Friday, March 30, 2012

java.lang.OutOfMemoryError: bitmap size exceeds VM budget - Android

Avoiding Memory Leaks


http://developer.android.com/resources/articles/avoiding-memory-leaks.html

http://stackoverflow.com/questions/1949066/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android


Thursday, March 29, 2012

ListView whose id attribute is 'android.R.id.list

The solution to this problem is


<ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

the id should be "list" and it should be like "@android:id/list" not "@+id/list"

Monday, March 19, 2012

setSelection on Spinner based on rowId


http://stackoverflow.com/questions/2559611/setselection-on-spinner-based-on-rowid

If you want to set the selection of a Spinner thats backed by a CursorAdapter, you can loop through all the items in the Cursor and look for the one you want (assuming that the primary key in your table is named "_id"):
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new SimpleCursorAdapter(...));
for (int i = 0; i < spinner.getCount(); i++) {
    Cursor value = (Cursor) spinner.getItemAtPosition(i);
    long id = value.getLong(value.getColumnIndex("_id");
    if (id == rowid) {
        spinner.setSelection(i);
    }
}
If you want to get the rowid of a selected item, you can do something similar:
Cursor cursor = (Cursor) spinner.getSelectedItem();
long rowid = cursor.getLong(cursor.getColumnIndex("_id"));


Array Adapters

http://stackoverflow.com/questions/2390102/how-to-set-selected-item-of-spinner-by-value-not-by-position


You have to access the ArrayAdapter directly: Suppose your Spinner is named mySpinner, and it contains as one of its choices: "some value". To find the position of "some value" in the Spinner use this:
String myString = "some value"; //the value you want the position for
ArrayAdapter myAdap = (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapter
int spinnerPosition = myAdap.getPosition(myString);
//set the default according to value
mySpinner.setSelection(spinnerPosition);

How To Build A Dashboard User Interface In Android

How to check internet connectivity in android


 public boolean isConnectedToNet() {
   ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getActiveNetworkInfo();
   if (netInfo != null && netInfo.isConnectedOrConnecting()) {
       return true;
   }
   return false;
    }

How to check internet connectivity in android


 public boolean isConnectedToNet() {
   ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getActiveNetworkInfo();
   if (netInfo != null && netInfo.isConnectedOrConnecting()) {
       return true;
   }
   return false;
    }
manifest

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Regenerating R.java

In Eclipse, under the Project menu, is an option build automatically. That would help you build the R.java file everytime modifications are made. The Clean... option is also there under Project.




http://blog.burnayev.com/2009/11/android-developer-tip-regenerating.html

 I've just spent half an hour trying to fathom how to get back my auto-generated R.java that magically disappeared at some point.

The usual ways of resolving issues such as clean build, restarting IDE, and wiping the screen clean with a damp cloth didn't work.

Googling around did reveal a lot of similar complaints and revived a stark deja vu feeling. The hilarious thing is that once the R guy is gone, a good chunk of your code goes red thus adding to the insult.

Rather than continuing the discovery of all the conceivable strains of the issue found in the wild, I decided to leverage the tried-and-true approach of staring at the code. Having done so for a while I spotted a few red guys under res folder. They were a leftover of my current redesign work that I was about to delete before things went awry. Sure enough, as soon as I hit Del on them the darn R thing automagically reappeared.

The moral of the story is there's just so much magic Google can do for us. Bad resources (e.g. layouts with errors) are not treated gracefully by Google Android Eclipse plugin and can deceive it into deleting R.java with no sound reason.

Tuesday, January 3, 2012

Android Back Stack


Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);