tag:blogger.com,1999:blog-88139213102150894132024-03-08T04:03:17.098-08:00Android WorldKnowledge Grows When SharedVikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.comBlogger16125tag:blogger.com,1999:blog-8813921310215089413.post-3363165120796772862013-02-23T20:44:00.001-08:002013-02-23T20:44:43.948-08:00Android Text to speach libraryAndroid now talks, and so can your apps. Text-to-speech can help you push your app in new directions. <br />
<br />
TTS=Text To Speach Library for Android -- Comes with 1.6 API Level 4 (Also called speech synthesis)<br />
TTS Engine supports-- English, French, German, Italian and Spanish<br />
General code:- <br />
The simplest way to do so is to use the <code>speak()</code> method<br />
<pre class="prettyprint"><span class="typ">String</span><span class="pln"> myText1 </span><span class="pun">=</span><span class="pln"> </span><span class="str">"Did you sleep well?"</span><span class="pun">;</span><span class="pln">
</span><span class="typ">String</span><span class="pln"> myText2 </span><span class="pun">=</span><span class="pln"> </span><span class="str">"I hope so, because it's time to wake up."</span><span class="pun">;</span><span class="pln">
mTts</span><span class="pun">.</span><span class="pln">speak</span><span class="pun">(</span><span class="pln">myText1</span><span class="pun">,</span><span class="pln"> </span><span class="typ">TextToSpeech</span><span class="pun">.</span><span class="pln">QUEUE_FLUSH</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">);</span><span class="pln">
mTts</span><span class="pun">.</span><span class="pln">speak</span><span class="pun">(</span><span class="pln">myText2</span><span class="pun">,</span><span class="pln"> </span><span class="typ">TextToSpeech</span><span class="pun">.</span><span class="pln">QUEUE_ADD</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">);</span></pre>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* android ads */
google_ad_slot = "2733719690";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com8tag:blogger.com,1999:blog-8813921310215089413.post-55158187052948727792012-05-31T23:25:00.001-07:002013-02-23T18:19:18.304-08:00SAX vs DOM Parsers<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* spirituality ads */
google_ad_slot = "5966387691";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<div dir="ltr" style="text-align: left;" trbidi="on">
SAX parser can get better speed.
A tree-based API is centered around a tree structure and therefore provides interfaces on components of a tree (which is a DOM document) such as Document interface,Node interface, NodeList interface, Element interface, Attr interface and so on. By contrast, however, an event-based API provides interfaces on handlers. There are four handler interfaces, ContentHandler interface, DTDHandler interface, EntityResolver interface and ErrorHandler interface.
<br /></div>
<B>DOM parsers and SAX parsers work in different ways.</B>
<P>
* A DOM parser creates a tree structure in memory from the input document and then waits for requests from client. But a SAX parser does not create any internal structure. Instead, it takes the occurrences of components of a input document as events, and tells the client what it reads as it reads through the input document.</P>
<p>
* A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client. But a SAX parser serves the client application always only with pieces of the document at any given time.
* With DOM parser, method calls in client application have to be explicit and forms a kind of chain. But with SAX, some certain methods (usually overriden by the cient) will be invoked automatically (implicitly) in a way which is called "callback" when some certain events occur. These methods do not have to be called explicitly by the client, though we could call them explicitly.
</p>
<p>
Ideally a good parser should be fast (time efficient),space efficient, rich in functionality and easy to use . But in reality, none of the main parsers have all these features at the same time. For example, a DOMParser is rich in functionality (because it creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree), but it is space inefficient when the document is huge, and it takes a little bit long to learn how to work with it. A SAXParser, however, is much more space efficient in case of big input document (because it creates no internal structure). What's more, it runs faster and is easier to learn than DOMParser because its API is really simple. But from the functionality point of view, it provides less functions which mean that the users themselves have to take care of more, such as creating their own data structures.
</P>
</p>
<table style="border-collapse:collapse;" cellspacing="0" cellpadding="0" width="100%" border="1">
<tbody>
<tr>
<td width="50%" height="16"><font face="Verdana" color="#ff0000" size="2"><b>SAX</b></font></td>
<td width="50%" height="16"><font face="Verdana" color="#ff0000" size="2"><b>DOM</b></font></td>
</tr>
<tr>
<td width="100%" colspan="2" height="32"><font face="Verdana" color="#ff0080" size="2">Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation.</font></td>
</tr>
<tr>
<td width="50%" height="32"><font face="Verdana" color="#0000ff" size="2">Parses node by node</font></td>
<td width="50%" height="32"><font face="Verdana" color="#0000ff" size="2">Stores the entire XML document into memory before processing</font></td>
</tr>
<tr>
<td width="50%" height="16"><font face="Verdana" color="#0000ff" size="2">Doesn’t store the XML in memory</font></td>
<td width="50%" height="16"><font face="Verdana" color="#0000ff" size="2">Occupies more memory</font></td>
</tr>
<tr>
<td width="50%" height="16"><font face="Verdana" color="#0000ff" size="2">We cant insert or delete a node</font></td>
<td width="50%" height="16"><font face="Verdana" color="#0000ff" size="2">We can insert or delete nodes</font></td>
</tr>
<tr>
<td width="50%" height="15"><font face="Verdana" color="#0000ff" size="2">Top to bottom traversing</font></td>
<td width="50%" height="15"><font face="Verdana" color="#0000ff" size="2">Traverse in any direction.</font></td>
</tr>
<tr>
<td width="50%" height="16"><font face="Verdana" color="#0000ff" size="2">SAX is an event based parser</font></td>
<td width="50%" height="16"><font face="Verdana" color="#0000ff" size="2">DOM is a tree model parser</font></td>
</tr>
<tr>
<td width="50%" height="16"><font face="Verdana" color="#0000ff" size="2">SAX is a Simple API for XML</font></td>
<td width="50%" height="16"><font face="Verdana" color="#0000ff" size="2">Document Object Model (DOM) API</font></td>
</tr>
<tr>
<td width="50%" height="50"><font face="Verdana" color="#0000ff" size="2">import javax.xml.parsers.*;<br />import org.xml.sax.*;<br />import org.xml.sax.helpers.*;</font></td>
<td width="50%" height="50"><font face="Verdana" color="#0000ff" size="2">import javax.xml.parsers.*;<br />import org.w3c.dom.*;<br /> </font></td>
</tr>
<tr>
<td width="50%" height="16"><font face="Verdana"><font color="#0000ff"><font size="2">doesn’t </font><font size="-1">preserve comments</font></font></font></td>
<td width="50%" height="16"><font face="Verdana" color="#0000ff" size="-1">preserves comments</font></td>
</tr>
<tr>
<td width="50%" height="18"><font color="#0000ff">SAX generally runs a little faster than DOM</font></td>
<td width="50%" height="18"><font color="#0000ff">SAX generally runs a little faster than DOM</font></td>
</tr>
<tr>
<td width="100%" colspan="2" height="69"><font face="Verdana" color="#ff0080" size="2">If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.</font></td>
</tr>
</tbody>
</table>Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com6tag:blogger.com,1999:blog-8813921310215089413.post-16279754814794682722011-12-09T07:55:00.000-08:002013-02-23T20:45:05.941-08:00HTML5 - LocalStorage<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* android ads */
google_ad_slot = "2733719690";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
One of the most useful feature of HTML 5 for the mobile and web developers is Local Storage. LocalStorage is a handy API included in the "HTML5" wave that gives developers an easy to use 5MB store on an users local machine. Local Storage is simple API's developers can use to store data and use it later in an offline mode.<br />
For example, if you are on a web page and by mistake you close it or navigate to another page while filling some useful information in the form, all the information input in the form is lost...What a bummer :(<br />
<br />
By nature, web html pages do not persist data, it was hard to persist the information. Local Storage API's has been designed to address this issue.<br />
<br />
Local Storage is a simple key/value dictionary with a simple API to match:<br />
simple code snippet would look like:- <br />
<b><i>//To Save a value to localStorage<br />
localStorage.setItem('key', 'value-to-be-saved');<br />
//OR<br />
localStorage.key = 'string value-to-be-saved';<br />
<br />
//Get the value back out of localStorage<br />
localStorage.getItem('key');<br />
//OR<br />
localStorage.key;<br />
<br />
//Clear all localStorage values<br />
localStorage.clear();</i><br />
</b><br />
I hope above is very simple and does not need any explanation.<br />
<br />
<b>Difference between Local Storage and Session Storage</b><br />
Joining LocalStorage in the HTML5 spec is also SessionStorage. It provides an identical API, but a different "retention policy." As the name implies, values in SessionStorage should only survive a single browser session (not server session). <br />
<br />
<b>How Local Storage Works</b><br />
<br />
let's take a few moments and first learn how local storage actually works.<br />
<br />
At the core, each of your pages has a reference to a global object called localStorage:<br />
If you step inside this object, there is a defined key-value pair structure that is well designed for storing data. The way it stores the data is by neatly organizing them into rows of key/value pairs.<br />
<br />
In the above localStorage visualization, you can see that there are three pieces of data:<br />
<br />
firstName with a value of Vikrant<br />
lastName with a value of Bains<br />
location with a value of Los-Angeles<br />
<br />
Think of each row as one contiguous piece of data made up of a key and a value. The key serves as the identifier to your data, and the value is the information associated with it. <br />
<br />
An important thing to note is the scope of this data. The localStorage data is domain-specific where you have only one instance per domain. In other words, any data inside your localStorage object is available only when you are referring to it from a page hosted within the same domain.<br />
<br />
<b>Some important facts:- </b> <br />
<b>1. LocalStorge values on Secure (SSL) pages are isolated </b><br />
Browsers isolate LocalStorage values based on scheme + hostname + unique port (also known as an HTML5 Origin). Hostname is expected, since you don't want malicious websites to have access to other websites' LocalStorage data. But scheme (i.e. http and https)?<br />
<br />
The result of this separation means that a value saved to LocalStorage on http://abc.com cannot be accessed by pages served from https://abc.com (and vice versa). There is some good reason for this (I suppose), especially in the case of isolating values created during a secure session from unsecured sessions. But since LocalStorage should never be a place for storing sensitive data,it makes this little strange as well...<br />
<br />
<b>2. SessionStorage values survive some browser restarts<br />
</b><br />
SessionStorage, unlike LocalStorage, is not designed for long-term persistence of values in the user's browser. Instead, values in SessionStorage are destroyed when a browser session ends, which is usually when the browser window is closed.<br />
<br />
There is an exception, though.<br />
<br />
When a browser provides a "Restore Session" feature, usually designed to help users quickly recover from a browser/computer crash, values in SessionStorage will be restored, too. So while it's a new "session" on the server, from the browser's perspective, it's a continuation of a single session across a browser restart.<br />
<br />
<b>3. LocalStorage values created in "incognito" or Safe mode are isolated<br />
</b><br />
When you fire-up a browser in private/incognito/safe mode , it will create a new, temporary database for LocalStorage values. That means anything saved to LocalStorage will be destroyed when the private browsing session is closed, making LocalStorage behave more like SessionStorage.<br />
<br />
Additionally, since a browser's "Session Restore" feature does not re-open private mode sessions, anything created in SessionStorage will also be lost after the browser window is closed. Really, in short, any data put in Local or SessionStorage during a private browsing session will be lost as soon as the browser window is closed (intentionally or not).<br />
<br />
<b>4. LocalStorage quotas cannot be made bigger than 5MB<br />
</b><br />
LocalStorage is not supposed to be the primary form of in-browser storage with HTML5 (IndexDB will eventually come along to provide that), but some apps may want more than the default 5MB LocalStorage provides. There is NO way to expand LocalStorage quotas.<br />
<br />
Technically, the LocalStorage origin restriction does not block sub-domains of the same host (using the same scheme and port) from accessing the same LocalStorage object. As a result, some browsers have exposed a workaround that grants "a1.website.com" and "a2.website.com" their own 5MB LocalStorage quotas. And since both sites are on the same origin, they can access each others values.<br />
<br />
<b>5. LocalStorage can be used on older browsers (including IE)<br />
</b><br />
Legacy browsers can use this feature as well. Fortunately, LocalStorage is incredibly well supported in Class A browsers. It's natively available in IE8+ (!), Firefox 3.5+, and Chrome 4+.<br />
<b><br />
Simple self explanatory code snippets</b><br />
<br />
Detecting Whether Local Storage Exists or Not<br />
function isLocalStorageSupported {<br />
<br />
try {<br />
<br />
var supported = false;<br />
if (window['localStorage'] !== null)<br />
{<br />
<br />
supported = true;<br />
<br />
}<br />
return supported;<br />
<br />
} catch(e) {<br />
<br />
return false;<br />
<br />
}<br />
<br />
} <br />
<br />
<b>Dealing with File Size</b><br />
<br />
You have a fixed size of 5 MB for each domain your local storage is tied to. If you try to add more data even after you have exceed your quota, a QUOTA_EXCEEDED_ERR exception will be thrown.<br />
<br />
Handling this exception is pretty straightforward:<br />
<br />
try {<br />
<br />
localStorage.setItem("key", "some data");<br />
<br />
} catch (e) {<br />
<br />
if (e == QUOTA_EXCEEDED_ERR) {<br />
<br />
// do something nice to notify your users<br />
<br />
}<br />
<br />
}<br />
<br />
All you need to do is wrap any code that tries to add more data to your localStorage into a try/catch statement. While just catching the exception will prevent users from seeing a script error, matching the exception to the QUOTA_EXCEEDED_ERR exception will allow you to go one step further and let your users know why they were not able to save the data to local local storage.<br />
<br />
<b>Removing Data</b><br />
<br />
There are two extremes to removing data from your localStorage object. You can remove everything scorched earth style, or you can selectively remove a key/value pair individually.<br />
<br />
Because scorched earth is an awesome game, let's look at it first. To remove everything from your localStorage for your current domain, you can call the clear() method:<br />
<br />
localStorage.clear();<br />
<br />
This will remove all traces of any data you may have stored for this domain, so use it cautiously if you have several pages on your site that each write to the local storage independently.<br />
<br />
To remove only select key/value entries from your local storage, you can use removeItem() and pass in the key name associated with the data you wish to remove:<br />
<br />
localStorage.removeItem("location");<br />
<br />
In this example, the location key and its value will be deleted while leaving all of the other data intact.<br />
<br />
<b>Retrieving Data</b><br />
<br />
To retrieve data stored in your localStorage object, you use the getItem method:<br />
<br />
var firstNameData = localStorage.getItem("firstName");<br />
var lastNameData = localStorage.getItem("lastName");<br />
var locationData = localStorage.getItem("location");<br />
<br />
The getItem method takes only the key as an argument, and it returns the value associated with that key. If the key you pass in does not exist, a value of undefined is returned.<br />
Note<br />
<br />
If you don't like using the getItem or setItem methods, you can bypass them by using object notation for setting and retrieving data:<br />
<br />
// storing data<br />
localStorage[key] = value;<br />
// retrieiving data<br />
var myData = localStorage[key]; <br />
<br />
<b>Adding Data</b><br />
<br />
To add data to your local storage object, all you need is a key and a value...and the setItem method that lives on your localStorage object:<br />
<br />
localStorage.setItem('firstName', 'Vikrant');<br />
localStorage.setItem('lastName', 'Bains');<br />
localStorage.setItem('location', 'Los-Angeles');<br />
<br />
In each line, we are adding a key/value pair to our localStorage object. The setItem method takes two arguments. The first argument is the key, and the second argument is the value.<br />
<br />
Conclusion- Local Storage is very simple to use but like any other project, careful design decisions needs to be made before implementing it. Merely simplicity of use should not be a driving factor to use thisVikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com0tag:blogger.com,1999:blog-8813921310215089413.post-90798737457090390492011-01-06T00:00:00.000-08:002011-01-06T00:00:07.432-08:00Android 3.0 Preview<iframe width="480" height="295" src="http://www.youtube.com/embed/hPUGNCIozp0?fs=1" frameborder="0"></iframe>Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com2tag:blogger.com,1999:blog-8813921310215089413.post-32365370373512715252011-01-03T00:47:00.001-08:002013-02-23T20:43:08.930-08:00Testing Gigya -- Login<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* android ads */
google_ad_slot = "2733719690";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<script type="text/javascript" lang="javascript" src="http://cdn.gigya.com/JS/socialize.js"></SCRIPT><br />
<script>
var conf =
{
APIKey: '2_Y82PzwJ_chSFImHXaIDJClnLyJzmk-VFOavSsaNTzl6m901s_NNxRAS0xJ3bd3_N'
};
</script><br />
<script type="text/javascript" lang="javascript"
src="http://cdn.gigya.com/JS/socialize.js?apikey=2_Y82PzwJ_chSFImHXaIDJClnLyJzmk-VFOavSsaNTzl6m901s_NNxRAS0xJ3bd3_N">
</SCRIPT><br />
<br />
<script type="text/javascript">
var conf = {
// Pleae replace the following key with your API Key
APIKey:'2_Y82PzwJ_chSFImHXaIDJClnLyJzmk-VFOavSsaNTzl6m901s_NNxRAS0xJ3bd3_N'
}
// Publish an Activity Feed internally (only to site scope)
// This method is associated with the "btnPublishAction" click
function publishFeed() {
// Constructing a UserAction Object
var act = new gigya.services.socialize.UserAction();
act.setUserMessage("This is the user message"); // Setting the User Message
act.setLinkBack("http://gigya.com"); // Adding a Link Back
act.setTitle("This is my title"); // Setting the Title
act.setDescription("This is my Description"); // Setting the Description
act.addMediaItem( { // Adding a Media (image)
src: 'http://www.cherrybam.com/graphics/graphics-cute/cute004.gif',
href: 'http://www.cherrybam.com/',
type: 'image'
});
// Parameters for the publishUserAction method,
var params =
{
userAction:act, // including the UserAction object
scope: 'internal', // the Activity Feed will be published interanlly (site scope) only (not to social networks).
privacy: 'public',
callback:publishAction_callback
};
// Publish the User Action
gigya.services.socialize.publishUserAction(conf, params);
}
// Display a status message according to the response from publishUserAction.
function publishAction_callback(response)
{
switch (response.errorCode )
{
case 0:
document.getElementById('status').style.color = "green";
document.getElementById('status').innerHTML =
"Activity Feed item published, and will be presented shortly on the Activity Feed Widget.";
break;
default:
document.getElementById('status').style.color = "red";
document.getElementById('status').innerHTML =
"Unable to send Feed item. status="
+ response.errorCode + "; " + response.errorMessage + ";
"
+ "Please make sure you are logged in to Gigya. You may log in using the Login Widget inside the 'Me' tab above" ;
}
}
// Logout from Gigya platform. This method is activated when "Logout" button is clicked
function logoutFromGS() {
gigya.services.socialize.logout(conf, {/*no required params*/}); // logout from Gigya platform
}
</script><br />
<br />
<br />
<br />
<script type="text/javascript">
// show the Activity Feed Widget inside the 'ActivityFeedWidget' <div>
:
gigya.services.socialize.showFeedUI(conf, {containerID: 'ActivityFeedWidget'});
</script><br />
<br />
<br />
<br />
<br />
Click the button below to publish an Activity Feed item:<br />
<br />
(Please make sure you are logged in to Gigya, you may log in using the Login Widget on the "Me" tab above)<br />
<br />
<input type=button id="btnPublishAction" onclick="publishFeed()" value="Publish" /><br />
<div id="status"></div><br />
<br />
<br />
<br />
<br />
Click the button below to logout from Gigya platform:<br />
<br />
<input id="btnLogout" type="button" value="Logout" onclick="logoutFromGS()"/>Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com0tag:blogger.com,1999:blog-8813921310215089413.post-49452710583973180912010-12-19T01:18:00.000-08:002010-12-19T01:18:53.287-08:00Google Code Blog: Save the date for Google I/O 2011<a href="http://googlecode.blogspot.com/2010/06/save-date-for-google-io-2011.html?spref=bl">Google Code Blog: Save the date for Google I/O 2011</a>: "Google I/O just recently came to a close, but it won’t be long before we start gearing up for next year. And we’d like to make sure it’s on..."Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com1tag:blogger.com,1999:blog-8813921310215089413.post-30115568557474101612010-12-05T21:18:00.000-08:002010-12-05T21:18:47.451-08:00Non UI blocking network call / web request<div style="color: black; font-family: Arial,Helvetica,sans-serif;">Code snippet to fetche content(s) from the web without blocking the UI (runs in the background in a Thread). Once finished, it posts a Handler that is picked up by the UI as soon as possible. </div><div style="color: blue; font-family: "Courier New",Courier,monospace;"><span style="color: black; font-family: Arial,Helvetica,sans-serif;">It is widely used in most of commercial/production level apps</span> </div><div style="color: blue; font-family: "Courier New",Courier,monospace;"><br />
</div><div style="color: blue; font-family: "Courier New",Courier,monospace;"><b><span style="color: red;">Code Snippet:-</span></b></div><div style="color: blue; font-family: "Courier New",Courier,monospace;">import java.io.BufferedInputStream;<br />
import java.io.InputStream;<br />
import java.net.URL;<br />
import java.net.URLConnection;<br />
import org.apache.http.util.ByteArrayBuffer;<br />
<br />
public class Iconic extends Activity {<br />
private String html = "";<br />
private Handler mHandler;<br />
<br />
public void onCreate(Bundle savedInstanceState) {<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br />
mHandler = new Handler();<br />
checkUpdate.start();<br />
}<br />
<br />
private Thread checkUpdate = new Thread() {<br />
public void run() {<br />
try {<br />
URL updateURL = new URL("http://iconic.4feets.com/update");<br />
URLConnection conn = updateURL.openConnection();<br />
InputStream is = conn.getInputStream();<br />
BufferedInputStream bis = new BufferedInputStream(is);<br />
ByteArrayBuffer baf = new ByteArrayBuffer(50);<br />
<br />
int current = 0;<br />
while((current = bis.read()) != -1){<br />
baf.append((byte)current);<br />
}<br />
<br />
/* Convert the Bytes read to a String. */<br />
html = new String(baf.toByteArray());<br />
mHandler.post(showUpdate);<br />
} catch (Exception e) {<br />
}<br />
}<br />
};<br />
<br />
private Runnable showUpdate = new Runnable(){<br />
public void run(){<br />
Toast.makeText(Iconic.this, "HTML Code: " + html, Toast.LENGTH_SHORT).show();<br />
}<br />
};<br />
}</div>Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com1tag:blogger.com,1999:blog-8813921310215089413.post-58274008303463064852010-12-05T20:53:00.000-08:002013-02-23T20:45:51.059-08:00Check for Updates in background Once in two days - schedule a background updates checker<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* android ads */
google_ad_slot = "2733719690";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<div style="color: blue; font-family: "Courier New",Courier,monospace;"><span style="color: black;">This code checks for updates of the Activity once in 2 days and in the background. If an update (higher version than current) is found, it opens a Dialog and asks the user to open the market. </span></div><div style="color: blue; font-family: "Courier New",Courier,monospace;"><br />
</div><div style="color: blue; font-family: "Courier New",Courier,monospace;"><b><span style="color: red;">code-snippet:-</span></b> </div><div style="color: blue; font-family: "Courier New",Courier,monospace;">public class Test extends Activity {<br />
private Handler mHandler;<br />
<br />
<br />
public void onCreate(Bundle savedInstanceState) {<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.front);<br />
mHandler = new Handler();<br />
<br />
/* Get Last Update Time from Preferences */<br />
SharedPreferences prefs = getPreferences(0);<br />
lastUpdateTime = prefs.getLong("lastUpdateTime", 0);<br />
<br />
/* Should Activity Check for Updates Now? once in 2days */<br />
if ((lastUpdateTime + (2*24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {<br />
<br />
/* Save current timestamp for next Check*/<br />
lastUpdateTime = System.currentTimeMillis(); <br />
SharedPreferences.Editor editor = getPreferences(0).edit();<br />
editor.putLong("lastUpdateTime", lastUpdateTime);<br />
editor.commit(); <br />
<br />
/* Start Update */ <br />
checkUpdate.start();<br />
}<br />
}<br />
<br />
/* This Thread checks for Updates in the Background */<br />
private Thread checkUpdate = new Thread() {<br />
public void run() {<br />
try {<br />
URL updateURL = new URL("http://abc.com/update"); <br />
URLConnection conn = updateURL.openConnection(); <br />
InputStream is = conn.getInputStream();<br />
BufferedInputStream bis = new BufferedInputStream(is);<br />
ByteArrayBuffer baf = new ByteArrayBuffer(50);<br />
<br />
int current = 0;<br />
while((current = bis.read()) != -1){<br />
baf.append((byte)current);<br />
}<br />
<br />
/* Convert the Bytes read to a String. */<br />
final String s = new String(baf.toByteArray()); <br />
<br />
/* Get current Version Number */<br />
int curVersion = getPackageManager().getPackageInfo("your.application.id", 0).versionCode;<br />
int newVersion = Integer.valueOf(s);<br />
<br />
/* Is a higher version than the current already out? */<br />
if (newVersion > curVersion) {<br />
/* Post a Handler for the UI to pick up and open the Dialog */<br />
mHandler.post(showUpdate);<br />
} <br />
} catch (Exception e) {<br />
}<br />
}<br />
};<br />
<br />
/* This Runnable creates a Dialog and asks the user to open the Market */ <br />
private Runnable showUpdate = new Runnable(){<br />
public void run(){<br />
new AlertDialog.Builder(Test.this)<br />
.setIcon(R.drawable.icon)<br />
.setTitle("Update Available")<br />
.setMessage("An update for is available!\n\nOpen Android Market and see the details?")<br />
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {<br />
public void onClick(DialogInterface dialog, int whichButton) {<br />
/* User clicked OK so do some stuff */<br />
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.application.id"));<br />
startActivity(intent);<br />
}<br />
})<br />
.setNegativeButton("No", new DialogInterface.OnClickListener() {<br />
public void onClick(DialogInterface dialog, int whichButton) {<br />
/* User clicked Cancel */<br />
}<br />
})<br />
.show();<br />
}<br />
}; <br />
}</div><div style="color: blue; font-family: "Courier New",Courier,monospace;"><br />
</div><div style="color: blue; font-family: "Courier New",Courier,monospace;"><br />
</div>Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com0tag:blogger.com,1999:blog-8813921310215089413.post-50512130034593533152010-12-05T20:38:00.000-08:002013-02-23T20:46:02.295-08:00To check if SD Card is present on android device via codeTo check if SD Card is present on the device, we can use simple code<br />
<div style="color: blue; font-family: "Courier New",Courier,monospace;"><b><span style="font-size: small;">public static boolean isSdCardPresent(){</span></b></div><div style="color: blue; font-family: "Courier New",Courier,monospace;"><b><span style="font-size: small;">return android.os.Environment.getExternalStorageState().equals(<span><span>android.os.Environment.MEDIA_MOUNTED); </span></span></span></b></div><span><span><b><span style="font-family: "Courier New",Courier,monospace; font-size: small;"><span style="color: blue;">} </span></span></b></span></span><br />
<span><span><b><span style="font-family: "Courier New",Courier,monospace; font-size: small;"><span style="color: blue;"> </span></span></b></span></span>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* android ads */
google_ad_slot = "2733719690";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com0tag:blogger.com,1999:blog-8813921310215089413.post-47505017589043927222010-11-10T13:16:00.000-08:002010-11-10T13:16:18.182-08:00Obtain IP Address for android deviceTwo ways to find the device ip:- <br />
<br />
1. Method1:- Open a socket to a known website/webserver like google.com or yahoo.com and get the ip using the technique shown below. It is straight forward(but not recommended)<br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">String get_ip(){</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">java.net.Socket conn = null;</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">String ipAddress;</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;"></span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">try {</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">conn = new java.net.Socket("www.google.com", 80);</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">} catch (UnknownHostException unknownhostexception) {</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">unknownhostexception.printStackTrace();</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">} catch (IOException ioexception) {</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">ioexception.printStackTrace();</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">} </span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">ipAddress = conn.getLocalAddress().toString();</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">Toast.makeText(ip.this," Device IP --"+ipAddress,Toast.LENGTH_LONG).show();</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">return ipAddress;</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">}</span><br />
2. Method2:- Iterate over all network interfaces and iterate there over all ip addresses. (Recommended way)<br />
<br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">public String getLocalIpAddress() {</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;"></span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">try {</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">for (Enumeration<networkinterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">NetworkInterface intf = en.nextElement();</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">for (Enumeration<inetaddress> enumIpAddr = intf.getInetAddresses(); </span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">enumIpAddr.hasMoreElements();) {</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">InetAddress inetAddress = enumIpAddr.nextElement();</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">if (!inetAddress.isLoopbackAddress()) {</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">return inetAddress.getHostAddress().toString();}}}</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">} catch (SocketException exception) {</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">Log.e("We got Exception here", exception.toString());</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">}</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">return null;</span><br />
<span style="color: blue; font-family: "Courier New", Courier, monospace;">}</span><br />
<br />
Note:- This will give you simple way to get the ip address (For device on WiFi we need to access wifiManager class) and is independent of Android. This is generic java code and can be used on the laptop as well<br />
<br />
<br />
Hope it helps!Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com0tag:blogger.com,1999:blog-8813921310215089413.post-36341617569248602092010-09-04T16:26:00.000-07:002013-02-23T20:43:22.787-08:00Harsha Bhogle at IIMA<embed allowfullscreen="true" allowscriptaccess="always" id="VideoPlayback" src="http://video.google.com/googleplayer.swf?docid=6922225374387263864&hl=en&fs=true" style="height: 326px; width: 400px;" type="application/x-shockwave-flash"> </embed>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* android ads */
google_ad_slot = "2733719690";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com2tag:blogger.com,1999:blog-8813921310215089413.post-17408059062904945282010-09-04T15:51:00.000-07:002013-02-23T20:45:30.010-08:00Android figuresAn overview about the Android SDK<br />
<br />
<br />
1. The Android SDK was announced officially on November 12, 2007.<br />
<br />
2. Table showing various Android versions and API levels<br />
<div align="left" class="separator" style="clear: both; text-align: center;"><br />
</div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">3. The Latest version of Android SDK is 2.2 called as Froyo</div><br />
4. Android had a 2.8% share of the worldwide smartphone market. By the following quarter (Q3 2009),<br />
Android’s market share had grown to 3.5%.<br />
<br />
5. Android would become the world’s second most popular smartphone platform. while BlackBerry falls <br />
<div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/_xp-CB29jbVA/TILM0ehyFUI/AAAAAAAAATo/6VUGiiUI6KM/s1600/android-api-dates.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="491" ox="true" src="http://4.bp.blogspot.com/_xp-CB29jbVA/TILM0ehyFUI/AAAAAAAAATo/6VUGiiUI6KM/s640/android-api-dates.bmp" width="640" /></a></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"> from 2nd to 5th place. iPhone remains in 3rd place. Microsoft’s Windows Mobile remains in 4th place.</div>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* android ads */
google_ad_slot = "2733719690";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com0tag:blogger.com,1999:blog-8813921310215089413.post-46027781797401841612010-07-11T21:15:00.000-07:002013-02-23T20:43:51.538-08:00Android Handler<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* android ads */
google_ad_slot = "2733719690";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
Message Handler<br />
I have started looking into Android. <br />
<br />
I have tried to build a utility class that can do Network I/O. And grasping things was little hard at first. Network I/O or other heavy-duty stuff in Android should be done on other worker thread. Because doing it in main thread (or the UI thread) can ( and will ) make your application unresponsive and may be killed as the system is persuaded to think that it has hung. For every Android developer, this is a must-read.<br />
<br />
So you have to do the long running operations in separate thread. And to interact between threads you have to resort to Handler. A Handler is used to send message or runnable to a particular thread. The thing to remember is that a Handler is associated with the MessageQueue of the single thread which has created it. After creating a Handler, it can be used to post message or runnable to that particular thread.<br />
<br />
Here is an example<br />
<br />
--------------------------------------------------------------------------------<br />
<ol><li><i>public class MyActivity extends Activity { <br />
<br />
void startHeavyDutyStuff() {<br />
<br />
// Here is the heavy-duty thread<br />
Thread t = new Thread() {<br />
<br />
public void run() {<br />
while (true) {<br />
<br />
mResults = doSomethingExpensive();<br />
<br />
//Send update to the main thread<br />
messageHandler.sendMessage(Message.obtain(messageHandler, mResults)); }}};<br />
t.start();<br />
}<br />
<br />
// Instantiating the Handler associated with the main thread.<br />
private Handler messageHandler = new Handler() {<br />
<br />
@Override<br />
public void handleMessage(Message msg) { <br />
switch(msg.what) {<br />
//handle update<br />
//.....}}};}</i></li>
</ol>--------------------------------------------------------------------------------Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com3tag:blogger.com,1999:blog-8813921310215089413.post-327380678494186312009-12-04T08:41:00.000-08:002009-12-04T08:43:38.580-08:00Dial a number from AppCode snippet to dial a number from app:-<br /><br /><blockquote>import android.app.Activity;<br />import android.content.Intent;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.view.KeyEvent;<br />import android.view.View;<br />import android.widget.Button;<br />import android.widget.EditText;<br />import android.widget.LinearLayout;<br /><br />public class DialANumber extends Activity {<br /> EditText mEditText_number = null;<br /> LinearLayout mLinearLayout_no_button = null;<br /> Button mButton_dial = null;<br /><br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /><br /> mLinearLayout_no_button = new LinearLayout(this);<br /><br /> mEditText_number = new EditText(this);<br /> mEditText_number.setText("5551222");<br /> mLinearLayout_no_button.addView(mEditText_number);<br /><br /> mButton_dial = new Button(this);<br /> mButton_dial.setText("Dial!");<br /> mLinearLayout_no_button.addView(mButton_dial);<br /> mButton_dial.setOnClickListener(new View.OnClickListener() {<br /> public void onClick(View v) {<br /> performDial();<br /> }<br /> });<br /><br /> setContentView(mLinearLayout_no_button);<br /> }<br /><br /> public boolean onKeyDown(int keyCode, KeyEvent event) {<br /> if (keyCode == KeyEvent.KEYCODE_CALL) {<br /> performDial();<br /> return true;<br /> }<br /> return false;<br /> }<br /><br /> public void performDial(){<br /> if(mEditText_number!=null){<br /> try {<br /> startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mEditText_number.getText())));<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> }<br />}</blockquote>Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com0tag:blogger.com,1999:blog-8813921310215089413.post-75941267089426516052009-02-15T20:23:00.000-08:002009-02-15T20:27:36.218-08:00J2ME App Architecture<a href="http://3.bp.blogspot.com/_xp-CB29jbVA/SZjrMqCi2uI/AAAAAAAAAK8/kWZJhLs0DB8/s1600-h/arch2.jpg"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 210px;" src="http://3.bp.blogspot.com/_xp-CB29jbVA/SZjrMqCi2uI/AAAAAAAAAK8/kWZJhLs0DB8/s320/arch2.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5303247163904678626" /></a><br /><strong>Architectural Options </strong><br /><br />Each cell phone could communicate with every required city traffic system. This solution distributes the networking and processing load onto many clients, but also means that each client needs to know how to communicate with each remote system (assuming the remote systems do not share the exact same protocol and implementation). <br /><br />There could be a single intermediary server which would communicate with each of the cell phones and each of the city traffic systems. This means the intermediate server is a single point of failure (unless made redundant, perhaps via clustering) but also that each client only needs to be able to communicate with one server (via one protocol). <br /><br />The second solution, illustrated below, is better. By keeping the client simple and pushing complexity to the server, the complexity becomes easier to manage. Mitigating the weaknesses of cell phones--a slow processor, weak user interface, slow and intermittent network access--is a secondary benefit. <br /><br /><br />The intermediary server architecture. The cell phone communicates via the carrier's network to the intermediary server, which processes requests and dispatches them to other systems, as needed.<br /><br />Other benefits of the intermediary server architecture<br /><br />Though cell phones are constantly improving, they are still low powered consumer devices. There are certain activities that, while perhaps achievable on a cell phone, are easier done on a server. If the traffic congestion service offloads complicated tasks, such as email notifications to family or friends regarding late arrival or map generation for route finding, to the server, more functionality can be offered without complicating the client. <br /><br />It is possible to cache information retrieved from the city traffic systems at the intermediary server. Therefore, when everyone is stuck at one interchange, the city traffic database is not queried repeatedly, and users will see better performance. <br /><br />For development purposes, the intermediary server offers fewer interactions to manage and debug. Rather than M x N interactions caused by M clients talking to N systems, there are M+N interactions. <br /><br />Authentication and access control become easier; the single server can handle these sometimes complicated tasks. The traffic congestion service can easily make sure users have paid and if there are premium features added, one point of authorization makes controlling access easier. <br /><br />When the protocol to access the city traffic systems change there is no need to track down and persuade users to upgrade the application on their cell phone; instead the intermediary server can be easily upgraded. Clients can use whatever protocol is best for communicating over the wireless network, rather than being forced to understand the (perhaps different) protocols of the remote systems. <br /><br />However, an intermediary server means more testing and more code to maintain, and if that server fails, the application is inaccessible. However, the alternative architecture, each client talking to each remote system, is a protocol management nightmare, requiring complex J2ME clients.Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com1tag:blogger.com,1999:blog-8813921310215089413.post-39911553200121949822009-02-15T20:12:00.000-08:002013-02-23T20:43:36.285-08:00Mobile Enterprise App ArchitectureTopic-1 <strong>"Mobile Application Architecture,"</strong> provides general design guidelines for a<br />mobile application, explains the key attributes, discusses the use of layers, provides<br />guidelines for performance, security, and deployment, and lists the key patterns and<br />technology considerations.<br />Topic-2 <strong>"Architecture and Design Guidelines"</strong>, helps you to understand the concepts of<br />software architecture, learn the key design principles for software architecture, and<br />provides the guidelines for the key attributes of software architecture.<br />Topic-3 "<strong>Presentation Layer Guidelines</strong>,” helps you to understand how the presentation<br />layer fits into the typical application architecture, learn about the components of the<br />presentation layer, learn how to design these components, and understand the common<br />issues faced when designing a presentation layer. It also contains key guidelines for<br />designing a presentation layer, and lists the key patterns and technology considerations.<br />Topic-4 <strong>"Business Layers Guidelines,”</strong> helps you to understand how the business layer<br />fits into the typical application architecture, learn about the components of the business<br />layer, learn how to design these components, and understand common issues faced when<br />designing a business layer. It also contains key guidelines for designing the business layer,<br />and lists the key patterns and technology considerations.<br />Topic-5 <strong>"Data Access Layer Guidelines,”</strong> helps you top understand how the data layer<br />fits into the typical application architecture, learn about the components of the data layer,<br />learn how to design these components, and understand the common issues faced when<br />designing a data layer. It also contains key guidelines for designing a data layer, and lists the<br />key patterns and technology considerations.<br />Topic-6, <strong>"Service Layer Guidelines,”</strong> helps you to understand how the service layer fits<br />into the typical application architecture, learn about the components of the service layer,<br />learn how to design these components, and understand common issues faced when<br />designing a service layer. It also contains key guidelines for designing a service layer, and<br />lists the key patterns and technology considerations.<br />Topic-7 <strong>"Communication Guidelines,</strong>” helps you to learn the guidelines for designing a<br />communication approach, and understand the ways in which components communicate<br />Mobile Application Pocket Guide v1.1<br />Microsoft patterns & practices 4<br />with each other. It will also help you to learn the interoperability, performance, and security<br />considerations for choosing a communication approach, and the communication technology<br />choices available.<br />• Chapter 8, "Deployment Patterns,” helps you to learn the key factors that influence<br />deployment choices, and contains recommendations for choosing a deployment pattern. It<br />also helps you to understand the effect of deployment strategy on performance, security,<br />and other quality attributes, and learn common deployment patterns.
<script type="text/javascript"><!--
google_ad_client = "ca-pub-3230227788494538";
/* android ads */
google_ad_slot = "2733719690";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
Vikrant Bainshttp://www.blogger.com/profile/01292069774420119067noreply@blogger.com0