Posts

Showing posts from October, 2013

Creating a TimePicker in Android

Android gives controls for the user to pick a time as ready-to-use dialogs. Each picker gives controls for selecting each part of the time (hour, minute, AM/PM). Using these pickers helps ensure that your users can pick a time is valid, formatted correctly, and adjusted to the user's locale. We can get TimePickerDialog through DialogFragment and TimePickerDialog.OnTimeSetListener. Create Android Project with name as "TimePickerDemo" with package name "com.etr.timepickerdemo". Create Activity with name as "MainActivity" and it will fetch time. This is the code for MainActivity. package com.etr.timepickerdemo; import java.util.Calendar; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; import android.text.format.DateFormat; import android.view.Menu; import android.view.View; import android.widget.Butt

Record Video in Android using Camera Intent

We can record video in android using camera intent.An intent action type of MediaStore.ACTION_VIDEO_CAPTURE or MediaStore.ACTION_IMAGE_CAPTURE can be used to capture images or videos or without directly using the Camera object. Create Android Project with name as "StartVideo" with package name "com.etr.startvideo". Create Activity with name as "VideoActivity" and it will start video record. This is the code for VideoActivity. package com.etr.startvideo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class VideoActivity extends Activity { public static final int code = 1111; Button start_video; @Override protected void onCreate(Bundle savedInstanceState) { try { super

Captured Image in Android using Camera Intent

We can capture image in android using camera intent.An intent action type of MediaStore.ACTION_VIDEO_CAPTURE or MediaStore.ACTION_IMAGE_CAPTURE can be used to capture videos or images without directly using the Camera object. Create Android Project with name as "StartCamera" with package name "com.etr.startcamera". Create Activity with name as "CameraActivity" and it will start camera. This is the code for CameraActivity. package com.etr.startcamera; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class CameraActivity extends Activity { public static final int code = 1337; Button start_camera; @Override protected void onCreate(Bundle savedInstanceState) { try { super.

Programmatically turn ON/OFF WiFi on Android device

WifiManager provides the primary API for managing all aspects of WI-FI connectivity.we can get WifiManager instance through  Context.getSystemService(Context.WIFI_SERVICE). WifiManager is used to find the currently active Wi-Fi network and change the Wi-Fi State. Create Android Project with name as "TurnOnWIFI" with package name "com.etr.turnonwifi". Create Activity with name as "MainActivity" and it will turn ON/OFF Wi-Fi. This is the code for MainActivity. package com.etr.turnonwifi; import android.app.Activity; import android.content.Context; import android.net.wifi.WifiManager; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button start, stop; @Override protected void onCreate(Bundle savedInstanceState) { try { su

AlarmManager in Android

AlarmManager is used to start the future actions.Example: Birthday SMS scheduler . AlarmManager is a class which is used to access the system alarm service.These allow you to schedule your application to be run at some point in the future.When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. When the device is in sleep mode registered alarms are retained,but will be cleared if it is turned off and rebooted.You do not instantiate this class directly, instead of that we can get it through Context.getSystemService(Context.ALARM_SERVICE). Create Android Project with name as "AlarmManagerDemo" with package name "com.etr.alarmmanagerdemo". Create Activity with name as "AlarmManagerActivity" and it will start the Alarm. This is the code for AlarmManagerActivity. package com.etr.alarmmanagerdemo; import android.app.Activity; import andr

Timer in Android

Timers will schedule and execute the tasks(one-shot or recurring).Each timer has one thread,where the tasks executes sequentially. When this thread is busy running a task, runnable tasks may be subject to delays. One-shot are scheduled with either a absolute time or after a relative delay. Recurring tasks are scheduled to run at an fixed period or a fixed rate. Fixed-period execution: It Runs based on the start time of the previous run. Fixed-rate execution: It doesn't based on the start time of the previous run. cancel() is used to stop the Timer.  Create Android Project with name as "TimerDemo" with package name  "com.android.timerdemo". Create Activity with name as "TimerDemo" and it will start the Timers. This is the code for TimerDemo. package com.android.timerdemo; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import

Android Service and IntentService

Service is an application component. Service which runs on background for a long time without a help of user interface. Service can even start up from another application component like Activity,Broadcast receiver and it will continue to run in the background even if the user switches to another application. For example, a service might handle network transactions, play music, perform file I/O from the background. Android has two type of service namely 1.Service 2.IntentService Difference between these two services are Service run on main UI Thread, while IntentService creates a worker thread and uses that thread to run the service. IntentService creates a queue that passes one intent at a time to onHandleIntent(). Service want to stop manually by stopService() and stopSelf(). Meanwhile,IntentService will stop automatically after the completion of work. IntentService implements onStartCommand() that sends Intent to queue and to onHandleIntent(). Create Android P