Today i was studying a lot about Task affinities and Back Stack and trying to understand them clearly as i am running into a situation where i launched an activity two times. Look at this scenario i have a menu with five options say A , B , C , D and E which are different activities meant for different purposes. I launched B activity then A activity then C activity and again A activity and i pressed back key now A activity is finished C is displayed again i pressed back key as per my imagination it should display B activity but i was surprised to see A activity.
B -> A -> C -> A on pressing back key it should be A -> C-> B
It seems that it maintains the back stack without reordering and i came across an useful flag in Intents class FLAG_ACTIVITY_REORDER_TO_FRONT which will solve this kind of issues when intent resolves to the activity in the current task it moves this activity to the top of the stack in this way.
B->A->C->A will be reordered as B->C->A
So the code will be as follows
Intent intent = new Intent();
intent.setClass(...., ....);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Happy coding .....
Wednesday, August 10, 2011
Monday, August 8, 2011
How to save images from img tags and css from stylesheet tags using Regular Expression in Android?
Regular Expressions are the best way to use for search a particular pattern using special syntax. Even though it looks complex to understand it simplifies searching and replacing in text processing and parsing the text. Most of the editors commonly use regex for searching and replacing so its usage is common across any programming languages. I used it for html parsing for an Android applications mainly img tags and css tags which i need to store it in local file system for offline usage.
The following regex pattern is used to retrieve the css path from the html page
".*?(\"text\\/css\").*?((?:\\/[\\w\\.\\-]+)+)" So the code snippet will be like this
Pattern p = Pattern.compile(regex);
Matcher m = p.match(fileName) //fileName is the document we want to parse;
if(m.find())
{
String word = m.group(1); // this gives the exact word ex: text/css
String path =m.group(2); // this gives the relative path like /css/xyz.css
}
Many people suggest to use parses like jsoup neckohtml etc which are under GPL but if the requirement is just parsing few search terms it will be easier to use Regex since we dont need to include external jar files.
Happy coding........
The following regex pattern is used to retrieve the css path from the html page
".*?(\"text\\/css\").*?((?:\\/[\\w\\.\\-]+)+)" So the code snippet will be like this
Pattern p = Pattern.compile(regex);
Matcher m = p.match(fileName) //fileName is the document we want to parse;
if(m.find())
{
String word = m.group(1); // this gives the exact word ex: text/css
String path =m.group(2); // this gives the relative path like /css/xyz.css
}
Many people suggest to use parses like jsoup neckohtml etc which are under GPL but if the requirement is just parsing few search terms it will be easier to use Regex since we dont need to include external jar files.
Happy coding........
Sunday, July 31, 2011
How to enable telnet on windows 7 for various android commands
I am currently developing some applications on Location Based services for Android devices. Most of the time i need to use emulator for testing the applications and i need to connect to devices using telnet for example geo fix some coordinates of one location. Recently i was not able to launch telnet in windows 7 and i found that its not enabled by default. So whoever gets stuck with this try to enable it by using start menu -> controlpanel -> programs -> program features -> Turn on/off window features which will load all the features do check the Telnet client box and say ok it will enable the telnet. In order to test it launch start menu and in search program box enter telnet it will display telnet command line program on pressing it will launch the telnet console which can be used for connecting to emulator using telnet commands like
o localhost 5555 (for opening the emulator host) and then
geo fix 37.24 , 112.220( for fixing the latitudes and longitudes)
Happy coding...............
o localhost 5555 (for opening the emulator host) and then
geo fix 37.24 , 112.220( for fixing the latitudes and longitudes)
Happy coding...............
Subscribe to:
Comments (Atom)