Wednesday, October 28, 2009
Android 2.0 !! Overview
Android 2.0 !!! Where is Google Maps
Just saw Android 2.0 (Eclairs) and downloaded it. Started the emulator, and compiled my project with 2.0 settings. I was surprised to see red marks all over the place. After looking at the console, I figured out that Android 2.0 doesn't have in-built Maps API. If your application uses the Google Maps API, then you are in for a surprise. Read on.
Now what?
In the emulator too, I didn't find the Google Maps application. Damn. But, just to test if my other functionalities were intact with the new Android 2.0, I added the maps.jar avaialble in the Android SDK as an external jar.
I thought this would solve the problem. All compile errors vanished. But when I tried to install my app, the console said,
"Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY"
To avoid this installation error, you have to remove this line from the manifest file so that Android detects the maps.jar as an external API and not an internal one.
Wednesday, October 21, 2009
Orientation independent applications
The Android phones detect orientation changes when you tilt your
phone from landscape to portrait or vice-versa. It’s a cute feature. Well, most of the time you would be using your phone in the portrait mode. But for the phones that have a full QWERTY keyboard, the orientation automatically changes to landscape mode when you slide out your keyboard. If your application is not designed to handle such orientation changes, the usability of your application might decrease to an extent at which it might get completely unusable, or at the least, very inconvenient for the user.
How to handle it? It is actually very simple. You would have put all your layout xml files in a folder called “res/layout”. This is the default location from where the layout files are picked up, i.e , when your phone is in portrait mode. You just need to create one more folder “res/layout-land” where you put you xml files, with the same name, but with a different layout, which would be convenient for the user to view when in portrait mode. This is all you have to do.
Next time, when the orientation is changed, your layout files will be picked up from the layout-land folder, and will be applied to your UI.
Enjoy….
Monday, October 5, 2009
Gesture Detection on Android
I just got my gesture detection code working. It's a fun way and a convenient way as well to enhance the usability of your application. So, here is the code that gets you going. First, for the Activity that you want to detect gestures, the OnGestureListener interface has to be implemented. Then, you need to override the methods and implement them. And finally, you need to implement the onTouchEvent method of the activity that will trigger the gesture listener code.
1: package com.beanie.androidco;
2:
3: import android.app.Activity;
4: import android.os.Bundle;
5: import android.view.GestureDetector;
6: import android.view.MotionEvent;
7: import android.view.GestureDetector.OnGestureListener;
8:
9: public class GestureDemo extends Activity implements OnGestureListener {
10: private GestureDetector gestureDetector;
11: @Override
12: public void onCreate(Bundle savedInstanceState) {
13: super.onCreate(savedInstanceState);
14: setContentView(R.layout.main);
15: gestureDetector = new GestureDetector(this);
16: }
17:
18: @Override
19: public boolean onTouchEvent(MotionEvent event) {
20: return gestureDetector.onTouchEvent(event);
21: }
22:
23: @Override
24: public boolean onDown(MotionEvent e) {
25: // Do something
26: return false;
27: }
28:
29: @Override
30: public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
31: float velocityY) {
32: // Do something
33: return false;
34: }
35:
36: @Override
37: public void onLongPress(MotionEvent e) {
38: // Do something
39: }
40:
41: @Override
42: public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
43: float distanceY) {
44: // Do something
45: return false;
46: }
47:
48: @Override
49: public void onShowPress(MotionEvent e) {
50: // Do something
51: }
52:
53: @Override
54: public boolean onSingleTapUp(MotionEvent e) {
55: // Do something
56: return false;
57: }
58: }
The most commonly used method would probably be the onFling method, where you have to check for a threshold velocity and displacement before you would want to trigger an action. For detecting Left Fling, Right Fling, Scroll down/up, you will need to write some extra code in the corresponding methods. Enjoy...
Monday, September 7, 2009
SMS Blocking in Android
Recently, I tried to develop an app that would block sms from certain numbers that you can choose. It seems that you cannot completely block the incoming sms. These were the problems that I faced while working with this app.1. There are no APIs supplied, that handles the SMS inbox (I mean, no content providersm which can be directly queried to fetch records from the sms table). However, you have a workaround of finding the tables where the sms is stored, and then querying and updating that table. Well, there is actually a content provider, that exposes this table to other applications, but it is not documented anywhere. So, I have to hardcode the column values. This is potentially dangerous, if in the next Donut release, Google changes the column names, my application would fall apart.
2. On receiving of an SMS, I have 2 things to do. Remove the SMS from the sms table, and disable the notification. Deleting the sms from the table was pretty easy, but I couldn't find a way to remove the Notification. Perhaps, there is no way that we can disable this notification. One workaround might be, to temporarily change the Notification settings, the moment you sense an incoming message from a blocked number. And, after you delete the message from the sms table, you just turn on the notification. I haven't worked on this, but this might just work.
3. If the Messaging application is already open, and an sms arrives, weird things start to happen. I can see the blocked sms being deleted, and the list of sms gets updated. But the next message in the list is marked as "Unread". Probably, this is done by the Notification, I don't know.
4. SMS broadcast, is definitely, an un-ordered one. So, the easiest way, that could have worked, would have been just to abort the broadcast. But in this case, you don't have this option. so, once you recieve this broadcast, you have to manually go and delete the message, by matching the incoming number, timestamp etc. Timestamp method isn't foolproof. Because, the timestamp you receive, is the timestamp of the broadcast, and not the time when the sms is written into the DB. So they wont match. You should pick up the incoming number, and check for the timestamp within a range of about 1 seconds. This should give you the sms that you need to delete.
5. There's another problem. When you recieve the broadcast, the system wont write the sms into the table, before it is sure that all the broadcast receivers have done their work. So, it waits for some time, and then writes the sms into the table. Before this, if you try to delete the sms, obviously, you would not find it in the table. So, make your code sleep for about a second, and then go and delete the SMS. That should do it. But, the most important problem still persists. THE NOTIFICATION. I guess, it can't be removed.