[Android] Curious issue about notifications

It may seem obvious, but this issue annoyed me very much and made me waste some valious hours.

As scenario, imagine that you launch ActivityA, and after clicking a button, you launch ActivityB on top of it.

Now, ActivityB executes a long-time background operation (such as parsing a JSON file), and we want a Notification to tell us the progress.

The author of the Notification could be ActivityA, so we can keep on using the App, and even click “back”, and we will still be informed about the progress of our operation.

Well, just one detail when doing that: Don’t forget to set the Notification’s Small Icon, or else it won’t show up (It will if we launch it from ActivityB without smallIcon, but in case of ActivityA, it won’t). Here is some code:

Intent i = getIntent();
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int notificationId = 100;
PendingIntent pi = PendingIntent.getActivity(ActivityA.this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
mNotificationBuilder.setContentTitle(getString("Parsing"))
.setContentText("10% parsed")
.setProgress(100, 10, false)
.setContentIntent(pi)
.setSmallIcon(R.drawable.icon); // Do not forget this line!!
mNotifyManager.notify(notificationId, mNotificationBuilder.build());

I know it is quite silly, but remember it! ūüôā

[Android] Eclipse shortcuts i use most

It is worth to spend some time reading about the shortcuts eclipse offers you to save switches / context-changes and become much more productive when coding. One of the most useful tutorials is this one from vogella. After a few years coding, the ones i consider most useful are these: (yellow ones are the best time savers for me)

ctrl+3: quick access to resources
ctrl+2: quick assist – rename/assign tools (renaming vars, assigning operations to local vars)
ctrl+1: quick fix (see my strings resources tutorial)
ctrl+I: automatic code indentation
ctrl+shift+O: fix imports
alt+left: go to previous location
alt+right: go to next location
ctrl+right: move to next text element
ctrl+left: move to previous text element
ctrl+alt+down: clone selected line below (next line)
ctrl+alt+up: clone current line above (previous line)
alt+up: move current line up
alt+down: move current line down
ctrl+D: delete current line
ctrl+DEL: delete next element
ctrl+backspace: delete previous element
shift+enter: adds a new line below current, without deleting or overwriting selection
ctrl+Q: back to the last edited position

And of course, the most traditional and obvious ones:

ctrl+F: find
ctrl+X: cut
ctrl+C: copy
ctrl+V: paste
ctrl+Z: undo
ctrl+Y: redo

I suggest you to correct some bad customs and get used to these shortcuts, and you can focus more on coding and less in interruptions/switches/context changes that often act as obstacles.

Have a nice coding day! ūüôā

Another time-saver! Creating String resources directly from Code editor

Write a string literal in your code like

mButton.setText("Text to add as String resource");

Then select the string literal  like this:

 1-select-text

and press ALT+SHIFT+A, a yellow menu will appear in the bottom-right corner,

2-bottom-menu

double click on “Extract Android String” , this menu will appear:

2.-add-str-res

choose a proper name for the String resource and click Ok.

You will get a new strings.xml entry:

   <string name="my_string_resource">Text to add as string resource</string>

And your Button’s setText line replaced by:

   mButton.setText(R.string.my_string_resource);

Time-saver! creating Android string resources directly from code.

Let’s say you copy and paste an XML TextView:

 

<TextView 
  android:id="@+id/whatever"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/copy_pasted_string"/>

 

You probably won’t have the “copy_pasted_string” defined in the strings.xml file, and creating it manually is slow.

On Eclipse, place the mouse over the “copy_pasted_string” text, press CTRL+1 then click “Rename Android resource”.

It will create a new strings.xml entry, plus a new R.string resource in the R.java file.

Saved me thousands of seconds of routinary work!

 

OsmDroid + Mobile Atlas Creator tutorial

As you may know, Mobile Atlas Creator (from now on, MOBAC) was blocked due to producing high stress on OSM servers, since mid-2013.

So MOBAC can’t generate Mapnik tiles now, and most of the OsmDroid Apps use Mapnik.

What we’re gonna do in this tutorial is to use MapQuest tiles, the ones that MOBAC actually support, instead of Mapnik tiles.

In MOBAC:

Select “Atlas > New Atlas”

Select the “OSMDroid ZIP” option

Now on the atlas, select “Map Source: OpenStreetMaps mapquest”

mapsource

Mark the “Recreate/adjust map tiles (CPU intensive)” checkbox

tileformat

Choose “Tile format: PNG”. To make sure this is correctly done, select another one (like JPEG) and then switch back to PNG.

Drag the area you wish to select and mark the Zoom Levels you want to include in your atlas

dragdrop

Click on atlas name (mine is “Unnamed atlas”), then click on Add Selection. A new layer must be created.

addselection

Then on the upper menu, click Atlas > Create atlas

Now you’ll find a .zip file inside your MOBAC folder. Extract it into a temporary folder.

Rename the “MapQuest” folder inside the zip to “MapquestOSM”, and zip it again (pay attention to the caps). Don’t worry about the name of the zip. “MapquestOSM.zip” would be OK, but its not strictly necessary.

copy the new zip file to /sdcard/osmdroid/ (not inside /sdcard/osmdroid/tiles!, just osmdroid folder)

Android code to load the atlas:

public class MyMapActivityBasic extends Activity
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   MapView mapView = new MapView(this, 256); //constructor
   mapView.setClickable(true);
   mapView.setBuiltInZoomControls(true);
   setContentView(mapView); //displaying the MapView
   mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
   mapView.getController().setZoom(15); //set initial zoom-level, depends on your need
   mapView.getController().setCenter(new GeoPoint(mLat, mLon));
   mapView.setUseDataConnection(true); 
   mapView.setMultiTouchControls(true);
 }
}

Android: INSTALL_FAILED_INVALID_URI

When installing Apps in my Android phone using ADB (via Eclipse internally, or via command line tool), i’ve found the following error:

1914 KB/s (4580896 bytes in 2.337s)
pkg: /data/local/tmp/package.apk

Failure [INSTALL_FAILED_INVALID_URI]

It seems to appear mostly on rooted phones (mine is). The solution is to change permissions to directory

/data/local

from rwxrwxr– to rwxrwxrwx.

This should do the trick ūüôā at least i’m able to install APK’s from now on.

Hope it helps!