Posts

Showing posts from March, 2010

Creating a MediaPlayer in a Service

Create a class which extends Service, in that in onCreate(), create the mediaPlayer and if we need, we can provide a notification also, to notify the user that a background service is being activated.

In onStart(), start(), the mediaplayer also, set looping equals true. So that the mediaplayer will play continously

The complete code is


public class AudioService extends Service {

Mediaplayer player;
public void onCreate() {
player = MediaPlayer.create(this, R.raw.mj);
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
nOTIFICATION NOTIFICATION = new Notification(0, "Player started", System.currentTimeMillis());
nm.notify(0, notification);
try{
player.prepare();
} catch(Exception e){
}
}

public void onStart(Intent intent, int startId) {
player.start();
player.setLooping(true);
}

public IBinder onBind(Intent intent) {
return null;
}
}

Updating List in Android

If we need to update list in android, then according to adapter, we need to modify the code.


For ListAdapter, after updating the arrayList, we need to call notifyDataSetChanged().
For CursorAdapter, after updating the cursor, we need to call requery().

Handling extensive operations in UI thread

If we need to do some extensive operations in UI thread, then create a new thread and do in that thread, so that in UI thread, we can show something else, like progressBar or manually create an animation and can show.

Creating two different apk from a single Application

In android, in some cases, we need to create two different apk from a single application. In that case, just change the package name in manifest file. Then two different apk will get created.

Rotating a view through RotateAnimation in Android

For rotating a view,


RotateAniamtion rAnim = new RotateAnimation(0,360, w/2, h/2);
rAnim.setInterpolator(new LinearInterpolator());
rAnim.setDuration(900L);
rAnim.setStartTime(200L);


List of Interpolators in Android are as follows

1. ACCELERATE_DECELERATE_INTERPOLATOR
2. ACCELERATE_INTERPOLATOR
3. DECELERATE_INTERPOLATOR
4. ANTICIPATE_INTERPOLATOR
5. BOUNCE_INTERPOLATOR
6. ANTICIPATE_OVERSHOOT_INTERPOLATOR
7. FADE_IN_INTERPOLATOR
8. FADE_OUT_INTERPOLATOR
9. LINEAR_INTERPOLATOR
10. OVERSHOOT_INTERPOLATOR
11. SLIDE_IN_LEFT_INTERPOLATOR
12. SLIDE_OUT_RIGHT_INTERPOLATOR

Checking Network Connection In Android

For checking connection status in Android, first we need to get an instance of ConnectionManager using getSystemService(). The syntax is as,


ConnectionManager connManager = (ConnectionManager)getSystemService(Context. CONNECTIVITY_SERVICE);

if ( connManager .getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED || connManager .getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ) {
text.setText("hey your online!!!");
//Do something in here when we are connected
} else if ( connManager.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
text.setText("Look your not online");
}

Getting Current location from GPS in Android

We can get the current location from Android phones using

- GPS provider
- Network Provider

For this, first create an instance of location manager.

Create a class which implements the LocationListener

LocationManager locationManager = (LocationManager) getSystemService(Context. LOCATION_SERVICE);

Then in the manifest we need to give the permission as,


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

For GPS provider, we will use this statement,


locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

and for getting information from network provider, that means , we want to get the current location when inside a building or where, GPS dont work network is there, then we can use this statement for getting location information as,


locationManager.reque…

Enabling and disabling lock Screen in Android

For enabling or disabling lock screen in Android, we need to get the instance of keyBoard Manager, using getSystemService(). The syntax is as,


KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);


For locking the screen use,

lock.reenableKeyguard();


and for disabling the lock use,

lock.disableKeyguard();


This will just hide the lock screen and will display if any incoming call or any such event is happening, it will display that screen. It wont display the lock screen.

When running the application, to disable from going to the lock state, just use
setKeepScreenOn() is set to true. or use the xml attribute android:keepScreenOn="true"

Another way to prevent the device to go to the sleep or lock mode when the application is running is set this api to true - setKeepScreenOn()

And of course we need to give permission android.permission.DISABLE_KEYGUARD

Map in Android

Earlier when the first SDK of androd released, google map is a part of SDK. Now, its not coming with SDK, its coming as add-on.

First for for displaying google map for android application, we need Google map API Key. For this, using the keytool in the JDK, we need to create the finger-print.

We will get the keyStore from eclipse. The path is


Window->Preferences->Android->Build


I can show a a sample command for getting the finger print in windows xp is,


C:\Program Files\Java\jre1.5.0_06\bin>keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Acer\.android\debug.keystore" -storepass android -keypass android



Type this command in dos prompt and you will get the finger print. Then using this finger-print, we need to get the map api key from the url

Sign Up


After getting the API key, use this API key to display map. Then at the time of creating Android application, we need to select the target as Google API 4 or whichever version we need. It is importa…

Handler In Android

Handler is a verry useful and powerful component in Android. Some special features of Handler are,



->If handler is created without any parameter, then it will be created in the same thread itself.

->We can pass data to handler using message



I created one application, which is using handler efficiently. In which, an image is blinking for one second.

So if we need to create a seperate thread for handler, first we need to create a thread and create the handler inside it.

Reasons for getting java.lang.VerifyError

"java.lang.VerifyError" are worst. VerifyError means that the class file contains bytecode that is syntactically correct but violates some semantic restriction e.g. a jump target that crosses method boundaries.


Some of the reasons for getting VerifyError are,


-> If the bytecode size of your method exceeds the 64kb limit

-> When the class file gets corrupted in some other way (e.g. through faulty RAM or a failing HD).