tag:blogger.com,1999:blog-87841594209632623322023-11-15T10:11:47.113-08:00Android TechnologyAnonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.comBlogger100125tag:blogger.com,1999:blog-8784159420963262332.post-61475148608463250582015-06-24T01:33:00.001-07:002015-06-24T01:33:58.143-07:00How to create AnalogClock and DigitalClock in Android.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Analogclock and Digitalclock views </h3>
<br />
<b>*</b> The AnalogClock view displays an analog clock with two hands — one for minutes and one for hours.<br />
<br />
<b>*</b> Its counterpart, the DigitalClock view, displays the time digitally. Both display the system time, and do not allow you to display a particular time.<br />
<br />
<b>*</b> Hence, if you want to display the time for a particular region, you have to build your own custom views. <br />
<br />Using the AnalogClock and DigitalClock views are straightforward; simply declare them in your XML file (such as <b>main.xml</b>), like this:<br />
<br /><?xml version=”1.0”encoding=”utf-8”?><br />
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical”<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”fill_parent” ><br />
<br />
<AnalogClock <br />
android:layout_width=”wrap_content” <br />
android:layout_height=”wrap_content” /><br /><br />
<DigitalClock<br />
android:layout_width=”wrap_content” <br />
android:layout_height=”wrap_content” /><br /><br />
</LinearLayout><br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-5702745346355804342015-06-19T01:40:00.000-07:002015-06-19T01:40:05.638-07:00Android ProgressBar Example.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
ProgressBar View </h3>
<br />
<b>*</b> The <b>ProgressBar</b> view provides visual feedback of some ongoing tasks, such as when you are per- forming a task in the background.<br />
<br />
<b>* For example</b>, you might be downloading some data from the Web and need to update the user about the status of the download.<br />
<br />
<b>*</b> In this case, the ProgressBar view is a good choice for this task.<br />
<br /><br /><b>1 . Using Eclipse, create an Android project and name it as ProgressBarView. </b><br />
<br />
<b>2 . Modify the main.xml file located in the res/layout folder by adding the following code in bold: </b><br />
<br />
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent" ><br /> <br /> <ProgressBar android:id="@+id/progressbar"<br /> android:layout_width="wrap_content" <br /> android:layout_height="wrap_content"<br /> style="?android:attr/progressBarStyleHorizontal" /><br /> <br /></LinearLayout><br />
<br /><b>3 . In the MainActivity.java file, add the following statements in bold: </b><br />
<br />
package com.emergingandroidtech.ProgressBarView;<br /><br />import android.app.Activity;<br />import android.os.Bundle;<br />import android.os.Handler;<br />import android.widget.ProgressBar;<br /><br />public class MainActivity extends Activity {<br /> <br /> private static int progress;<br /> private ProgressBar progressBar;<br /> private int progressStatus = 0;<br /> private Handler handler = new Handler();<br /> <br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> <br /> progress = 0;<br /> progressBar = (ProgressBar) findViewById(R.id.progressbar);<br /> progressBar.setMax(200);<br /> <br /> //---do some work in background thread---<br /> new Thread(new Runnable() <br /> {<br />
<br /> public void run() <br /> {<br /> //---do some work here---<br /> while (progressStatus < 200) <br /> {<br /> progressStatus = doSomeWork();<br /> //---Update the progress bar---<br /> handler.post(new Runnable() <br /> {<br /> public void run() {<br /> progressBar.setProgress(progressStatus);<br /> }<br /> });<br /> }<br /><br /> //---hides the progress bar---<br /> handler.post(new Runnable() <br /> {<br /> public void run() <br /> {<br /> //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---<br /> progressBar.setVisibility(8);<br /> }<br /> });<br /> } <br /><br /> //---do some long lasting work here---<br /> private int doSomeWork() <br /> {<br /> try {<br /> //---simulate doing some work---<br /> Thread.sleep(50);<br /> } catch (InterruptedException e) <br /> {<br /> e.printStackTrace();<br /> }<br /> return ++progress;<br /> } <br /> }).start(); <br /> }<br />}<br />
<br />
<b>4 . It shows the ProgressBar animating. After about five seconds, it will disappear.</b><br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-30729460235944046852015-06-01T01:19:00.001-07:002015-06-01T01:19:26.039-07:00How to update an activity from BroadcastReceiver ?<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Updating an Activity from a BroadcastReceiver </h3>
<br />
If you’ll want to send the SMS message back to the main activity of your application.<br />
<br />
<b>For example</b>, you might wish to display the message in a TextView.<br />
<br />
The following example demonstrates how you can do this.<br /><br />
<h4 style="text-align: left;">
Creating a View-Based Application Project</h4>
<br />
<b>1- Add the following lines in bold to the main.xml file:</b><br />
<br />
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><Button <br /> android:id="@+id/btnSendSMS" <br /> android:layout_width="fill_parent" <br /> android:layout_height="wrap_content"<br /> android:text="Send SMS" /> <br /> <br /><TextView <br /> android:id="@+id/textView1" <br /> android:layout_width="wrap_content" <br /> android:layout_height="wrap_content" /><br /> <br /></LinearLayout><br />
<br />
<b>2 . Add the following statements in bold to the SMSReceiver.java file: </b><br />
<br />
package com.emergingandroidtech.SMS;<br /><br />import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.telephony.SmsMessage;<br />import android.widget.Toast;<br /><br />public class SMSReceiver extends BroadcastReceiver<br />{<br /> @Override<br /> public void onReceive(Context context, Intent intent) <br /> { <br /> //---get the SMS message passed in---<br /> Bundle bundle = intent.getExtras(); <br /> SmsMessage[] msgs = null;<br /> String str = ""; <br /> if (bundle != null)<br /> {<br /> //---retrieve the SMS message received---<br /> Object[] pdus = (Object[]) bundle.get("pdus");<br /> msgs = new SmsMessage[pdus.length]; <br /> for (int i=0; i<msgs.length; i++){<br /> msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); <br /> str += "SMS from " + msgs[i].getOriginatingAddress(); <br /> str += " :";<br /> str += msgs[i].getMessageBody().toString();<br /> str += "\n"; <br /> }<br /> //---display the new SMS message---<br /> Toast.makeText(context, str, Toast.LENGTH_SHORT).show();<br /> <br /> <br /> //---send a broadcast intent to update the SMS received in the activity---<br /> Intent broadcastIntent = new Intent();<br /> broadcastIntent.setAction("SMS_RECEIVED_ACTION");<br /> broadcastIntent.putExtra("sms", str);<br /> context.sendBroadcast(broadcastIntent);<br /> } <br /> }<br />}<br /><br />
<b>3 . Add the following statements in bold to the MainActivity.java file: </b><br />
<br />
package com.emergingandroidtech.SMS;<br /><br />import android.app.Activity;<br />import android.app.PendingIntent;<br />import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.content.IntentFilter;<br /><br />import android.os.Bundle;<br />import android.telephony.SmsManager;<br />import android.view.View;<br />import android.widget.Button;<br />import android.widget.TextView;<br />import android.widget.Toast;<br /><br />public class MainActivity extends Activity {<br /> <br /> Button btnSendSMS; <br /> IntentFilter intentFilter; <br /> <br /> private BroadcastReceiver intentReceiver = new BroadcastReceiver() {<br /> @Override<br /> public void onReceive(Context context, Intent intent) {<br /> //---display the SMS received in the TextView---<br /> TextView SMSes = (TextView) findViewById(R.id.textView1); <br /> SMSes.setText(intent.getExtras().getString("sms"));<br /> }<br /> };<br /> <br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main); <br /> <br /> //---intent to filter for SMS messages received---<br /> intentFilter = new IntentFilter();<br /> intentFilter.addAction("SMS_RECEIVED_ACTION");<br /><br /> <br /> <br /> btnSendSMS = (Button) findViewById(R.id.btnSendSMS); <br /> btnSendSMS.setOnClickListener(new View.OnClickListener() <br /> {<br /> public void onClick(View v) <br /> { <br /> //sendSMS("5554", "Hello my friends!");<br /> <br /> Intent i = new <br /> Intent(android.content.Intent.ACTION_VIEW);<br /> i.putExtra("address", "5556; 5558; 5560");<br /> i.putExtra("sms_body", "Hello my friends!");<br /> i.setType("vnd.android-dir/mms-sms");<br /> startActivity(i);<br /> <br /> }<br /> });<br /> }<br /> <br /> @Override<br /> protected void onResume() {<br /> //---register the receiver---<br /> registerReceiver(intentReceiver, intentFilter);<br /> super.onResume();<br /> }<br /> <br /> @Override<br /> protected void onPause() {<br /> //---unregister the receiver---<br /> unregisterReceiver(intentReceiver);<br /> super.onPause();<br /> }<br /> <br /> <br /> <br /> //---sends an SMS message to another device---<br /> private void sendSMS(String phoneNumber, String message)<br /> { <br /> //...<br /> }<br /><br />
}<br />
<br />
<b>4. Add the following lines in Android Manifest file .</b><br />
<br />
<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.emergingandroidtech.SMS"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".MainActivity"<br /> android:label="@string/app_name"<br /> ><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /><br /> <receiver android:name=".SMSReceiver"> <br /> <intent-filter> <br /> <action android:name=<br /> "android.provider.Telephony.SMS_RECEIVED" /> <br /> </intent-filter> <br /> </receiver><br /><br /> </application><br /> <uses-sdk android:minSdkVersion="8" /><br /><br /><uses-permission android:name="android.permission.SEND_SMS"></uses-permission><br /><uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission><br /></manifest><br />
<br />
<b>5 .Using the DDMS, send an SMS message to the emulator. It shows the Toast class displaying the message received, and the TextView showing the message received.</b><br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-78618664840975978772015-05-08T02:48:00.000-07:002015-05-08T02:48:46.261-07:00Calling Built-In Application using Intent in android<div dir="ltr" style="text-align: left;" trbidi="on">
You have seen how to call activities within your own application.<br />
One of the key aspects of Android programming is using the intent to call activities from other applications.<br />
In particular, your application can call the many built-in applications that are included with an Android device.<br />
For example, if your application needs to enable a user to call a particular person saved in the Contacts application, you can simply use an Intent object to bring up the Contacts application, from which the user can select the person to call.<br />
This enables your application to present a consistent user experience, and enables you to avoid building another application to retrieve all the contacts in the Contacts application.<br />
The following Example demonstrates how to call some of the built-in applications commonly found on an Android device.<br /><br />
<h4 style="text-align: left;">
Calling Built-in Applications Using intents</h4>
<div style="text-align: left;">
<br /><b>1 . Using Eclipse, create a new Android project and name it Intents.</b></div>
<b><br /></b>
<b>2 . Add the following statements in bold to the main.xml file: </b><br />
<br />
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent" ><br /><Button<br /> android:id="@+id/btn_webbrowser"<br /> android:layout_width="fill_parent" <br /> android:layout_height="wrap_content" <br /> android:text="Web Browser" /> <br /><Button<br /> android:id="@+id/btn_makecalls"<br /> android:layout_width="fill_parent" <br /> android:layout_height="wrap_content" <br /> android:text="Make Calls" /> <br /><Button<br /> android:id="@+id/btn_showMap"<br /> android:layout_width="fill_parent" <br /> android:layout_height="wrap_content" <br /> android:text="Show Map" /> <br /><Button<br /> android:id="@+id/btn_chooseContact"<br /> android:layout_width="fill_parent" <br /> android:layout_height="wrap_content" <br /> android:text="Choose Contact" /><br /><br /> </LinearLayout><br /><br />
<b>3 . Add the following statements in bold to the MainActivity.java file: </b><br />
<br />
package com.emergingandroidtech.Intents;<br /><br />import android.app.Activity;<br />import android.content.Intent;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.provider.ContactsContract;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.Toast;<br /><br />public class MainActivity extends Activity {<br /> <br /> Button b1, b2, b3, b4 ;<br /> int request_Code = 1;<br /> <br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> <br /> //---Web browser button---<br /> b1 = (Button) findViewById(R.id.btn_webbrowser);<br /> b1.setOnClickListener(new OnClickListener()<br /> {<br /> public void onClick(View arg0){<br /> /*<br /> Intent i = new<br /> Intent(android.content.Intent.ACTION_VIEW,<br /> Uri.parse("http://emergingandroidtech.blogspot.in/"));<br /> */<br /> <br /> //---OR---<br /> Intent i = new<br /> Intent("android.intent.action.VIEW"); <br /> //Intent(android.content.Intent.ACTION_VIEW); <br /> i.setData(Uri.parse("http://emergingandroidtech.blogspot.in/"));<br /> <br /> startActivity(i); <br /> }<br /> });<br /> <br /> //---Make calls button---<br /> b2 = (Button) findViewById(R.id.btn_makecalls);<br /> b2.setOnClickListener(new OnClickListener()<br /> {<br /> public void onClick(View arg0){<br /> /*<br /> Intent i = new <br /> Intent(android.content.Intent.ACTION_DIAL);<br /> startActivity(i);<br /> */<br /> <br /> //---OR---<br /> <br /> Intent i = new <br /> Intent(android.content.Intent.ACTION_CALL, <br /> Uri.parse("tel:+651234567"));<br /> startActivity(i);<br /> <br /> }<br /> }); <br /> <br /> //---Show Map button---<br /> b3 = (Button) findViewById(R.id.btn_showMap);<br /> b3.setOnClickListener(new OnClickListener()<br /> {<br /> public void onClick(View arg0){<br /> Intent i = new <br /> Intent(android.content.Intent.ACTION_VIEW, <br /> Uri.parse("geo:37.827500,-122.481670")); <br /> startActivity(i); <br /> }<br /> }); <br /> <br /> //---Choose Contact button---<br /> b4 = (Button) findViewById(R.id.btn_chooseContact);<br /> b4.setOnClickListener(new OnClickListener()<br /> {<br /> public void onClick(View arg0){<br /> Intent i = new <br /> Intent(android.content.Intent.ACTION_PICK); <br /> //i.setType(ContactsContract.Contacts.CONTENT_TYPE);<br /> i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);<br /> startActivityForResult(i,request_Code); <br /> }<br /> }); <br /><br /> <br /> }<br /> <br /> public void onActivityResult(int requestCode, int resultCode, Intent data)<br /> {<br /> if (requestCode == request_Code) <br /> {<br /> if (resultCode == RESULT_OK)<br /> { <br /> Toast.makeText(this,data.getData().toString(), <br /> Toast.LENGTH_SHORT).show();<br /> Intent i = new Intent(<br /> android.content.Intent.ACTION_VIEW, <br /> Uri.parse(data.getData().toString()));<br /> startActivity(i);<br /> } <br /> }<br /> }<br />}<br />
<br />
<b>4 .Click the Web Browser button to load the Browser application on the emulator.<br /> </b><br />
<b>5 . Click the Make Calls button and the Phone application will load .</b><br />
<br />
<b>6 . Similarly, to load the Maps application, click the Show Map button.<br /> </b><br />
<b>NOTE In order to display the Maps application, you need to run the application on an AVD that supports the Google APIs.<br /> </b><br />
<b>7 . Click the Choose Contact application to show a list of contacts that you can select . Selecting a contact will show details about that contact.</b></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-52696199991544480402015-04-19T23:45:00.000-07:002015-04-19T23:45:06.241-07:00How to use ScrollView in Android ?<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Scrollview </h3>
<b>*</b> A<b> ScrollView </b>is a special type of FrameLayout in that it enables users to scroll through a list of views that occupy more space than the physical display.<br />
<br />
<b>*</b> The ScrollView can contain only one child view or ViewGroup, which normally is a LinearLayout. <br />
<br />
<b>*</b> <b>NOTE </b><br />
<b>-> </b>Do not use a ListView together with the ScrollView.<br />
<br />
<b>-></b> The ListView is designed for showing a list of related information and is optimized for dealing with large lists. <br />
<br />
<b>The following main.xml content shows a ScrollView containing a LinearLayout, which in turn contains some Button and EditText views:</b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<br />
<ScrollView <br />
android:layout_width=”fill_parent”<br />
android:layout_height=”fill_parent” <br />
xmlns:android=”http://schemas.android.com/apk/res/android” ><br />
<br />
<LinearLayout<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”wrap_content” <br />
android:orientation=”vertical” ><br />
<br />
<Button<br />
android:id=”@+id/button1” <br />
android:layout_width=”fill_parent” <br />
android:layout_height=”wrap_content” <br />
android:text=”Button1” /><br />
<br />
<Button <br />
android:id=”@+id/button2” <br />
android:layout_width=”fill_parent” <br />
android:layout_height=”wrap_content” <br />
android:text=”Button2” /> <Button android:id=”@+id/button3” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Button3” /> <EditText android:id=”@+id/txt” android:layout_width=”fill_parent” android:layout_height=”300px” /> <Button android:id=”@+id/button4” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Button4” /> <Button android:id=”@+id/button5” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Button5” /> </LinearLayout> </ScrollView><br />
Figure 3-12 shows the ScrollView enabling the users to drag the screen upward to reveal the views located at the bottom of the screen. </div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-9861103936982505102015-04-14T00:08:00.002-07:002015-04-14T00:08:42.184-07:00Dynamically update option menu items in Android.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Updating Menu Items Dynamically</h3>
<div style="text-align: left;">
<br /></div>
By overriding your Activity’s <b>onPrepareOptionsMenu</b> method, you can modify a Menu based on an application’s current state immediately before the Menu is displayed.<br />
<br />
This lets you dynamically disable/enable Menu Items, set visibility, and modify text.<br /><br />
Note that the <b>onPrepareOptionsMenu</b> method is triggered whenever the menu button is clicked, the overflow menu displayed, or the Action Bar is created.<br />
<br /><br />To modify Menu Items dynamically, you can either record a reference to them from within the <b>onCreateOptionsMenu</b> method when they’re created, or you can use the <b>findItem </b>method on the Menu object, where <b>onPrepareOptionsMenu </b>is overridden.<br />
<br />
<h4 style="text-align: left;">
Modifying Menu Items dynamically</h4>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
@Override </div>
<div style="text-align: left;">
public boolean onPrepareOptionsMenu(Menu menu) </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
super.onPrepareOptionsMenu(menu);<br /> </div>
<div style="text-align: left;">
MenuItem menuItem = menu.findItem(MENU_ITEM);<br /> [ ... modify Menu Items ... ]<br /> </div>
<div style="text-align: left;">
return true; </div>
<div style="text-align: left;">
}<br /></div>
</div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-87510225109495275812015-04-13T03:17:00.000-07:002015-04-13T03:17:23.834-07:00Bluetooth Communication Android.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Bluetooth Communications</h3>
<div style="text-align: left;">
The Android Bluetooth communications APIs are wrappers around <b>RFCOMM</b>, the Bluetooth radio frequency communications protocol. RFCOMM supports <b>RS232 </b>serial communication over the <b>Logical Link Control and Adaptation Protocol (L2CAP) </b>layer.<br /> </div>
<div style="text-align: left;">
In practice, this alphabet soup provides a mechanism for opening communication sockets between two paired Bluetooth devices.<br /> </div>
<div style="text-align: left;">
Before your application can communicate between devices, the devices must be paired (bonded). If users attempt to connect two unpaired devices, they will be prompted to pair them before the connection is established.<br /> </div>
<div style="text-align: left;">
You can establish an RFCOMM communication channel for bidirectional communications using the following classes. </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b>BluetoothServerSocket —</b> Used to establish a listening socket for initiating a link between devices. To establish a handshake, one device acts as a server to listen for, and accept, incoming connection requests. </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b>BluetoothSocket</b> — Used to create a new client to connect to a listening Bluetooth Server Socket. Also returned by the Bluetooth Server Socket after a connection is established. </div>
<div style="text-align: left;">
Once a connection is established, Bluetooth Sockets are used by both the server and client to transfer data streams.<br /> </div>
<div style="text-align: left;">
When creating an application that uses Bluetooth as a peer-to-peer transport layer, you’ll need to implement both a Bluetooth Server Socket to listen for connections and a Bluetooth Socket to initiate a new channel and handle communications.</div>
<div style="text-align: left;">
<br /><br />When connected, the Bluetooth Server Socket returns a Bluetooth Socket that’s then used by the server device to send and receive data. This server-side Bluetooth Socket is used in exactly the same way as the client socket. The designations of server and client are relevant only to how the connec- tion is established; they don’t affect how data fl ows after that connection is made.<br /></div>
</div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-7879207067946779052015-04-11T00:31:00.000-07:002015-04-11T00:31:12.004-07:00Custom Toast in Android.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Customizing Toasts</h3>
<div style="text-align: left;">
The standard Toast message window is often sufficient, but in many situations you’ll want to cus- tomize its appearance and screen position. </div>
<div style="text-align: left;">
You can modify a Toast by setting its display position and assigning it alternative Views or layouts.<br /><br />Let’s Make a Toast <br />It shows how to align a Toast to the bottom of the screen using the <b>setGravity</b> method. </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Aligning Toast text<br /> </div>
<div style="text-align: left;">
Context context = this; </div>
<div style="text-align: left;">
String msg = “To the bride and groom!”; </div>
<div style="text-align: left;">
int duration = Toast.LENGTH_SHORT; </div>
<div style="text-align: left;">
Toast toast = Toast.makeText(context, msg, duration); </div>
<div style="text-align: left;">
int offsetX = 0; </div>
<div style="text-align: left;">
int offsetY = 0;<br /> </div>
<div style="text-align: left;">
toast.setGravity(Gravity.BOTTOM, offsetX, offsetY); </div>
<div style="text-align: left;">
toast.show();<br /><br />When a text message just isn’t going to get the job done, you can specify a custom View or layout to use a more complex, or more visual, display. </div>
<div style="text-align: left;">
Using <b>setView</b> on a Toast object, you can specify any View (including a layout) to display using the Toast mechanism. </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
For example, assigns a layout, containing the <b>CompassView Widget</b> along with a TextView, to be dis- played as a Toast.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b>Using Views to customize a Toast</b><br /> </div>
<div style="text-align: left;">
Context context = getApplicationContext(); </div>
<div style="text-align: left;">
String msg = “Cheers!”;</div>
<div style="text-align: left;">
int duration = Toast.LENGTH_LONG; </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Toast toast = Toast.makeText(context, msg, duration); </div>
<div style="text-align: left;">
toast.setGravity(Gravity.TOP, 0, 0);<br /> </div>
<div style="text-align: left;">
LinearLayout ll = new LinearLayout(context); </div>
<div style="text-align: left;">
ll.setOrientation(LinearLayout.VERTICAL);<br /> </div>
<div style="text-align: left;">
TextView myTextView = new TextView(context); </div>
<div style="text-align: left;">
CompassView cv = new CompassView(context);<br />myTextView.setText(msg);<br /> </div>
<div style="text-align: left;">
int lHeight = LinearLayout.LayoutParams.FILL_PARENT; </div>
<div style="text-align: left;">
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;<br /> </div>
<div style="text-align: left;">
ll.addView(cv, new LinearLayout.LayoutParams(lHeight, lWidth)); </div>
<div style="text-align: left;">
ll.addView(myTextView, new LinearLayout.LayoutParams(lHeight, lWidth));<br />ll.setPadding(40, 50, 0, 50);<br /> </div>
<div style="text-align: left;">
toast.setView(ll); </div>
<div style="text-align: left;">
toast.show();<br /><br />The resulting Toast will appear.<br /><br /><b>Using Toasts in Worker Threads</b><br /> </div>
<div style="text-align: left;">
As GUI components, Toasts must be created and shown on the GUI thread; otherwise, you risk throwing a cross-thread exception.A Handler is used to ensure that the Toast is opened on the GUI thread. </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b>Opening a Toast on the GUI thread</b><br /> </div>
<div style="text-align: left;">
Handler handler = new Handler();<br />private void mainProcessing() </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
Thread thread = new Thread(null, doBackgroundThreadProcessing, ”Background”); </div>
<div style="text-align: left;">
thread.start(); }<br /> </div>
<div style="text-align: left;">
private Runnable doBackgroundThreadProcessing = new Runnable() </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
public void run() </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
backgroundThreadProcessing(); </div>
<div style="text-align: left;">
} </div>
<div style="text-align: left;">
};<br /><br />private void backgroundThreadProcessing() </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
handler.post(doUpdateGUI); }<br /> </div>
<div style="text-align: left;">
// Runnable that executes the update GUI method. </div>
<div style="text-align: left;">
private Runnable doUpdateGUI = new Runnable() </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
public void run() </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
Context context = getApplicationContext(); </div>
<div style="text-align: left;">
String msg = “To open mobile development!”; </div>
<div style="text-align: left;">
int duration = Toast.LENGTH_SHORT; </div>
<div style="text-align: left;">
Toast.makeText(context, msg, duration).show(); </div>
<div style="text-align: left;">
} </div>
<div style="text-align: left;">
};<br /></div>
</div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-16957177187370929652015-04-08T01:52:00.001-07:002015-04-08T01:52:22.745-07:00Bind Service to Activity in Android.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Binding Activities to Services</h3>
<div style="text-align: left;">
So far, you have seen how services are created and how they are called and terminated when they are done with their task. </div>
<div style="text-align: left;">
All the services that you have seen are simple — either they start with a counter and increment at regular intervals, or they download a fixed set of files from the Internet. </div>
<div style="text-align: left;">
However, real-world services are usually much more sophisticated, requiring the passing of data so that they can do the job correctly for you. </div>
<div style="text-align: left;">
Using the service demonstrated earlier that downloads a set of files, suppose you now want to let the calling activity determine what files to download, instead of hardcoding them in the service. </div>
<div style="text-align: left;">
Here is what you need to do. First, in the calling activity, you create an Intent object, specifying the service name:</div>
<div style="text-align: left;">
<br />Button btnStart=(Button)findViewById(R.id.btnStartService); </div>
<div style="text-align: left;">
btnStart.setOnClickListener(newView.OnClickListener()</div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
public void onClick(View v)</div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
Intent intent = new Intent(getBaseContext(), MyService.class); </div>
<div style="text-align: left;">
} </div>
<div style="text-align: left;">
});<br /> </div>
<div style="text-align: left;">
You then create an array of URL objects and assign it to the Intent object through its <b>putExtra()</b> method. </div>
<div style="text-align: left;">
Finally, you start the service using the Intent object:<br /> </div>
<div style="text-align: left;">
Button btnStart=(Button)findViewById(R.id.btnStartService); </div>
<div style="text-align: left;">
btnStart.setOnClickListener(new View.OnClickListener()</div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
public void onClick(View v)</div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
Inten tintent=newIntent(getBaseContext(),MyService.class); </div>
<div style="text-align: left;">
try </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
URL[] urls = new URL[] </div>
<div style="text-align: left;">
{<br />new URL(“http://www.amazon.com/somefiles.pdf”), </div>
<div style="text-align: left;">
new URL(“http://www.wrox.com/somefiles.pdf”), </div>
<div style="text-align: left;">
new URL(“http://www.google.com/somefiles.pdf”), </div>
<div style="text-align: left;">
new URL(“http://emergingandroidtech.blogspot.in/somefiles.pdf”)}; </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
intent.putExtra(“URLs”, urls); </div>
<div style="text-align: left;">
} </div>
<div style="text-align: left;">
catch (MalformedURLException e) </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
e.printStackTrace(); </div>
<div style="text-align: left;">
} </div>
<div style="text-align: left;">
startService(intent); </div>
<div style="text-align: left;">
} </div>
<div style="text-align: left;">
});<br /> </div>
<div style="text-align: left;">
Note that the URL array is assigned to the Intent object as an Object array. </div>
<div style="text-align: left;">
On the service’s end, you need to extract the data passed in through the Intent object in the <b>onStartCommand()</b> method: <br /> </div>
<div style="text-align: left;">
@Override </div>
<div style="text-align: left;">
public int onStartCommand(Intent intent,int flags,int startId)</div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
//We want this service to continue running until it is explicitly </div>
<div style="text-align: left;">
//stopped,so return sticky. </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Toast.makeText(this,“ServiceStarted”,Toast.LENGTH_LONG).show();<br /></div>
<div style="text-align: left;">
Object [] objUrls = (Object []) intent.getExtras().get(“URLs”); </div>
<div style="text-align: left;">
URL [] urls = new URL[objUrls.length]; </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
for (int i=0; i<objUrls.length-1; i++) </div>
<div style="text-align: left;">
{ </div>
<div style="text-align: left;">
urls [i] = (URL) objUrls[i]; </div>
<div style="text-align: left;">
} </div>
<div style="text-align: left;">
new DoBackgroundTask().execute(urls); </div>
<div style="text-align: left;">
return START_STICKY; </div>
<div style="text-align: left;">
}<br /> </div>
<div style="text-align: left;">
The preceding first extracts the data using the <b>getExtras() </b>method to return a Bundle object. </div>
<div style="text-align: left;">
It then uses the<b> get() </b>method to extract out the URL array as an Object array. </div>
<div style="text-align: left;">
Because in Java you cannot directly cast an array from one type to another, you have to create a loop and cast each member of the array individually. </div>
<div style="text-align: left;">
Finally, you execute the background task by passing the URL array into the <b>execute() </b>method. </div>
<div style="text-align: left;">
This is one way in which your activity can pass values to the service. </div>
<div style="text-align: left;">
As you can see, if you have relatively complex data to pass to the service, you have to do some additional work to ensure that the data is passed correctly. </div>
<div style="text-align: left;">
A better way to pass data is to bind the activity directly to the service so that the activity can call any public members and methods on the service directly. </div>
</div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com2tag:blogger.com,1999:blog-8784159420963262332.post-77833637928963660902015-04-06T02:12:00.001-07:002015-04-06T02:29:11.612-07:00Communication between Service and Activity in Android.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Communicating Between A Service And An Activity</h3>
<br />
Often a service simply executes in its own thread, independently of the activity that calls it.<br />
This doesn’t pose any problem if you simply want the service to perform some tasks periodically and the activity does not need to be notified of the status of the service.<br />
<br />
For example, you may have a service that periodically logs the geographical location of the device to a database.<br />
In this case, there is no need for your service to interact with any activities, because its main purpose is to save the coordinates into a database.<br />
However, suppose you want to monitor for a particular location. When the service logs an address that is near the location you are monitoring, it might need to communicate that information to the activity. In this case, you would need to devise a way for the service to interact with the activity.<br />
The following example demonstrates how a service can communicate with an activity using a BroadcastReceiver. <br />
<br />
<h4 style="text-align: left;">
Invoking an Activity from a Service</h4>
<b>1 . Using the same project created in the previous post, add the following statements in bold to the MyIntentService.java file:</b><br />
<br />
package com.emergingandroidtech.Services;<br />
import java.net.MalformedURLException;<br />
import java.net.URL;<br />
import android.app.IntentService;<br />
import android.content.Intent;<br />
import android.util.Log;<br />
<br />
public class MyIntentService extends IntentService<br />
{<br />
public MyIntentService()<br />
{<br />
super(“MyIntentServiceName”);<br />
}<br />
<br />
@Override <br />
protected void onHandleIntent(Intent intent)<br />
{<br />
try<br />
{<br />
int result= DownloadFile(newURL(“http://www.amazon.com/somefile.pdf”)); <br />
Log.d(“IntentService”,“Downloaded“+result+“bytes”);<br />
<br />
//---send a broadcast to inform the activity<br />
// that the file has been downloaded--- <br />
Intent broadcastIntent = new Intent(); broadcastIntent.setAction(“FILE_DOWNLOADED_ACTION”); getBaseContext().sendBroadcast(broadcastIntent);<br />
}<br />
catch(MalformedURLException e)<br />
{<br />
e.printStackTrace(); <br />
}<br />
}<br />
<br />
private int DownloadFile(URL url)<br />
{<br />
try<br />
{<br />
//---simulate taking sometime to download a file--- <br />
Thread.sleep(5000);<br />
}<br />
catch(InterruptedException e)<br />
{<br />
e.printStackTrace();<br />
}<br />
return 100;<br />
}<br />
} <br />
<br />
<b>2 . Add the following statements in bold to the MainActivity.java file: </b><br />
<br />
package com.emergingandroidtech.Services;<br />
import android.app.Activity;<br />
import android.content.BroadcastReceiver;<br />
import android.content.Context;<br />
import android.content.Intent;<br />
import android.os.Bundle;<br />
import android.view.View;<br />
import android.widget.Button;<br />
import android.widget.Toast;<br />
import android.content.IntentFilter;<br />
<br />
public class MainActivity extends Activity<br />
{<br />
IntentFilter intentFilter;<br />
<br />
/** Called when the activity is first created.*/<br />
@Override<br />
public void onCreate(Bundle savedInstanceState)<br />
{<br />
super.onCreate(savedInstanceState); <br />
setContentView(R.layout.main);<br />
<br />
//---intent to filter for file downloaded intent--- <br />
intentFilter = new IntentFilter(); <br />
intentFilter.addAction(“FILE_DOWNLOADED_ACTION”);<br />
<br />
//---register the receiver---<br />
registerReceiver(intentReceiver, intentFilter);<br />
<br />
Button btnStart=(Button)findViewById(R.id.btnStartService);<br />
btnStart.setOnClickListener(new View.OnClickListener()<br />
{ <br />
public void onClick(View v)<br />
{<br />
//startService(newIntent(getBaseContext(),MyService.class)); <br />
startService(newIntent(getBaseContext(),MyIntentService.class));<br />
}<br />
});<br />
<br />
Button btnStop=(Button)findViewById(R.id.btnStopService);<br />
btnStop.setOnClickListener(new View.OnClickListener()<br />
{<br />
public void onClick(View v)<br />
{<br />
stopService(newIntent(getBaseContext(),MyService.class)); <br />
}<br />
});<br />
}<br />
<br />
private BroadcastReceiver intentReceiver = new BroadcastReceiver()<br />
{<br />
@Override<br />
public void onReceive(Context context, Intent intent)<br />
{<br />
Toast.makeText(getBaseContext(), “File downloaded!”, Toast.LENGTH_LONG).show(); <br />
}<br />
};<br />
} <br />
<br />
<b>3 . Click the Start Service button. After about five seconds, the Toast class will display a message indicating that the file has been downloaded.</b><br />
<br />
<i><b>How It Works </b></i><br />
<br />
To notify an activity when a service has finished its execution, you broadcast an intent using the <b>sendBroadcast()</b> method:<br />
<br />
@Override<br />
protected void onHandleIntent(Intent intent)<br />
{<br />
try<br />
{<br />
int result= DownloadFile(new URL(“http://www.amazon.com/somefile.pdf”));<br />
Log.d(“IntentService”,“Downloaded“+result+“bytes”);<br />
<br />
//---send a broadcast to inform the activity <br />
// that the file has been downloaded--- <br />
Intent broadcastIntent = new Intent(); broadcastIntent.setAction(“FILE_DOWNLOADED_ACTION”); getBaseContext().sendBroadcast(broadcastIntent); <br />
}<br />
catch(MalformedURLException e)<br />
{<br />
e.printStackTrace(); <br />
}<br />
}<br />
<br />
The action of this intent that you are broadcasting is set to “FILE_DOWNLOADED_ACTION”, which means any activity that is listening for this intent will be invoked.<br />
Hence, in your MainActivity.java file, you listen for this intent using the<b> registerReceiver() </b>method from the IntentFilter class:<br />
<br />
@Override<br />
public void onCreate(Bundle savedInstanceState)<br />
{<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br />
<br />
//---intent to filter for file downloaded intent--- <br />
intentFilter = new IntentFilter(); <br />
intentFilter.addAction(“FILE_DOWNLOADED_ACTION”); <br />
<br />
//---register the receiver---<br />
registerReceiver(intentReceiver, intentFilter);<br />
... ... }<br />
<br />
When the intent is received, it invokes an instance of the BroadcastReceiver class that you have defi ned:<br />
<br />
private BroadcastReceiver intentReceiver=new BroadcastReceiver()<br />
{<br />
@Override <br />
public void onReceive(Context context,Intent intent)<br />
{ <br />
Toast.makeText(getBaseContext(),“Filedownloaded!”, Toast.LENGTH_LONG).show();<br />
} <br />
};<br />
}<br />
<br />
In this case, you displayed the message “File downloaded!” Of course, if you need to pass some data from the service to the activity, you can make use of the Intent object. </div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-8808639836306655092015-04-03T04:31:00.000-07:002015-04-03T04:31:14.485-07:00IntentService in Android example.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Executing Asynchronous tasks on Separate threads using intentService </h3>
<br />
In earlier posts, you saw how to start a service using the<b> startService() </b>method and stop a service using the <b>stopService()</b> method.<br />
You have also seen how you should execute long-running task on a separate thread — not the same thread as the calling activities.<br />
It is important to note that once your service has finished executing a task, it should be stopped as soon as possible so that it does not unnecessarily hold up valuable resources.<br />
That’s why you use the<b> stopSelf() </b>method to stop the service when a task has been completed. Unfortunately, a lot of developers often forgot to terminate the service when it is done performing its task.<br />
To easily create a service that runs a task asynchronously and terminates itself when it is done, you can use the <b>IntentService</b> class.<br />
<br />
The<b> IntentService</b> class is a base class for Service that handles asynchronous requests on demand.<br />
It is started just like a normal service and it executes its task within a worker thread and terminates itself when the task is completed.<br />
<br />
The following example demonstrates how to use the <b>IntentService</b> class.<br /><br />
<h4 style="text-align: left;">
Using the intentService Class to Auto-Stop a Service</h4>
<br />
<b>1. Add a new class file named MyIntentService.java.</b><br />
<br />
<b>2 . Populate the MyIntentService.java file as follows: </b><br />
<br />
package com.emergingandroidtech.Services;<br /><br />import java.net.MalformedURLException;<br />import java.net.URL;<br /><br />import android.app.IntentService;<br />import android.content.Intent;<br />import android.util.Log;<br /><br />public class MyIntentService extends IntentService {<br /> <br /> public MyIntentService() {<br /> super("MyIntentServiceName");<br /> } <br /> <br /> @Override <br /> protected void onHandleIntent(Intent intent) { <br /> try {<br /> int result = <br /> DownloadFile(new URL("http://www.amazon.com/somefile.pdf"));<br /> Log.d("IntentService", "Downloaded " + result + " bytes");<br /> <br /> //---send a broadcast to inform the activity <br /> // that the file has been downloaded---<br /> Intent broadcastIntent = new Intent();<br /> broadcastIntent.setAction("FILE_DOWNLOADED_ACTION"); <br /> getBaseContext().sendBroadcast(broadcastIntent);<br /><br /> } catch (MalformedURLException e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> <br /> private int DownloadFile(URL url) { <br /> try {<br /> //---simulate taking some time to download a file---<br /> Thread.sleep(5000);<br /> } catch (InterruptedException e) {<br /> // TODO Auto-generated catch block<br /> e.printStackTrace();<br /> } <br /> return 100;<br /> } <br />}<br />
<br />
<b>3 . Add the following statement in bold to the AndroidManifest.xml file: </b><br />
<br />
<!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
</o:OfficeDocumentSettings>
</xml><![endif]--><br />
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val="Cambria Math"/>
<m:brkBin m:val="before"/>
<m:brkBinSub m:val="--"/>
<m:smallFrac m:val="off"/>
<m:dispDef/>
<m:lMargin m:val="0"/>
<m:rMargin m:val="0"/>
<m:defJc m:val="centerGroup"/>
<m:wrapIndent m:val="1440"/>
<m:intLim m:val="subSup"/>
<m:naryLim m:val="undOvr"/>
</m:mathPr></w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
DefSemiHidden="true" DefQFormat="false" DefPriority="99"
LatentStyleCount="267">
<w:LsdException Locked="false" Priority="0" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Normal"/>
<w:LsdException Locked="false" Priority="9" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="heading 1"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/>
<w:LsdException Locked="false" Priority="39" Name="toc 1"/>
<w:LsdException Locked="false" Priority="39" Name="toc 2"/>
<w:LsdException Locked="false" Priority="39" Name="toc 3"/>
<w:LsdException Locked="false" Priority="39" Name="toc 4"/>
<w:LsdException Locked="false" Priority="39" Name="toc 5"/>
<w:LsdException Locked="false" Priority="39" Name="toc 6"/>
<w:LsdException Locked="false" Priority="39" Name="toc 7"/>
<w:LsdException Locked="false" Priority="39" Name="toc 8"/>
<w:LsdException Locked="false" Priority="39" Name="toc 9"/>
<w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/>
<w:LsdException Locked="false" Priority="10" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Title"/>
<w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/>
<w:LsdException Locked="false" Priority="11" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/>
<w:LsdException Locked="false" Priority="22" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Strong"/>
<w:LsdException Locked="false" Priority="20" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/>
<w:LsdException Locked="false" Priority="59" SemiHidden="false"
UnhideWhenUsed="false" Name="Table Grid"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/>
<w:LsdException Locked="false" Priority="1" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 1"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 1"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 1"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/>
<w:LsdException Locked="false" Priority="34" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/>
<w:LsdException Locked="false" Priority="29" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Quote"/>
<w:LsdException Locked="false" Priority="30" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 1"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 1"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 2"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 2"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 2"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 2"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 2"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 3"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 3"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 3"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 3"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 3"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 4"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 4"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 4"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 4"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 4"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 5"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 5"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 5"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 5"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 5"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 6"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 6"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 6"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 6"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 6"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/>
<w:LsdException Locked="false" Priority="19" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/>
<w:LsdException Locked="false" Priority="21" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/>
<w:LsdException Locked="false" Priority="31" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/>
<w:LsdException Locked="false" Priority="32" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/>
<w:LsdException Locked="false" Priority="33" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Book Title"/>
<w:LsdException Locked="false" Priority="37" Name="Bibliography"/>
<w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/>
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
</style>
<![endif]-->
<br />
<div class="MsoNormal">
<?xml version="1.0"
encoding="utf-8"?></div>
<div class="MsoNormal">
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>package="com.emergingandroidtech.Services"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span>android:versionCode="1"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:versionName="1.0"></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span><application
android:icon="@drawable/icon"
android:label="@string/app_name"></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span><activity
android:name=".MainActivity"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:label="@string/app_name"></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span><intent-filter></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span><action android:name="android.intent.action.MAIN" /></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span><category android:name="android.intent.category.LAUNCHER"
/></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span></intent-filter></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span></activity></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span><service
android:name=".MyService"></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span><intent-filter></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span><action android:name="com.emergingandroidtech.MyService"
/><span style="mso-spacerun: yes;"> </span></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span></intent-filter></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span></service></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span><service
android:name=".MyIntentService" /></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span></application></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span><uses-sdk
android:minSdkVersion="9" /></div>
<div class="MsoNormal">
<uses-permission
android:name="android.permission.INTERNET"></uses-permission></div>
<div class="MsoNormal">
</manifest></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b> 4. Add the following in main.xml file.</b></div>
<div class="MsoNormal">
<!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
</o:OfficeDocumentSettings>
</xml><![endif]--></div>
<br />
<div class="MsoNormal">
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val="Cambria Math"/>
<m:brkBin m:val="before"/>
<m:brkBinSub m:val="--"/>
<m:smallFrac m:val="off"/>
<m:dispDef/>
<m:lMargin m:val="0"/>
<m:rMargin m:val="0"/>
<m:defJc m:val="centerGroup"/>
<m:wrapIndent m:val="1440"/>
<m:intLim m:val="subSup"/>
<m:naryLim m:val="undOvr"/>
</m:mathPr></w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
DefSemiHidden="true" DefQFormat="false" DefPriority="99"
LatentStyleCount="267">
<w:LsdException Locked="false" Priority="0" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Normal"/>
<w:LsdException Locked="false" Priority="9" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="heading 1"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/>
<w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/>
<w:LsdException Locked="false" Priority="39" Name="toc 1"/>
<w:LsdException Locked="false" Priority="39" Name="toc 2"/>
<w:LsdException Locked="false" Priority="39" Name="toc 3"/>
<w:LsdException Locked="false" Priority="39" Name="toc 4"/>
<w:LsdException Locked="false" Priority="39" Name="toc 5"/>
<w:LsdException Locked="false" Priority="39" Name="toc 6"/>
<w:LsdException Locked="false" Priority="39" Name="toc 7"/>
<w:LsdException Locked="false" Priority="39" Name="toc 8"/>
<w:LsdException Locked="false" Priority="39" Name="toc 9"/>
<w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/>
<w:LsdException Locked="false" Priority="10" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Title"/>
<w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/>
<w:LsdException Locked="false" Priority="11" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/>
<w:LsdException Locked="false" Priority="22" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Strong"/>
<w:LsdException Locked="false" Priority="20" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/>
<w:LsdException Locked="false" Priority="59" SemiHidden="false"
UnhideWhenUsed="false" Name="Table Grid"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/>
<w:LsdException Locked="false" Priority="1" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 1"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 1"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 1"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/>
<w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/>
<w:LsdException Locked="false" Priority="34" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/>
<w:LsdException Locked="false" Priority="29" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Quote"/>
<w:LsdException Locked="false" Priority="30" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 1"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 1"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 2"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 2"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 2"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 2"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 2"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 3"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 3"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 3"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 3"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 3"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 4"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 4"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 4"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 4"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 4"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 5"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 5"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 5"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 5"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 5"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/>
<w:LsdException Locked="false" Priority="60" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Shading Accent 6"/>
<w:LsdException Locked="false" Priority="61" SemiHidden="false"
UnhideWhenUsed="false" Name="Light List Accent 6"/>
<w:LsdException Locked="false" Priority="62" SemiHidden="false"
UnhideWhenUsed="false" Name="Light Grid Accent 6"/>
<w:LsdException Locked="false" Priority="63" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/>
<w:LsdException Locked="false" Priority="64" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/>
<w:LsdException Locked="false" Priority="65" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/>
<w:LsdException Locked="false" Priority="66" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/>
<w:LsdException Locked="false" Priority="67" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/>
<w:LsdException Locked="false" Priority="68" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/>
<w:LsdException Locked="false" Priority="69" SemiHidden="false"
UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/>
<w:LsdException Locked="false" Priority="70" SemiHidden="false"
UnhideWhenUsed="false" Name="Dark List Accent 6"/>
<w:LsdException Locked="false" Priority="71" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/>
<w:LsdException Locked="false" Priority="72" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful List Accent 6"/>
<w:LsdException Locked="false" Priority="73" SemiHidden="false"
UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/>
<w:LsdException Locked="false" Priority="19" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/>
<w:LsdException Locked="false" Priority="21" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/>
<w:LsdException Locked="false" Priority="31" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/>
<w:LsdException Locked="false" Priority="32" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/>
<w:LsdException Locked="false" Priority="33" SemiHidden="false"
UnhideWhenUsed="false" QFormat="true" Name="Book Title"/>
<w:LsdException Locked="false" Priority="37" Name="Bibliography"/>
<w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/>
</w:LatentStyles>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
</style>
<![endif]-->
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<?xml version="1.0"
encoding="utf-8"?></div>
<div class="MsoNormal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:orientation="vertical"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:layout_width="fill_parent"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:layout_height="fill_parent"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span>></div>
<div class="MsoNormal">
<Button android:id="@+id/btnStartService"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:layout_width="fill_parent" </div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:layout_height="wrap_content"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:text="Start Service" /></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span></div>
<div class="MsoNormal">
<Button android:id="@+id/btnStopService"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:layout_width="fill_parent" </div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span>android:layout_height="wrap_content"</div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;">
</span>android:text="Stop Service" /></div>
<div class="MsoNormal">
<span style="mso-spacerun: yes;"> </span></div>
<div class="MsoNormal">
</LinearLayout></div>
<br />
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>5 . Add the following statement in bold to the MainActivity.java file: </b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
public class MainActivity extends Activity</div>
<div class="MsoNormal">
{ </div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
/**Called when the activity is first created.*/ </div>
<div class="MsoNormal">
@Override </div>
<div class="MsoNormal">
public void onCreate(Bundle savedInstanceState)</div>
<div class="MsoNormal">
{ </div>
<div class="MsoNormal">
super.onCreate(savedInstanceState); </div>
<div class="MsoNormal">
setContentView(R.layout.main);<br /></div>
<div class="MsoNormal">
Button btnStart=(Button)findViewById(R.id.btnStartService); </div>
<div class="MsoNormal">
btnStart.setOnClickListener(newView.OnClickListener()</div>
<div class="MsoNormal">
{ </div>
<div class="MsoNormal">
public void onClick(View v)</div>
<div class="MsoNormal">
{ </div>
<div class="MsoNormal">
//startService(new Intent(getBaseContext(), MyService.class)); </div>
<div class="MsoNormal">
startService(new Intent(getBaseContext(), MyIntentService.class)); </div>
<div class="MsoNormal">
} </div>
<div class="MsoNormal">
});<br /></div>
<div class="MsoNormal">
Button btnStop=(Button)findViewById(R.id.btnStopService); </div>
<div class="MsoNormal">
btnStop.setOnClickListener(newView.OnClickListener()</div>
<div class="MsoNormal">
{ </div>
<div class="MsoNormal">
public void onClick(View v)</div>
<div class="MsoNormal">
{ </div>
<div class="MsoNormal">
stopService(newIntent(getBaseContext(),MyService.class)); </div>
<div class="MsoNormal">
} </div>
<div class="MsoNormal">
}); </div>
<div class="MsoNormal">
} </div>
<div class="MsoNormal">
} <br /> </div>
<div class="MsoNormal">
<b>5 . Click the Start Service button. After about five seconds, you should observe the following statement in the LogCat window:</b><br /> </div>
<div class="MsoNormal">
01-1703:05:21.244:DEBUG/IntentService(692): Downloaded 100bytes</div>
</div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-42948957119628419922015-03-30T22:22:00.001-07:002015-03-30T22:22:59.902-07:00Run repeatedly task using timer class in android.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Performing repeated tasks in a Service</h3>
Besides performing long-running tasks in a service, you might also perform some repeated tasks in a service. For example, you may write an alarm clock service that runs persistently in the background. In this case, your service may need to periodically execute some code to check whether a prescheduled time has been reached so that an alarm can be sounded. To execute a block of code to be executed at a regular time interval, you can use the Timer class within your service.<br />
<br />
The following Example shows you how. <br /><br />
<h4 style="text-align: left;">
Running Repeated Tasks Using the Timer Class</h4>
<h4 style="text-align: left;">
1 . Using the same project created in the previous post, add the following statements in bold to the MyService.java file:</h4>
package com.emergingandroidtech.Services;<br />import android.app.Service;<br />
import android.content.Intent;<br />
import android.os.AsyncTask;<br />
import android.os.IBinder;<br />
import android.util.Log;<br />
import android.widget.Toast;<br />
import java.net.URL;<br />import java.util.Timer;<br />
import java.util.TimerTask;<br /><br />
public class MyService extends Service<br />
{<br />
int counter = 0;<br />
static final int UPDATE_INTERVAL = 1000;<br />
private Timer timer = new Timer();<br /><br />
@Override<br />
public IBinder onBind(Intent arg0)<br />
{<br />
return null;<br />
}<br /><br />
@Override<br />
public int onStartCommand(Intent intent,int flags,int startId)<br />
{<br />
//We want this service to continuer unning until it is explicitly<br />
//stopped,sor eturn sticky. <br />
Toast.makeText(this,“ServiceStarted”,Toast.LENGTH_LONG).show();<br />
<br />
doSomethingRepeatedly();<br />
<br />
return START_STICKY; <br />
}<br /><br />
private void doSomethingRepeatedly()<br />
{<br />
timer.scheduleAtFixedRate( new TimerTask()<br />
{<br />
public void run()<br />
{<br />
Log.d(“MyService”, String.valueOf(++counter));<br />
}<br />
},<br />
0,<br />
UPDATE_INTERVAL);<br />
}<br /><br />
@Override<br />
public void onDestroy()<br />
{ <br />
super.onDestroy();<br />
if (timer != null)<br />
{<br />
timer.cancel(); <br />
}<br />
Toast.makeText(this,“ServiceDestroyed”,Toast.LENGTH_LONG).show(); <br />
}<br />
} <br /><br />
<b>2 . Click the Start Service button.</b> <br />
<br />
<b>3 . Observe the output displayed in the LogCat window: </b><br />
<br />
01-1615:12:04.364:DEBUG/MyService(495):1<br />
01-1615:12:05.384:DEBUG/MyService(495):2<br />
01-1615:12:06.386:DEBUG/MyService(495):3<br />
01-1615:12:07.389:DEBUG/MyService(495):4<br />
01-1615:12:08.364:DEBUG/MyService(495):5<br />
01-1615:12:09.427:DEBUG/MyService(495):6<br />
01-1615:12:10.374:DEBUG/MyService(495):7<br />
<br />
<i><b>How It Works</b></i><br />
<br />
In this example, you created a Timer object and called its <b>scheduleAtFixedRate()</b> method inside the <b>doSomethingRepeatedly() </b>method that you have defined:<br /><br />
private void doSomethingRepeatedly()<br />
{<br />
timer.scheduleAtFixedRate(newTimerTask()<br />
{<br />
public void run()<br />
{<br />
Log.d(“MyService”,String.valueOf(++counter));<br />
}<br />
},<br />
0,<br />
UPDATE_INTERVAL);<br />
}<br /><br />
You passed an instance of the TimerTask class to the <b>scheduleAtFixedRate()</b> method so that you can execute the block of code within the <b>run()</b> method repeatedly. The second parameter to the <b>scheduleAtFixedRate() </b>method specifies the amount of time, in milliseconds, before first execution. The third parameter specifies the amount of time, in milliseconds, between subsequent executions. In the preceding example, you essentially print out the value of the counter every one second (1,000 milliseconds). The service repeatedly prints the value of counter until the service is terminated:<br /><br />
@Override <br />
public void onDestroy()<br />
{<br />
super.onDestroy();<br />
if(timer!=null)<br />
{<br />
timer.cancel(); <br />
} <br />
Toast.makeText(this,“ServiceDestroyed”,Toast.LENGTH_LONG).show(); <br />
}<br /><br />
For the <b>scheduleAtFixedRate()</b> method, your code is executed at fixed time intervals, regardless of how long each task takes. For example, if the code within your <b>run() </b>method takes two seconds to complete, then your second task will start immediately after the first task has ended. Similarly, if your delay is set to three seconds and the task takes two seconds to complete, then the second task will wait for one second before starting. <br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-15206071039116632082015-03-30T00:01:00.000-07:002015-03-31T02:10:59.707-07:00How to use Async Task in a Service in android ?<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Performing Tasks in a Service Asynchronously</h3>
<br />
<b>1 . Using the same project created in the previous post, add the following statements in bold to the MyService.java file:<br /> </b><br />
package com.emergingandroidtech.Services;<br />
import android.app.Service;<br />
import android.content.Intent;<br />
import android.os.IBinder;<br />
import android.util.Log;<br />
import android.widget.Toast;<br />
import java.net.MalformedURLException;<br />
import java.net.URL;<br />
import android.os.AsyncTask;<br />
<br />
public class MyService extends Service<br />
{<br />
@Override<br />
public IBinder onBind(Intent arg0)<br />
{<br />
return null;<br />
}<br />
<br />
@Override <br />
public int onStartCommand(Intent intent,int flags,int startId)<br />
{<br />
<br />
//We want this service to continue running until it is explicitly<br />
//stopped,so return sticky. <br />
Toast.makeText(this,“ServiceStarted”,Toast.LENGTH_LONG).show();<br />
try<br />
{<br />
new DoBackgroundTask().execute(<br />
new URL(“http://www.amazon.com/somefiles.pdf”),<br />
new URL(“http://www.google.com/somefiles.pdf”),<br />
new URL(“http://emergingandroidtech.blogspot.in”)); <br />
}<br />
catch (MalformedURLException e)<br />
{<br />
e.printStackTrace();<br />
}<br />
return START_STICKY; <br />
}<br />
<br />
@Override<br />
public void onDestroy()<br />
{<br />
super.onDestroy();<br />
Toast.makeText(this,“ServiceDestroyed”,Toast.LENGTH_LONG).show(); <br />
} <br />
<br />
private int DownloadFile(URL url)<br />
{<br />
try<br />
{<br />
//---simulate taking sometime to download a file--- <br />
Thread.sleep(5000);<br />
}<br />
catch(InterruptedException e)<br />
{<br />
e.printStackTrace(); <br />
}<br />
//---return an arbitrary number representing <br />
//the size of the file downloaded--- <br />
return 100; <br />
}<br />
<br />
private class DoBackgroundTask extends AsyncTask<URL, Integer, Long><br />
{<br />
protected Long doInBackground(URL... urls)<br />
{<br />
int count = urls.length;<br />
long totalBytesDownloaded = 0;<br />
for (int i = 0; i < count; i++)<br />
{<br />
totalBytesDownloaded += DownloadFile(urls[i]);<br />
<br />
//---calculate percentage downloaded and<br />
// report its progress--- <br />
publishProgress((int) (((i+1) / (float) count) * 100)); <br />
}<br />
return totalBytesDownloaded; <br />
}<br />
<br />
protected void onProgressUpdate(Integer... progress)<br />
{<br />
Log.d(“Downloading files”, String.valueOf(progress[0]) + “% downloaded”); <br />
<br />
Toast.makeText(getBaseContext(), String.valueOf(progress[0]) + “% downloaded”, Toast.LENGTH_LONG).show(); <br />
}<br />
<br />
protected void onPostExecute(Long result)<br />
{<br />
Toast.makeText(getBaseContext(), “Downloaded “ + result + “ bytes”, Toast.LENGTH_LONG).show(); <br />
stopSelf();<br />
} <br />
}<br />
} <br />
<br />
<b>2. Click the Start Service button. </b><br />
<b>The Toast class will display a message indicating what percentage of the download is completed. </b><br />
<b>You should see four of them: 25%, 50%, 75%, and 100%. </b><br />
<br />
<b>3 . You will also observe the following output in the LogCat window: </b><br />
<br />
01-1602:56:29.051:DEBUG/Downloadingfiles(8844):25%downloaded<br />
01-1602:56:34.071:DEBUG/Downloadingfiles(8844):50%downloaded<br />
01-1602:56:39.106:DEBUG/Downloadingfiles(8844):75%downloaded<br />
01-1602:56:44.173:DEBUG/Downloadingfiles(8844):100%downloaded<br />
<br />
<i><b>How It Works</b></i><br />
<br />
This example illustrates one way in which you can execute a task asynchronously within your service. You do so by creating an inner class that extends the AsyncTask class. The AsyncTask class enables you to perform background execution without needing to manually handle threads and handlers. The DoBackgroundTask class extends the AsyncTask class by specifying three generic types:<br />
<br />
private class DoBackgroundTask extends AsyncTask<URL,Integer,Long>{<br />
<br />
In this case, the three types specified are URL, Integer and Long. These three types specify the data type used by the following three methods that you implement in an AsyncTask class:<br />
<br />
➤ <b>doInBackground()</b>— This method takes an array of the first generic type specified earlier. In this case, the type is URL. This method is executed in the background thread and is where you put your long-running code. To report the progress of your task, you call the <b>publishProgress()</b> method, which invokes the next method, <b>onProgressUpdate()</b>, which you implement in an AsyncTask class. The return type of this method takes the third generic type specified earlier, which is Long in this case.<br />
<br />
➤ <b>onProgressUpdate()</b>— This method is invoked in the UI thread and is called when you call the <b>publishProgress()</b> method. It takes an array of the second generic type specified earlier. In this case, the type is Integer. Use this method to report the progress of the background task to the user.<br />
<br />
➤ <b>onPostExecute()</b>— This method is invoked in the UI thread and is called when the <b>doInBackground()</b> method has finished execution. This method takes an argument of the third generic type specified earlier, which in this case is a Long.<br />
<br />
To download multiple files in the background, you created an instance of the DoBackgroundTask class and then called its <b>execute()</b> method by passing in an array of URLs:<br />
<br />
try<br />
{<br />
newDoBackgroundTask().execute(<br />
newURL(“http://www.amazon.com/somefiles.pdf”),<br />
newURL(“http://www.google.com/somefiles.pdf”),<br />
newURL(“http://emergingandroidtech.blogspot.in”)); <br />
}<br />
catch(MalformedURLException e)<br />
{<br />
//TODO Auto-generated catch block <br />
e.printStackTrace(); <br />
}<br />
<br />
The preceding causes the service to download the files in the background, and reports the progress as a percentage of files downloaded. More important, the activity remains responsive while the files are downloaded in the background, on a separate thread.<br />
Note that when the background thread has finished execution, you need to manually call the <b>stopSelf()</b> method to stop the service:<br />
<br />
protected void onPostExecute(Long result)<br />
{<br />
Toast.makeText(getBaseContext(), “Downloaded“+result+“bytes”, Toast.LENGTH_LONG).show(); stopSelf(); <br />
}<br />
<br />
The <b>stopSelf()</b> method is the equivalent of calling the <b>stopService() </b>method to stop the service.</div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-83855231818157082492015-03-27T03:25:00.001-07:002015-03-27T03:25:14.266-07:00How to create your own service in android ?<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Creating your Own Services</h3>
<div style="text-align: left;">
The best way to understand how a service works is by creating one. The following example shows you the steps to create a simple service. </div>
<div style="text-align: left;">
For now, you will learn how to start and stop a service. <br /> </div>
<h4 style="text-align: left;">
Creating a Simple Service</h4>
<div style="text-align: left;">
<br /><b>1 . Using Eclipse, create a new Android project and name it Services.</b></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b>2 . Add a new class file to the project and name it MyService.java. Populate it with the following code: </b></div>
<br />
package com.emergingandroidtech.Services;<br /><br />
import android.app.Service;<br />
import android.content.Intent;<br />
import android.os.IBinder;<br />
import android.widget.Toast;<br /><br />
public class MyService extends Service {<br /><br />
@Override<br />
public IBinder onBind(Intent arg0)<br />
{<br />
return null;<br />
}<br /><br />
@Override <br />
public int onStartCommand(Intent intent, int flags, int startId)<br />
{ <br />
// We want this service to continue running until it is explicitly<br />
// stopped, so return sticky.<br />
Toast.makeText(this, “Service Started”, Toast.LENGTH_LONG).show();<br />
return START_STICKY; <br />
} <br />
<br />
@Override <br />
public void onDestroy()<br />
{<br />
super.onDestroy();<br />
Toast.makeText(this, “Service Destroyed”, Toast.LENGTH_LONG).show(); <br />
}<br />
} <br /><br />
<b>3 . In the AndroidManifest.xml file, add the following statement in bold: </b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<manifest<br />
xmlns:android=”http://schemas.android.com/apk/res/android”<br />
package=”com.emergingandroidtech.Services”<br />
android:versionCode=”1”<br />
android:versionName=”1.0”> <br />
<br />
<application<br />
android:icon=”@drawable/icon”<br />
android:label=”@string/app_name”><br />
<br />
<activity<br />
android:name=”.MainActivity”<br />
android:label=”@string/app_name”> <br />
<br />
<intent-filter> <br />
<action android:name=”android.intent.action.MAIN”/><br />
<category android:name=”android.intent.category.LAUNCHER”/> <br />
</intent-filter><br />
<br />
</activity><br />
<br />
<service android:name=”.MyService” /><br />
</application><br />
<uses-sdk<br />
android:minSdkVersion=”9”/><br />
<br />
</manifest> <br /><br />
<b>4 . In the main.xml file, add the following statements in bold:</b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<LinearLayout<br />
xmlns:android=”http://schemas.android.com/apk/res/android”<br />
android:orientation=”vertical”<br />
android:layout_width=”fill_parent” <br />
android:layout_height=”fill_parent” ><br />
<br />
<Button<br />
android:id=”@+id/btnStartService”<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”wrap_content” <br />
android:text=”Start Service” /><br />
<br />
<Button<br />
android:id=”@+id/btnStopService”<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”wrap_content” <br />
android:text=”Stop Service” /><br />
<br />
</LinearLayout> <br /><br />
<b>5 . Add the following statements in bold to the MainActivity.java file:</b><br />
<br />
package com.emergingandroidtech.Services;<br />import android.app.Activity;<br />
import android.os.Bundle;<br />import android.content.Intent;<br />
import android.view.View;<br />
import android.widget.Button;<br /><br />
public class MainActivity extends Activity<br />
{<br />
<br />
/** Called when the activity is first created.*/<br />
<br />
@Override<br />
public void onCreate(Bundle savedInstanceState)<br />
{<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br /><br />
Button btnStart = (Button) findViewById(R.id.btnStartService); <br />
btnStart.setOnClickListener(new View.OnClickListener()<br />
{<br />
public void onClick(View v)<br />
{<br />
startService(new Intent(getBaseContext(), MyService.class)); <br />
}<br />
});<br /><br />
Button btnStop = (Button) findViewById(R.id.btnStopService); <br />
btnStop.setOnClickListener(new View.OnClickListener()<br />
{ <br />
public void onClick(View v)<br />
{<br />
stopService(new Intent(getBaseContext(), MyService.class));<br />
}<br />
});<br />
<br />
}<br />
} <br /><br />
<b>6 . Clicking the Start Service button will start the service. To stop the service, click the Stop Service button.</b><br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com1tag:blogger.com,1999:blog-8784159420963262332.post-78409966410749017352015-03-23T02:02:00.000-07:002015-03-23T02:02:05.595-07:00GridView in Android.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
GridView </h3>
The <b>GridView</b> shows items in a two-dimensional scrolling grid. You can use the GridView together with an ImageView to display a series of images.<br />
The following example demonstrates how.<br />
<br />
<b>1 . Using Eclipse, create a new Android project and name it Grid. </b><br />
<br />
<b>2 . Drag and drop a series of images into the res/drawable-mdpi folder. When a dialog is displayed, check the copy option and click OK. </b><br /><br /><b>3 . Populate the main.xml file with the following content: </b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<GridView<br />
xmlns:android=”http://schemas.android.com/apk/res/android” <br />
android:id=”@+id/gridview”<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”fill_parent” <br />
android:numColumns=”auto_fit”<br />
android:verticalSpacing=”10dp” <br />
android:horizontalSpacing=”10dp”<br />
android:columnWidth=”90dp”<br />
android:stretchMode=”columnWidth”<br />
android:gravity=”center” /> <br />
<br /><b>4 . Add the following statements in bold to the MainActivity.java file: </b><br />
<br />
package com.emergingandroidtech.Grid;<br /><br />import android.app.Activity;<br />import android.os.Bundle;<br /><br />import android.content.Context;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.widget.AdapterView;<br />import android.widget.AdapterView.OnItemClickListener;<br />import android.widget.BaseAdapter;<br />import android.widget.GridView;<br />import android.widget.ImageView;<br />import android.widget.Toast;<br /><br />public class MainActivity extends Activity {<br /> //---the images to display---<br /> Integer[] imageIDs = {<br /> R.drawable.pic1,<br /> R.drawable.pic2,<br /> R.drawable.pic3,<br /> R.drawable.pic4,<br /> R.drawable.pic5,<br /> R.drawable.pic6,<br /> R.drawable.pic7 <br /> };<br /><br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> <br /> GridView gridView = (GridView) findViewById(R.id.gridview);<br /> gridView.setAdapter(new ImageAdapter(this));<br /><br /> gridView.setOnItemClickListener(new OnItemClickListener() <br /> {<br /> public void onItemClick(AdapterView<?> parent, <br /> View v, int position, long id) <br /> { <br /> Toast.makeText(getBaseContext(), <br /> "pic" + (position + 1) + " selected", <br /> Toast.LENGTH_SHORT).show();<br /> }<br /> }); <br /> }<br /> <br /> public class ImageAdapter extends BaseAdapter <br /> {<br /> private Context context;<br /><br /> public ImageAdapter(Context c) <br /> {<br /> context = c;<br /> }<br /><br /> //---returns the number of images---<br /> public int getCount() {<br /> return imageIDs.length;<br /> }<br /><br /> //---returns the ID of an item--- <br /> public Object getItem(int position) {<br /> return position;<br /> }<br /><br /> public long getItemId(int position) {<br /> return position;<br /> }<br /><br /> //---returns an ImageView view---<br /> public View getView(int position, View convertView, ViewGroup parent) <br /> {<br /> ImageView imageView;<br /> if (convertView == null) {<br /> imageView = new ImageView(context);<br /> imageView.setLayoutParams(new GridView.LayoutParams(185, 185));<br /> imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);<br /> imageView.setPadding(5, 5, 5, 5);<br /> } else {<br /> imageView = (ImageView) convertView;<br /> }<br /> imageView.setImageResource(imageIDs[position]);<br /> return imageView;<br /> }<br /> } <br />}<br />
<br />
<b>5 . It shows the GridView displaying all the images. </b><br /><br /><i><b>How It Works </b></i><br />
<br />
Like the Gallery and ImageSwitcher example, you implement the ImageAdapter class and then bind it to the GridView:<br />
<br />
GridView gridView = (GridView)findViewById(R.id.gridview); <br />
gridView.setAdapter(newImageAdapter(this));<br />gridView.setOnItemClickListener(newOnItemClickListener()<br />
{<br />
public void onItemClick(AdapterView<?> parent, View v,int position,long id) <br />
{ <br />
Toast.makeText(getBaseContext(), “pic”+(position+1)+“selected”, Toast.LENGTH_SHORT).show(); } <br />
});<br /><br />
When an image is selected, you display a Toast message indicating the image selected. Within the GridView, you can specify the size of the images and how images are spaced out in the GridView by setting the padding for each image:<br />
<br />
//---returns an ImageView view--- <br />
public View getView(int position,View convertView, ViewGroup parent)<br />
{ <br />
ImageView imageView;<br />
if(convertView==null)<br />
{<br />
imageView=newImageView(context); <br />
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));<br />
imageView.setScaleType( ImageView.ScaleType.CENTER_CROP);<br />
imageView.setPadding(5, 5, 5, 5); <br />
}<br />
else<br />
{<br />
imageView=(ImageView)convertView; <br />
}<br />
imageView.setImageResource(imageIDs[position]); <br />
return imageView;<br />
}<br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-19197714116681046232015-03-20T03:31:00.000-07:002015-03-20T03:31:38.792-07:00Android Download Image from URL.<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Downloading Binary data </h3>
<h3 style="text-align: left;">
</h3>
One of the common tasks you need to perform is downloading binary data from the Web. For example, you may want to download an image from a server so that you can display it in your application.<br />
The following<b> Example</b> shows how this is done.<br /> <br />
<b>1 .Add the following statements in bold to the main.xml file: </b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<br />
<LinearLayout<br />
xmlns:android=”http://schemas.android.com/apk/res/android”<br />
android:orientation=”vertical”<br />
android:layout_width=”fill_parent” <br />
android:layout_height=”fill_parent” ><br /><br />
<ImageView android:id=”@+id/img” <br />
android:layout_width=”wrap_content”<br />
android:layout_height=”wrap_content” <br />
android:layout_gravity=”center” /><br /><br />
</LinearLayout><br />
<br />
<b>2 . Add the following statements in bold to the MainActivity.java file: </b><br />
<br />
public class MainActivity extends Activity<br />
{<br />
ImageView img;<br /><br />
private InputStream OpenHttpConnection(String urlString) throwsIOException<br />
{<br />
//...<br />
}<br /><br />
private Bitmap DownloadImage(String URL) <br />
{<br />
Bitmap bitmap = null;<br />
InputStream in = null; <br />
try<br />
{<br />
in = OpenHttpConnection(URL);<br />
bitmap = BitmapFactory.decodeStream(in);<br />
in.close(); <br />
}<br />
catch (IOException e1)<br />
{<br />
Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show();<br />e1.printStackTrace(); <br />
}<br />
return bitmap;<br />
}<br /><br />
/**Called when the activity is first created.*/<br />
@Override<br />
public void onCreate(Bundle savedInstanceState)<br />
{<br />
super.onCreate(savedInstanceState); <br />
setContentView(R.layout.main);<br /><br />
//---download an image---<br />
Bitmap bitmap = DownloadImage( “http://www.streetcar.org/mim/cable/images/cable-01.jpg”); <br />
img = (ImageView) findViewById(R.id.img);<br />
img.setImageBitmap(bitmap);<br />
}<br />
} <br /><br />
<i><b>How It Works </b></i><br />
<br />
The <b>DownloadImage() </b>method takes the URL of the image to download and then opens the connection to the server using the <b>OpenHttpConnection() </b>method that you have defined earlier. Using the InputStream object returned by the connection, the <b>decodeStream() </b>method from the BitmapFactory class is used to download and decode the data into a Bitmap object. The <b>DownloadImage() </b>method returns a Bitmap object.<br />
<br />
<b>* The image is then displayed using an ImageView view.</b> <br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-38124754189459105862015-03-18T02:22:00.000-07:002015-03-18T02:22:08.561-07:00How to implement spinner in Android ?<div dir="ltr" style="text-align: left;" trbidi="on">
The<b> SpinnerView </b>displays one item at a time from a list and enables users to choose among them.<br />
The following <b>Example</b> shows how you can use the SpinnerView in your activity.<br /><br />
<b>Using the SpinnerView to Display an item at a Time </b><br />
<br />
<b>1 . Using Eclipse, create an Android project and name it as SpinnerView.</b><br /><br /><b>2 . Modify the main.xml file located in the res/layout folder as shown here: </b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<LinearLayout<br />
xmlns:android=”http://schemas.android.com/apk/res/android”<br />
android:orientation=”vertical”<br />
android:layout_width=”fill_parent” <br />
android:layout_height=”fill_parent” ><br /><br />
<Spinner android:id=”@+id/spinner1” <br />
android:layout_width=”wrap_content”<br />
android:layout_height=”wrap_content” <br />
android:drawSelectorOnTop=”true” /><br /><br />
</LinearLayout> <br /><br />
<b>3 . Add the following lines in bold to the strings.xml file located in the res/values folder: </b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<resources><br />
<br />
<stringname=”hello”>HelloWorld,MainActivity!</string> <br />
<stringname=”app_name”>SpinnerView</string><br />
<br />
<string-array name=”presidents_array”> <br />
<br />
<item>Dwight D. Eisenhower</item> <br />
<item>John F. Kennedy</item> <br />
<item>Lyndon B. Johnson</item><br />
<item>Richard Nixon</item> <br />
<item>Gerald Ford</item> <br />
<item>Jimmy Carter</item><br />
<item>Ronald Reagan</item> <br />
<item>George H. W. Bush</item> <br />
<item>Bill Clinton</item><br />
<item>George W. Bush</item> <br />
<item>Barack Obama</item> <br />
<br />
</string-array><br />
<br />
</resources> <br /><br />
<b>4 . Add the following statements in bold to the MainActivity.java file: </b><br />
<br />
package com.emergingandroidtech.SpinnerView;<br /><br />import android.app.Activity;<br />import android.os.Bundle;<br /><br />import android.view.View;<br />import android.widget.AdapterView;<br />import android.widget.AdapterView.OnItemSelectedListener;<br />import android.widget.ArrayAdapter;<br />import android.widget.Spinner;<br />import android.widget.Toast;<br /><br />public class MainActivity extends Activity {<br /> <br /> String[] presidents; <br /> <br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> <br /> presidents = <br /> getResources().getStringArray(R.array.presidents_array);<br /> Spinner s1 = (Spinner) findViewById(R.id.spinner1);<br /> <br /> ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,<br /> android.R.layout.simple_spinner_dropdown_item, presidents);<br /><br /> s1.setAdapter(adapter);<br /> s1.setOnItemSelectedListener(new OnItemSelectedListener()<br /> {<br /> @Override<br /> public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) <br /> {<br /> int index = arg0.getSelectedItemPosition();<br /> Toast.makeText(getBaseContext(), <br /> "You have selected item : " + presidents[index], <br /> Toast.LENGTH_SHORT).show(); <br /> }<br /><br /> @Override<br /> public void onNothingSelected(AdapterView<?> arg0) {} <br /> }); <br /> }<br />}<br />
<br />
<b>5 .Click on the SpinnerView and you will see a pop-up displaying the list of presidents’ names. Clicking on an item will display a message showing you the item selected.</b></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-62023248706502435092015-03-16T02:06:00.000-07:002015-03-16T02:06:04.814-07:00ImageSwitcher in Android<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
ImageSwitcher</h3>
<br />
The previous post <a href="http://emergingandroidtech.blogspot.in/2015/03/gallery-in-android.html" target="_blank">http://emergingandroidtech.blogspot.in/2015/03/gallery-in-android.html</a><br />
demonstrated how to use the Gallery view together with an ImageView to display a series of thumbnail images so that when one is selected, the selected image is displayed in the ImageView.<br />
However, sometimes you don’t want an image to appear abruptly when the user selects it in the Gallery view — you might, for example, want to apply some animation to the image when it transits from one image to another. In this case, you need to use the ImageSwitcher together with the Gallery view.<br />
<br />
<b>The following Try It Out shows you how.</b><br />
<br />
<h4 style="text-align: left;">
Using the imageSwitcher View </h4>
<br />
<br />
<b>1 . Using Eclipse, create a new Android project and name it as ImageSwitcher. </b><br />
<br />
<b>2 . Modify the main.xml file by adding the following statements in bold: </b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<RelativeLayout<br />
xmlns:android=”http://schemas.android.com/apk/res/android” <br />
android:orientation=”vertical”<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”fill_parent” <br />
android:background=”#ff000000”><br />
<br />
<Gallery<br />
android:id=”@+id/gallery1” <br />
android:layout_width=”fill_parent”<br />
android:layout_height=”wrap_content” /><br />
<br />
<ImageSwitcher <br />
android:id=”@+id/switcher1” <br />
android:layout_width=”fill_parent” <br />
android:layout_height=”fill_parent” <br />
android:layout_alignParentLeft=”true” <br />
android:layout_alignParentRight=”true” <br />
android:layout_alignParentBottom=”true” /><br />
<br />
</RelativeLayout> <br />
<br />
<b>3 . Right-click on the res/values folder and select New ➪ File. Name the file attrs.xml. </b><br />
<br />
<b>4 . Populate the attrs.xml file as follows: </b><br />
<br />
<?xml version=”1.0” encoding=”utf-8”?><br />
<resources> <br />
<declare-styleable<br />
name=”Gallery1”> <br />
<attr name=”android:galleryItemBackground” /> <br />
</declare-styleable><br />
</resources> <br />
<br />
<b>5 . Drag and drop a series of images into the res/drawable-mdpi folder When a dialog is displayed, check the copy option and click OK.</b><br />
<br />
<b>6 . Add the following bold statements to the MainActivity.java file: </b><br />
<br />
package com.emergingandroidtech.ImageSwitcher;<br /><br />import android.app.Activity;<br />import android.os.Bundle;<br /><br />import android.content.Context;<br />import android.content.res.TypedArray;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.view.ViewGroup.LayoutParams;<br />import android.view.animation.AnimationUtils;<br />import android.widget.BaseAdapter;<br /><br />import android.widget.AdapterView;<br />import android.widget.AdapterView.OnItemClickListener;<br />import android.widget.Gallery;<br />import android.widget.ViewSwitcher.ViewFactory;<br />import android.widget.ImageSwitcher;<br />import android.widget.ImageView;<br /><br />public class MainActivity extends Activity implements ViewFactory { <br /> //---the images to display---<br /> Integer[] imageIDs = {<br /> R.drawable.pic1,<br /> R.drawable.pic2,<br /> R.drawable.pic3,<br /> R.drawable.pic4,<br /> R.drawable.pic5,<br /> R.drawable.pic6,<br /> R.drawable.pic7 <br /> };<br /> <br /> private ImageSwitcher imageSwitcher;<br /> <br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> <br /> imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);<br /> imageSwitcher.setFactory(this);<br /> imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,<br /> android.R.anim.slide_in_left));<br /> imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,<br /> android.R.anim.slide_out_right));<br /> <br /> Gallery gallery = (Gallery) findViewById(R.id.gallery1);<br /> gallery.setAdapter(new ImageAdapter(this));<br /> gallery.setOnItemClickListener(new OnItemClickListener() <br /> {<br /> public void onItemClick(AdapterView<?> parent, <br /> View v, int position, long id) <br /> { <br /> imageSwitcher.setImageResource(imageIDs[position]);<br /> }<br /> }); <br /> }<br /> <br /> public View makeView() <br /> {<br /> ImageView imageView = new ImageView(this);<br /> imageView.setBackgroundColor(0xFF000000);<br /> imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);<br /> imageView.setLayoutParams(new <br /> ImageSwitcher.LayoutParams(<br /> LayoutParams.FILL_PARENT,<br /> LayoutParams.FILL_PARENT));<br /> return imageView;<br /> }<br /><br /> public class ImageAdapter extends BaseAdapter <br /> {<br /> private Context context;<br /> private int itemBackground;<br /><br /> public ImageAdapter(Context c) <br /> {<br /> context = c;<br /><br /> //---setting the style--- <br /> TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);<br /> itemBackground = a.getResourceId(<br /> R.styleable.Gallery1_android_galleryItemBackground, 0);<br /> a.recycle(); <br /> }<br /><br /> //---returns the number of images---<br /> public int getCount() <br /> {<br /> return imageIDs.length;<br /> }<br /><br /> //---returns the ID of an item--- <br /> public Object getItem(int position) <br /> {<br /> return position;<br /> }<br /><br /> public long getItemId(int position) <br /> {<br /> return position;<br /> }<br /><br /> //---returns an ImageView view---<br /> public View getView(int position, View convertView, ViewGroup parent)<br /> {<br /> ImageView imageView = new ImageView(context);<br /> <br /> imageView.setImageResource(imageIDs[position]);<br /> imageView.setScaleType(ImageView.ScaleType.FIT_XY);<br /> imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));<br /> imageView.setBackgroundResource(itemBackground);<br /> <br /> return imageView;<br /> }<br /> } <br />}<br />
<br />
<b> 7 . Run the application it shows the Gallery and ImageSwitcher views, with both the collection of images as well as the image selected. </b></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-28142915196361947262015-03-13T01:20:00.000-07:002015-03-13T01:20:25.606-07:00Gallery in Android<div dir="ltr" style="text-align: left;" trbidi="on">
The<b> Gallery</b> is a view that shows items (such as images) in a center-locked, horizontal scrolling list.<br />
<br />
The following example shows you how to use the Gallery view to display a set of images.<br />
<br />
<h4 style="text-align: left;">
Using the Gallery View</h4>
<br />
<b>1 . Using Eclipse, create a new Android project</b><br />
<br />
<b>2 . Modify the main.xml file as shown in bold:</b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<LinearLayout<br />
xmlns:android=”http://schemas.android.com/apk/res/android”<br />
android:orientation=”vertical”<br />
android:layout_width=”fill_parent” <br />
android:layout_height=”fill_parent”><br />
<br />
<TextView <br />
android:layout_width=”fill_parent”<br />
android:layout_height=”wrap_content” <br />
android:text=”Images of San Francisco” /><br />
<br />
<Gallery<br />
android:id=”@+id/gallery1”<br />
android:layout_width=”fill_parent” <br />
android:layout_height=”wrap_content” /><br />
<br />
<ImageView <br />
android:id=”@+id/image1” <br />
android:layout_width=”320px” <br />
android:layout_height=”250px” <br />
android:scaleType=”fitXY” /><br />
<br />
</LinearLayout><br />
<br />
<b>3 . Right-click on the res/values folder and select New ➪ File. Name the file attrs.xml. </b><br />
<br />
<b>4 . Populate the attrs.xml file as follows: </b><br />
<br />
<?xml version=”1.0” encoding=”utf-8”?><br />
<resources><br />
<declare-styleable name=”Gallery1”><br />
<attr name=”android:galleryItemBackground” /> <br />
</declare-styleable><br />
</resources> <br /><br />
<b>5 . Prepare a series of images and name them pic1.png, pic2.png, and so on.</b><br />
<br />
<b>6 . Add the following statements in bold to the MainActivity.java file: </b><br />
<br />
package com.emergingandroidtech.Gallery;<br /><br />import android.app.Activity;<br />import android.os.Bundle;<br /><br />import android.content.Context;<br />import android.content.res.TypedArray;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.widget.AdapterView;<br />import android.widget.AdapterView.OnItemClickListener;<br />import android.widget.BaseAdapter;<br />import android.widget.Gallery;<br />import android.widget.ImageView;<br />import android.widget.Toast;<br /><br />public class MainActivity extends Activity { <br /> //---the images to display---<br /> Integer[] imageIDs = {<br /> R.drawable.pic1,<br /> R.drawable.pic2,<br /> R.drawable.pic3,<br /> R.drawable.pic4,<br /> R.drawable.pic5,<br /> R.drawable.pic6,<br /> R.drawable.pic7 <br /> };<br /> <br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> <br /> Gallery gallery = (Gallery) findViewById(R.id.gallery1);<br /> <br /> gallery.setAdapter(new ImageAdapter(this)); <br /> gallery.setOnItemClickListener(new OnItemClickListener() <br /> {<br /> public void onItemClick(AdapterView<?> parent, View v, int position, long id) <br /> { <br /> Toast.makeText(getBaseContext(), <br /> "pic" + (position + 1) + " selected", <br /> Toast.LENGTH_SHORT).show();<br /> <br /> //---display the images selected---<br /> ImageView imageView = (ImageView) findViewById(R.id.image1); <br /> imageView.setImageResource(imageIDs[position]);<br /> }<br /> });<br /> }<br /> <br /> public class ImageAdapter extends BaseAdapter <br /> {<br /> private Context context;<br /> private int itemBackground;<br /><br /> public ImageAdapter(Context c) <br /> {<br /> context = c;<br /> //---setting the style---<br /> TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);<br /> itemBackground = a.getResourceId(<br /> R.styleable.Gallery1_android_galleryItemBackground, 0);<br /> a.recycle(); <br /> }<br /><br /> //---returns the number of images---<br /> public int getCount() {<br /> return imageIDs.length;<br /> } <br /> <br /> //---returns the ID of an item--- <br /> public Object getItem(int position) {<br /> return position;<br /> } <br /> <br /> //---returns the ID of an item--- <br /> public long getItemId(int position) {<br /> return position;<br /> }<br /> <br /> //---returns an ImageView view---<br /> public View getView(int position, View convertView, ViewGroup parent) {<br /> ImageView imageView = new ImageView(context);<br /> imageView.setImageResource(imageIDs[position]);<br /> imageView.setScaleType(ImageView.ScaleType.FIT_XY);<br /> imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));<br /> imageView.setBackgroundResource(itemBackground);<br /> return imageView;<br /> }<br /> } <br />}<br />
<br />
<b>7 . Shows the Gallery view displaying the series of images. </b><br />
<br />
<b>8 . You can swipe the images to view the entire series of images. Observe that as you click on an image, the Toast class will display its name.</b><br />
<br />
<br />
<br />
<b>9 . To display the selected image in the ImageView, add the following statements in bold to the MainActivity.java file:</b> <br />
<br />/**Called when the activity is first created.*/<br />
@Override<br />
public void onCreate(BundlesavedInstanceState)<br />
{<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main); <br />
Gallery gallery=(Gallery)findViewById(R.id.gallery1); <br />
gallery.setAdapter(newImageAdapter(this));<br />
gallery.setOnItemClickListener(newOnItemClickListener() <br />
{ <br />
public void onItemClick(AdapterView<?> parent,View v, int position,long id)<br />
{ <br />
Toast.makeText(getBaseContext(), “pic”+(position+1)+“selected”, Toast.LENGTH_SHORT).show();<br /><br />
//---display the images selected---<br />
ImageView imageView = (ImageView) findViewById(R.id.image1); imageView.setImageResource(imageIDs[position]); <br />
} <br />
});<br />
} <br /><br />
<b>10 . This time, you will see the image selected in the ImageView.</b><br /><br /><br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-43710969936898160392015-03-11T01:59:00.003-07:002015-03-11T01:59:48.082-07:00Android WebView Example<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Webview </h3>
<br />
The <b>WebView</b> enables you to embed a web browser in your activity. This is very useful if your appli- cation needs to embed some web content, such as maps from some other providers, and so on. The following Try It Out shows how you can programmatically load the content of a web page and dis- play it in your activity.<br />
<br />
<h4 style="text-align: left;">
Using the WebView View</h4>
<br />
<b>1 .</b> Using Eclipse, create a new Android project and name it as WebView.<br />
<br />
<b>2 .</b> Add the following statements to the main.xml file:<br />
<br />
<?xml version=”1.0” encoding=”utf-8”?><br />
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” <br />
android:layout_width=”fill_parent” <br />
android:layout_height=”fill_parent” ><br /><br />
<WebView<br />
android:id=”@+id/webview1” <br />
android:layout_width=”wrap_content” <br />
android:layout_height=”wrap_content” /><br /><br />
</LinearLayout> <br /><br />
<b>3 .</b> In the MainActivity.java file, add the following statements in bold:<br />
<br />
package com.emergingandroidtech.WebView;<br />import android.app.Activity;<br />
import android.os.Bundle;<br />
import android.webkit.WebSettings;<br />
import android.webkit.WebView;<br /><br />
public class MainActivity extends Activity<br />
{<br />
<br />
/**Called when the activity is first created.*/<br />
<br />
@Override <br />
public void onCreate(BundlesavedInstanceState)<br />
{<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br /><br />
WebView wv = (WebView) findViewById(R.id.webview1);<br />
WebSettings webSettings = wv.getSettings(); <br />
webSettings.setBuiltInZoomControls(true);<br />
wv.loadUrl( “www.google.com”);<br />
}<br />
}<br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-68593628429060682002015-03-10T02:58:00.001-07:002015-03-10T02:58:35.089-07:00How to create notifications in android ?<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Displaying Notifications</h3>
<br />So far, you have been using the Toast class to display messages to the user. While the Toast class is a handy way to show users alerts, it is not persistent. It flashes on the screen for a few seconds and then disappears. If it contains important information, users may easily miss it if they are not looking at the screen. For messages that are important, you should use a more persistent method. In this case, you should use the NotificationManager to display a persistent message at the top of the device, commonly known as the status bar (sometimes also referred to as the notification bar).<br />
<br />
<h4 style="text-align: left;">
Displaying notifications on the Status Bar</h4>
<div style="text-align: left;">
<br /><b>1 .</b> Using Eclipse, create a new Android project and name it Notifications. </div>
<br />
<b>2 .</b> Add a new class file named NotificationView.java to the src folder of the project.<br />
In addition, add a new notification.xml file to the res/layout folder as well.<br /><br /><b>3 .</b> Populate the notification.xml file as follows:<br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<br />
<LinearLayout<br />
xmlns:android=”http://schemas.android.com/apk/res/android”<br />
android:orientation=”vertical”<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”fill_parent”><br />
<br />
<TextView<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”wrap_content” <br />
android:text=”Here are the details for the notification...” /><br />
<br />
</LinearLayout> <br /><br />
<b>4 .</b> Populate the NotificationView.java file as follows:<br />
<br />
package com.emergingandroidtech.Notifications;<br />import android.app.Activity;<br />
import android.app.NotificationManager;<br />
import android.os.Bundle;<br /><br />
public class NotificationView extends Activity<br />
{<br />
@Override<br />
public void onCreate(Bundle savedInstanceState)<br />
{<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.notification); <br />
<br />
//---look up the notification manager service---<br />
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);<br /><br />
//---cancel the notification that we started<br />
nm.cancel(getIntent().getExtras().getInt(“notificationID”)); <br />
}<br />
} <br /><br />
<b>5 .</b> Add the following statements in bold to the AndroidManifest.xml file:<br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<manifest<br />
xmlns:android=”http://schemas.android.com/apk/res/android” package=”com.emergingandroidtech.Notifications” <br />
android:versionCode=”1” <br />
android:versionName=”1.0”> <br />
<br />
<application<br />
android:icon=”@drawable/icon”<br />
android:label=”@string/app_name”><br />
<br />
<activity<br />
android:name=”.MainActivity” <br />
android:label=”@string/app_name”> <br />
<intent-filter> <action android:name=”android.intent.action.MAIN”/> <br />
<category android:name=”android.intent.category.LAUNCHER”/> <br />
</intent-filter><br />
</activity><br />
<br />
<activity<br />
android:name=”.NotificationView” <br />
android:label=”Details of notification”> <br />
<intent-filter><br />
<action android:name=”android.intent.action.MAIN” /> <br />
<category android:name=”android.intent.category.DEFAULT” /> <br />
</intent-filter><br />
</activity><br />
</application> <br />
<uses-sdk<br />
android:minSdkVersion=”9”/><br />
<br />
<uses-permission<br />
android:name=”android.permission.VIBRATE”/><br />
<br />
</manifest> <br /><br />
<b>6 . </b>Add the following statements in bold to the main.xml file:<br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<LinearLayout<br />
xmlns:android=”http://schemas.android.com/apk/res/android” <br />
android:orientation=”vertical”<br />
android:layout_width=”fill_parent” <br />
android:layout_height=”fill_parent”><br />
<br />
<Button android:id=”@+id/btn_displaynotif”<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”wrap_content” <br />
android:text=”Display Notification” /><br />
<br />
</LinearLayout> <br /><br />
<b>7 . </b>Finally, add the following statements in bold to the MainActivity.java file:<br />
<br />
package com.emergingandroidtech.Notifications;<br />import android.app.Activity;<br />
import android.os.Bundle;<br />
import android.app.Notification;<br />
import android.app.NotificationManager;<br />
import android.app.PendingIntent;<br />
import android.content.Intent;<br />
import android.view.View;<br />
import android.widget.Button;<br /><br />
public class MainActivity extends Activity<br />
{<br />
int notificationID = 1;<br />
<br />
/**Called when the activity is first created.*/<br />
@Override<br />
public void onCreate(BundlesavedInstanceState)<br />
{<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main); <br />
Button button = (Button) findViewById(R.id.btn_displaynotif); <br />
button.setOnClickListener(new Button.OnClickListener()<br />
{<br />
public void onClick(View v)<br />
{<br />
displayNotification(); <br />
}<br />
});<br />
}<br /><br />
protected void displayNotification() <br />
{<br />
//---PendingIntent to launch activity if the user selects <br />
// this notification--- <br />
Intent i = new Intent(this, NotificationView.class); <br />
i.putExtra(“notificationID”, notificationID);<br /><br />
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);<br />
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); <br />Notification notif = new Notification( R.drawable.icon, “Reminder: Meeting starts in 5 minutes”, System.currentTimeMillis());<br />
<br />CharSequence from = “System Alarm”;<br />
CharSequence message = “Meeting with customer at 3pm...”; <br />
notif.setLatestEventInfo(this, from, message, pendingIntent);<br /><br />
//---100ms delay, vibrate for 250ms, pause for 100 ms and<br />
// then vibrate for 500ms--- <br />
notif.vibrate = new long[] { 100, 250, 100, 500}; <br />
nm.notify(notificationID, notif);<br />
}<br />
}<br />
<br />
<b>8 .</b> Click the Display Notification button and a notification will appear on the status bar. <br />
<br />
<b>9 .</b> Clicking and dragging the status bar down will reveal the notification . <br />
<br />
<b>10 .</b> Clicking on the notification will reveal the NotificationView activity. This also causes the notification to be dismissed from the status bar. <br />
<br />
<br />
<br />
<br />
<br />
<i><b>How It Works </b></i><br />
<br />
To display a notification, you first created an Intent object to point to the NotificationView class:<br /><br />
//---PendingIntent to launch activity if the user selects<br />
//this notification--- <br />
Intent i=newIntent(this,NotificationView.class);<br />
i.putExtra(“notificationID”,notificationID);<br /><br />
This intent will be used to launch another activity when the user selects a notification from the list of notifications. In this example, you added a key/value pair to the Intent object so that you can tag the notification ID, identifying the notification to the target activity. This ID will be used to dismiss the notifications later. You would also need to create a PendingIntent object. A PendingIntent object helps you to perform an action on your application’s behalf, often at a later time, regardless of whether your application is running or not. In this case, you initialized it as follows:<br /><br />
PendingIntent pendingIntent= PendingIntent.getActivity(this,0,i,0);<br /><br />
The<b> getActivity() </b>method retrieves a PendingIntent object and you set it using the following arguments:<br />
<br />
➤context — Application context<br />
➤ request code — Request code for the intent<br />
➤ intent — The intent for launching the target activity<br />
➤ flags — The flags in which the activity is to be launched<br />
<br />
You then obtain an instance of the NotificationManager class and create an instance of the Notification class:<br /><br />
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);<br />Notification notif = new Notification( R.drawable.icon, “Reminder: Meeting starts in 5 minutes”, System.currentTimeMillis());<br /><br />
The Notification class enables you to specify the notification’s main information when the notification first appears on the status bar. The second argument to the Notification constructor sets the “ticker text” on the status bar.<br />
<br />
Next, you set the details of the notification using the <b>setLatestEventInfo() </b>method:<br /><br />
CharSequence from = “System Alarm”;<br />
CharSequence message = “Meeting with customer at 3pm...”;<br />
notif.setLatestEventInfo(this, from, message, pendingIntent);<br />
<br />
//---100ms delay, vibrate for 250ms, pause for 100 ms and <br />
// then vibrate for 500ms--- <br />
notif.vibrate = new long[] { 100, 250, 100, 500};<br /><br />
The preceding also sets the notification to vibrate the phone. Finally, to display the notification you use the <b>notify() </b>method:<br /><br />
nm.notify(notificationID, notif);<br /><br />
When the user clicks on the notification, the NotificationView activity is launched. Here, you dismiss the notification by using the <b>cancel() </b>method of the NotificationManager object and passing it the ID of the notification (passed in via the Intent object):<br /><br />
//---look up the notification manager service---<br />
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);<br /><br />
//---cancel the notification that we started<br />
nm.cancel(getIntent().getExtras().getInt(“notificationID”));<br /><br /><br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-19731854243104049302015-03-09T01:14:00.002-07:002015-03-09T01:15:15.202-07:00Android Custom dialog example<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
<b>Displaying a dialog Window</b> </h3>
There are times where you need to display a dialog window to get a confirmation from the user. In this case, you can override the <b>onCreateDialog()</b> protected method defined in the base Activity class to display a dialog window.<br />
<br />
The following Try It Out shows you how.<br />
<br />
<h4 style="text-align: left;">
Displaying a Dialog Window Using an Activity </h4>
<div style="text-align: left;">
<br />
<b>1 .</b> Using Eclipse, create a new Android project and name it Dialog. </div>
<br />
<b>2 . Add the following statements in bold to the main.xml file: </b><br />
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<br />
<LinearLayout<br />
xmlns:android=”http://schemas.android.com/apk/res/android” <br />
android:orientation=”vertical”<br />
android:layout_width=”fill_parent” <br />
android:layout_height=”fill_parent”><br />
<br />
<TextView android:layout_width=”fill_parent” <br />
android:layout_height=”wrap_content”<br />
android:text=”@string/hello”/><br />
<br />
<Button android:id=”@+id/btn_dialog” <br />
android:layout_width=”fill_parent” <br />
android:layout_height=”wrap_content”<br />
android:text=”Click to display a dialog” /><br />
<br />
</LinearLayout><br />
<br />
<b>3 . Add the following statements in bold to the MainActivity.java file:</b><br />
<br />
package com.emergingandroidtech.Dialog;<br />
import android.app.Activity;<br />
import android.os.Bundle;<br />
import android.app.AlertDialog;<br />
import android.app.Dialog;<br />
import android.content.DialogInterface;<br />
import android.view.View;<br />
import android.widget.Button;<br />
import android.widget.Toast;<br />
<br />
public class MainActivity extends Activity<br />
{<br />
CharSequence[] items = { “Google”, “Apple”, “Microsoft” }; <br />
boolean[] itemsChecked = new boolean [items.length];<br />
<br />
/**Called when the activity is first created.*/ <br />
@Override<br />
public void onCreate(BundlesavedInstanceState)<br />
{ <br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br />
<br />
Button btn = (Button) findViewById(R.id.btn_dialog); <br />
btn.setOnClickListener(new View.OnClickListener()<br />
{<br />
public void onClick(View v)<br />
{<br />
showDialog(0); <br />
} <br />
});<br />
}<br />
<br />
@Override<br />
protected Dialog onCreateDialog(int id)<br />
{<br />
switch (id)<br />
{ <br />
case 0: <br />
return new AlertDialog.Builder(this) .setIcon(R.drawable.icon) .setTitle(“This is a dialog with some simple text...”) .setPositiveButton(“OK”, new DialogInterface.OnClickListener()<br />
{<br />
public void onClick(DialogInterface dialog, int whichButton) <br />
{ <br />
Toast.makeText(getBaseContext(), “OK clicked!”, Toast.LENGTH_SHORT).show(); <br />
}<br />
}) .setNegativeButton(“Cancel”, new DialogInterface.OnClickListener()<br />
{<br />
public void onClick(DialogInterface dialog, int whichButton) <br />
{ <br />
Toast.makeText(getBaseContext(), “Cancel clicked!”, Toast.LENGTH_SHORT).show(); <br />
}<br />
})<br />
.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener()<br />
{<br />
@Override <br />
public void onClick(DialogInterface dialog, int which, boolean isChecked)<br />
{ <br />
Toast.makeText(getBaseContext(), items[which] + (isChecked ? “ checked!”: “ unchecked!”), Toast.LENGTH_SHORT).show(); <br />
} <br />
} )<br />
.create();<br />
}<br />
return null; <br />
<br />
<b>4 . </b>Click the button to display the dialog. Checking the various checkboxes will cause the Toast class to display the text of the item checked/unchecked. To dismiss the dialog, click the OK or Cancel button.<br />
<br />
<i><b>How It Works </b></i><br />
<br />
To display a dialog, you first override the <b>onCreateDialog()</b> method in the Activity class:<br />
<br />
@Override<br />
protected Dialog onCreateDialog(int id)<br />
{ <br />
//...<br />
} <br />
}<br />
}<br />
<br />
This method is called when you call the <b>showDialog()</b> method: <br />
<br />
Button btn=(Button)findViewById(R.id.btn_dialog); <br />
btn.setOnClickListener(newView.OnClickListener()<br />
{ <br />
public void onClick(View v)<br />
{ <br />
showDialog(0);<br />
} <br />
});<br />
<br />
The <b>onCreateDialog() </b>method is a callback for creating dialogs that are managed by the activity. When you call the <b>showDialog()</b> method, this callback will be invoked. The <b>showDialog()</b> method accepts an integer argument identifying a particular dialog to display. To create a dialog, you use the AlertDialog class’s Builder constructor. You set the various properties, such as icon, title, and buttons, as well as checkboxes: <br />
<br />
@Override<br />
protected Dialog onCreateDialog(int id)<br />
{<br />
switch(id)<br />
{ <br />
case0: <br />
return new AlertDialog.Builder(this) .setIcon(R.drawable.icon) .setTitle(“This is a dialog with somesimple text...”) .setPositiveButton(“OK”,new DialogInterface.OnClickListener()<br />
{<br />
public void onClick(DialogInterface dialog, int whichButton) <br />
{ <br />
Toast.makeText(getBaseContext(), “OKclicked!”,Toast.LENGTH_SHORT).show(); <br />
} <br />
}) .setNegativeButton(“Cancel”,new DialogInterface.OnClickListener()<br />
{<br />
public void onClick(DialogInterface dialog, int whichButton) <br />
{<br />
Toast.makeText(getBaseContext(), “Cancel clicked!”,Toast.LENGTH_SHORT).show();<br />
}<br />
})<br />
.setMultiChoiceItems(items,itemsChecked,new DialogInterface.OnMultiChoiceClickListener()<br />
{<br />
<br />
@Override <br />
public void onClick(DialogInterface dialog,int which, boolean isChecked)<br />
{<br />
Toast.makeText(getBaseContext(), items[which]+(isChecked?“checked!”: “unchecked!”), Toast.LENGTH_SHORT).show(); <br />
}<br />
} )<br />
.create(); <br />
}<br />
return null; <br />
}<br />
<br />
The preceding code sets two buttons:<br />
<b>OK</b> and <b>Cancel</b>, using the <b>setPositiveButton()</b> and <b>setNegativeButton()</b> methods, respectively. You also set a list of checkboxes for users to choose via the<b> setMultiChoiceItems()</b> method. For the <b>setMultiChoiceItems() </b>method, you passed in two arrays: one for the list of items to display and another to contain the value of each item to indicate if they are checked. When each item is checked, you use the Toast class to display a message .</div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-83070248468573494832015-03-04T01:35:00.003-08:002015-03-04T01:35:57.008-08:00Android Date Picker dialog<div dir="ltr" style="text-align: left;" trbidi="on">
<h4 style="text-align: left;">
Displaying the DatePicker View in a Dialog Window </h4>
<br />
Like the TimePicker, you can also display the DatePicker in a dialog window. <br />
<br />
<h4 style="text-align: left;">
Using a Dialog to Display the DatePicker View</h4>
<br />
<b>1 . Add the following statements in bold to the MainActivity.java file:</b><br /><br />
package com.emergingandroidtech.DatePicker;<br />import android.app.Activity;<br />
import android.os.Bundle;<br />import android.view.View;<br />
importandroid.widget.Button;<br />import android.widget.Toast;<br />import android.app.Dialog;<br />
import android.app.TimePickerDialog;<br />import android.widget.TimePicker;<br />
import android.widget.DatePicker;<br />import android.app.DatePickerDialog;<br />
import java.util.Calendar;<br /><br />
public class MainActivity extends Activity{<br />
TimePicker timePicker;<br />
DatePicker datePicker;<br />int hour,minute; <br />
int yr, month, day;<br />static final int TIME_DIALOG_ID=0; <br />
static final int DATE_DIALOG_ID = 1;<br /><br />
/**Called when the activity is first created.*/<br />
@Override<br />
public void onCreate(BundlesavedInstanceState){<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br /><br />
//showDialog(TIME_DIALOG_ID);<br /><br />
//---get the current date---<br />
Calendar today = Calendar.getInstance();<br />
yr = today.get(Calendar.YEAR); <br />
month = today.get(Calendar.MONTH); <br />
day = today.get(Calendar.DAY_OF_MONTH); <br />
showDialog(DATE_DIALOG_ID);<br />timePicker=(TimePicker)findViewById(R.id.timePicker); <br />
timePicker.setIs24HourView(true);<br />datePicker=(DatePicker)findViewById(R.id.datePicker);<br /><br />
//---Buttonview---<br />
Button btnOpen=(Button)findViewById(R.id.btnSet);<br />
btnOpen.setOnClickListener(newView.OnClickListener()<br />
{<br />
public void onClick(View v){<br />
Toast.makeText(getBaseContext(), “Dateselected:”+datePicker.getMonth()+ “/”+datePicker.getDayOfMonth()+ “/”+datePicker.getYear()+“\n”+ “Timeselected:”+timePicker.getCurrentHour()+ “:”+timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); } <br />
});<br />
}<br /><br />
@Override<br />
protected Dialog onCreateDialog(int id)<br />
{<br />
switch(id)<br />
{ <br />
case TIME_DIALOG_ID: <br />
return newTimePickerDialog( this,mTimeSetListener,hour,minute,false);<br />
case DATE_DIALOG_ID: <br />
return new DatePickerDialog( this, mDateSetListener, yr, month, day); <br />
}<br />
return null;<br />
}<br />
<br />private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() <br />
{<br />
public void onDateSet( DatePicker view, int year, int monthOfYear, int dayOfMonth) {<br />
yr = year;<br />
month = monthOfYear;<br />
day = dayOfMonth;<br />
Toast.makeText(getBaseContext(), “You have selected : “ + (month + 1) + “/” + day + “/” + year, Toast.LENGTH_SHORT).show();<br />
}<br />
};<br /><br />
private TimePickerDialog.OnTimeSetListener mTimeSetListener= newTimePickerDialog.OnTimeSetListener() <br />
{<br />
public void onTimeSet( TimePicker view,int hourOfDay,int minuteOfHour) <br />
{<br />
hour=hourOfDay;<br />
minute=minuteOfHour;<br />
Toast.makeText(getBaseContext(), “Youhaveselected:“+hour+“:”+minute, Toast.LENGTH_SHORT).show();<br />}<br />
};<br />
}<br />
<br />
<b><i>How It Works </i></b><br />
<br />
The DatePicker works exactly like the TimePicker. When a date is set, it fires the <b>onDateSet()</b> method, where you can obtain the date set by the user: <br /><br />
public void onDateSet( DatePicker view,int year,int monthOfYear,int dayOfMonth)<br />
{<br />
yr=year;<br />
month=monthOfYear; <br />
day=dayOfMonth; <br />
Toast.makeText(getBaseContext(), “Youhaveselected:“+(month+1)+ “/”+day+“/”+year, Toast.LENGTH_SHORT).show();<br />
}<br /><br />
<b>* </b>Note that you have to initialize the three variables — yr, month, and day — before showing the dialog: <br /><br />
//---getthecurrentdate---<br />
Calendar today=Calendar.getInstance();<br />
yr=today.get(Calendar.YEAR); <br />
month=today.get(Calendar.MONTH); <br />
day=today.get(Calendar.DAY_OF_MONTH); <br />
showDialog(DATE_DIALOG_ID);<br /><br />
If you don’t, you will get an illegal argument exception error during run time (“current should be >= start and <= end”) when you create an instance of the DatePickerDialog class.<br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-88597096246558537842015-03-03T00:50:00.000-08:002015-03-03T00:50:04.183-08:00Date Picker in Android<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
DatePicker View</h3>
Another view that is similar to the TimePicker is the DatePicker. Using the DatePicker, you can enable users to select a particular date on the activity. <br />
<br />
<h4 style="text-align: left;">
1 . main.xml file as shown here: </h4>
<br />
<?xml version=”1.0”encoding=”utf-8”?><br />
<br />
<LinearLayout<br />
xmlns:android=”http://schemas.android.com/apk/res/android” <br />
android:orientation=”vertical”<br />
android:layout_width=”fill_parent”<br />
android:layout_height=”fill_parent”><br /><br />
<DatePicker<br />
android:id=”@+id/datePicker”<br />
android:layout_width=”wrap_content”<br />
android:layout_height=”wrap_content” /><br /><br />
<TimePicker<br />
android:id=”@+id/timePicker” <br />
android:layout_width=”wrap_content”<br />
android:layout_height=”wrap_content”/><br /><br />
<Button<br />
android:id=”@+id/btnSet”<br />
android:layout_width=”wrap_content” <br />
android:layout_height=”wrap_content”<br />
android:text=”Iamallset!”/><br /><br />
</LinearLayout><br />
<br />
<h4 style="text-align: left;">
2 . Add in the following statements in bold to the MainActivity.java file: </h4>
<br />
package com.emergingandroidtech.DatePicker;<br />import android.app.Activity;<br />
import android.os.Bundle;<br />
import android.view.View;<br />
import android.widget.Button;<br />import android.widget.Toast;<br />import android.app.Dialog;<br />
import android.app.TimePickerDialog;<br />import android.widget.TimePicker;<br />import android.widget.DatePicker;<br /><br />
public class MainActivity extends Activity<br />
{ <br />
TimePicker timePicker;<br />
DatePicker datePicker;<br />int hour,minute;<br />
static final int TIME_DIALOG_ID=0;<br /><br />
/**Called when the activity is first created.*/<br />
@Override<br />
public void onCreate(BundlesavedInstanceState)<br />
{ <br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br /><br />
//showDialog(TIME_DIALOG_ID);<br />timePicker=(TimePicker)findViewById(R.id.timePicker); <br />
timePicker.setIs24HourView(true); <br />
datePicker = (DatePicker) findViewById(R.id.datePicker);<br /><br />
//---Buttonview---<br />
Button btnOpen=(Button)findViewById(R.id.btnSet); <br />
btnOpen.setOnClickListener(newView.OnClickListener(){<br />
public void onClick(View v)<br />
{ <br />
Toast.makeText(getBaseContext(), “Date selected:” + datePicker.getMonth() + 1 + “/” + datePicker.getDayOfMonth() + “/” + datePicker.getYear() + “\n” + “Timeselected:”+timePicker.getCurrentHour()+ “:”+timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); } <br />
});<br />
}<br />@Override<br />
protected Dialog onCreateDialog(int id)<br />
{<br />
switch(id)<br />
{ <br />
case TIME_DIALOG_ID: <br />
return newTimePickerDialog( this,mTimeSetListener,hour,minute,false); <br />
}<br />
return null;<br />
}<br /><br />
private TimePickerDialog.OnTimeSetListener mTimeSetListener= newTimePickerDialog.OnTimeSetListener()<br />
{ <br />
public void onTimeSet( TimePicker view,int hourOfDay,int minuteOfHour) <br />
{ <br />
hour=hourOfDay; minute=minuteOfHour; <br />
Toast.makeText(getBaseContext(), “Youhaveselected:“+hour+“:”+minute, Toast.LENGTH_SHORT).show(); <br />
}<br />
};<br />
}<br />
<br />
<i><b>How It Works </b></i><br />
<br />
Like the TimePicker, you call the<b> getMonth()</b>, <b>getDayOfMonth()</b>, and <b>getYear()</b> methods to get the month, day, and year, respectively:<br />
<br />“Dateselected:”+datePicker.getMonth()+1+ “/”+datePicker.getDayOfMonth()+ “/”+datePicker.getYear()+“\n”+<br /><br />
<b>* </b>Note that the <b>getMonth() </b>method returns <b>0 </b>for January, <b>1</b> for February, and so on. Hence, you need to add a one to the result of this method to get the month number.<br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0tag:blogger.com,1999:blog-8784159420963262332.post-34673663171327234152015-03-02T07:13:00.001-08:002015-03-02T07:13:54.837-08:00Android TimePicker Dialog<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Displaying the TimePicker in a Dialog Window </h3>
<br />
While you can display the TimePicker in an activity, a better way is to display it in a dialog window, so that once the time is set, it disappears and doesn’t take up any space in an activity. <br />
<br />
<h4 style="text-align: left;">
Using a Dialog to Display the TimePicker View </h4>
<br />
1 .<br />
<br />import android.app.Activity;<br />
import android.os.Bundle;<br />
import android.view.View;<br />
import android.widget.Button;<br />
import android.widget.TimePicker;<br />
import android.widget.Toast;<br />import android.app.Dialog;<br />
import android.app.TimePickerDialog;<br /><br />
public class MainActivity extends Activity<br />
{<br />
TimePickertimePicker;<br />int hour, minute;<br />
static final int TIME_DIALOG_ID = 0;<br /><br />
/**Called when the activity is first created.*/<br />
<br />
@Override<br />
public void onCreate(BundlesavedInstanceState)<br />
{<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br />showDialog(TIME_DIALOG_ID);<br />timePicker=(TimePicker)findViewById(R.id.timePicker);<br />
timePicker.setIs24HourView(true);<br />//---Buttonview--- <br />
Button btnOpen = (Button) findViewById(R.id.btnSet);<br />
btnOpen.setOnClickListener(newView.OnClickListener()<br />
{<br />
public void onClick(View v)<br />
{ <br />
Toast.makeText(getBaseContext(), “Timeselected:”+ timePicker.getCurrentHour().toString()+“:”+timePicker.getCurrentMinute().toString(), Toast.LENGTH_SHORT).show(); <br />
}<br />
});<br />
}<br /><br />
@Override <br />
protected Dialog onCreateDialog(int id)<br />
{<br />
switch (id)<br />
{<br />
case TIME_DIALOG_ID: return new TimePickerDialog( this, mTimeSetListener, hour, minute, false); } <br />
return null;<br />
}<br /><br />
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {<br />
public void onTimeSet( TimePicker view, int hourOfDay, int minuteOfHour) <br />
{<br />
hour = hourOfDay; <br />
minute = minuteOfHour;<br />
Toast.makeText(getBaseContext(), “You have selected : “ + hour + “:” + minute, Toast.LENGTH_SHORT).show(); <br />
}<br />
};<br />
}<br />
2 . When the activity is loaded, you can see the TimePicker displayed in a dialog window. Set a time and then click the Set button. You will see the Toast window displaying the time that you just set.<br /><br />
<b><i>How It Works </i></b><br />
<br />
To display a dialog window, you use the <b>showDialog() </b>method, passing it an ID to identify the source of the dialog:<br />showDialog(TIME_DIALOG_ID);<br /><br />
When the <b>showDialog()</b> method is called, the <b>onCreateDialog() </b>method will be called:<br /><br />
@Override<br />
protected Dialog onCreateDialog(int id) <br />
{<br />
switch(id)<br />
{ <br />
caseTIME_DIALOG_ID: return new TimePickerDialog( this,mTimeSetListener,hour,minute,false);<br />
}<br />
return null;<br />
}<br />
Here, you create a new instance of the TimePickerDialog class, passing it the current context, the callback, the initial hour and minute, as well as whether the TimePicker should be displayed in 24-hour format. When the user clicks the Set button in the TimePicker dialog window, the <b>onTimeSet() </b>method will be called:<br />
<br />
private TimePickerDialog.OnTimeSetListener mTimeSetListener= newTimePickerDialog.OnTimeSetListener() <br />
{<br />
public void onTimeSet( TimePicker view,int hourOfDay,int minuteOfHour) <br />
{<br />
hour=hourOfDay; <br />
minute=minuteOfHour; <br />
Toast.makeText(getBaseContext(), “Youhaveselected:“+hour+“:”+minute, Toast.LENGTH_SHORT).show();<br />
} <br />
};<br /><br />
Here, the <b>onTimeSet() </b>method will contain the hour and minute set by the user via the hourOfDay and minuteOfHour arguments, respectively.<br /></div>
Anonymoushttp://www.blogger.com/profile/10298581677463736298noreply@blogger.com0