Monday, April 2, 2012

FieldChangeListener for Blackberry AutoCompleteField


Adding FieldChangeListener for Blackberry AutoCompleteField will not work because the Field itself will be using it to display the drop down list.

We have to override the FieldChangeListener. But if we just override FieldChangeListener we will not be able to see the dropdown. So we have to override the method and implement the parent method inside the overridden method. Below is the source code the of the same.

public class MyAutoCompleteField extends AutoCompleteField{
    public MyAutoCompleteField(BasicFilteredList filteredList)           
    {
       super(filteredList);
    }   
     public void fieldChanged(Field field, int context)
     {
         super.fieldChanged(field, context);
         Dialog.alert("text"); // Your code goes here
     }
}

Code above is self explanatory. I have created a custom AutoCompleteField called MyAutoCompleteField and have overridden the fieldChanged method. Usage of this is similar to AutoCompleteField.

Friday, December 10, 2010

Media Scanner : Android

What is Media Scanner?
MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider. The MediaScannerConnectionClient provides an interface for the media scanner service to return the Uri for a newly scanned file to the client of the MediaScannerConnection class.


In simple terms, when ever you create an image, record video or audio. Your program must update media scanner with details of the file, so that other applications like gallery, video player, media player etc. will be able to identify  the presence of the new file created.
Here is the code to update media scanner.



String name= /** NAME OF YOUR FILE **/
String path= /** YOUR FILE PATH **/
File k= new File(path, name);
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Video.Media.TITLE, name);
values.put(MediaStore.Video.Media.DATE_ADDED, (int)(current/1000));
// CHANGE THIS LINE TO SUIT THE MIME TYPE OF YOUR FILE
values.put(MediaStore.Video.Media.MIME_TYPE, "video/3gpp");
values.put(MediaStore.Video.Media.DATA,k.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base=MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,newUri ));
                               
------------------------------------------------------------------------------------------------------------------------------------

Tuesday, December 7, 2010

Programming Hardware Camera Button: Android

Here is how you can program for physical buttons on android devices.
An android device can have many buttons like camera, back, menu, search, home etc. Programming for all hardware buttons is more or less the same.
In this post let us see how we can program for Camera Button on the device.
This program will start an activity defined by us instead of the default camera preview.

1) Create a class that extends BroadcastReceiver and implement onReceive method. 

The code inside onReceive method will run whenever a broadcast message is received. In this case i have written a program to start an activity called myApp.

Whenever hardware camera button is clicked the default camera application is launched by the system. This may create a conflict and block your activity. E.g If you are creating your own camera application it may fail to launch because default camera application will be using all the resources. Moreover there might be other applications which are listening to the same broadcast. To prevent this call the function "abortBroadcast()", this will tell other programs that you are responding to this broadcast.
http://developer.android.com/reference/android/content/BroadcastReceiver.html#abortBroadcast()]


public class HDC extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
// Prevent other apps from launching
     abortBroadcast();
// Your Program
     Intent startActivity = new Intent();
        startActivity.setClass(context, myApp.class);
        startActivity.setAction(myApp.class.getName());
        startActivity.setFlags(
        Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(startActivity);
        }
    }
}

2) Add below lines to your android manifest file.

<receiver android:name=".HDC" >
    <intent-filter android:priority="10000">         
        <action android:name="android.intent.action.CAMERA_BUTTON" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>            
</receiver>


The above lines are added to your manifest file to tell the system that your program is ready to receive broadcast messages.
This line is added to receive an intimation when hardware button is clicked.
<action android:name="android.intent.action.CAMERA_BUTTON" />
HDC is the class created in step 1(do not forget the ".")
<receiver android:name=".HDC" >
The "abortBroadcast()" function is called to prevent other applications from responding to the broadcast. What if your application is the last one to receive the message? To prevent this some priority has to be set to make sure that your app receives it prior to any other program. To set priority add this line. Current priority is 10000 which is very high, you can change it according to your requirements.
<intent-filter android:priority="10000"> 

Below link will help you have better understanding of the program.
http://developer.android.com/reference/android/content/Intent.html
--------------------------------------------------------------------------------------------------------------