Thursday, September 11, 2014

Set the initial position in the spinner instead of the default initial position.(Android)


In your Activities onCreate() method do the following.

1. Get the spinner object
     Ex:
     Spinner obj =  findViewById(R.id.spinner);
2. Add listener to it
     obj.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
                      //TODO      
                }
      });

3. obj.setSelection(yourInitialPosition,false);
NOTE1: By calling this above method, Android make sure that the onItemSelected is not called 2 times. It will be called only once after the activity launches first time.

NOTE2: If we don't call the setSelection(int,boolean); at all then by default android calls onItemSelected with default position as 0.

NOTE3: if we call set setSelection(int) instead, then onItemSelected will be getting called twice.

public void setSelection (int position, boolean animate)

    Jump directly to a specific item in the adapter data.

Saturday, July 26, 2014

Android DownloadManager's broadcast is received two times for the same download id

                                     
Hi,

    Here I am going to explain the temporary fix for the mentioned problem with Android DownloadManager
 
My Observations:

1.  Android DownloadManager is sending broadcast more than one time for the same download id. First time if we receive STATUS_SUCCESSFUL, second time we may receive STATUS_FAILURE for the same download id. Whenever the DownloadManager sends broadcast with the uri as null we need to return from the onReceive() as shown below. This way we can solve this issue.

2. Generally DownloadManager deletes the semi-downloaded files if they were failed to download further.
    Some times it is found that DownloadManager is deleting the successfully downloaded files as well. So
    to come out of the problem copy the downloaded file to another location as shown below.

Temporay solutions:
1. In your application BroadcastReceiver's class onReceive method, do the following

      public class DownloadReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action != null) {
                if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    DownloadManager downloadMgr = (DownloadManager) context
                            .getSystemService(context.DOWNLOAD_SERVICE);
                    Uri uri = downloadMgr.getUriForDownloadedFile(downloadId);
                    if(uri != null) {
                              //Do your stuff here
                              Query query = new Query();
                              query.setFilterById(downloadId);
                              Cursor cursor = downloadMgr.query(query);
                              if(cursor != null && cursor.moveToFirst()) {
                                       //Get the status of the download
                                      int status = cursor
                                    .getInt(cursor
                                            .getColumnIndex(DownloadManager.COLUMN_STATUS));
                                      //To Check how many bytes download
                                      int bytes = cursor
                                    .getInt(cursor
                                            .getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                                      //Gives the downloaded file location.
                                      String downloadedLocation = cursor
                                    .getString(cursor
                                            .getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
                                      //Copy the downloaded file to  another location
                                      FileUtil.copy(downloadedLocation, <target_location_path);
                              }
                    }
                 }
             }
           }
      }
 



Run a C/C++ Program in windows Eclipse IDE

1. Install CDT plugin which suits your eclipse release.
2. Download mingw or cygwin (mingw suggested).
3. Right click  Properties -> C/C++ Build -> Tool chain editor -> Current toolchain (select MinGW GCC)
4. Set the Environment path variables
         Right click Properties -> C/C++ Build -> Environment -> add PATH variable and value is <path to
         bin of mingw>  For ex: PATH : C:\mingw\bin
5. Set the make command
        Right click Properties -> C/C++ Build -> Settings -> Tool chain (tab) -> GCC C Compiler
            clear the existing command and add mingw32-gcc
        Also select MinGw Linker and select the command , replace with mingw32-gcc
6. Binary parser
      Right click Properties -> C/C++ Build -> Settings -> Binary Parsers (tab)
            check PE Windows Parser.
7. Cttl + B to build ( or you can click on hammer icon 
8.  Sample code
     #include <stdio.h>

int main() {

float x = 0.5;
if(x == 0.5) {
printf("IF");
} else if(x == 0.5f) {
printf("ELSE IF");
} else {
printf("ELSE");
}
return 0;
}

9. Run it
10. Result is 'IF'

     Happy coding.