Thursday, May 31, 2012

Scheduling background task using Alarm Manager Android

All of us know, what Alarm does. Alarms will run in the background, whenever it counter the time, plays nice Music. Alarm wake-up us. Every phone has this feature from low end to high end mobiles.

In Android, we can use Alarms to schedule the tasks. Android has Alarm manager to manage these Alarms.

Service, perform long-running operations in the background and does not provide a user interface.

We need to use Alarm manger as well as service for scheduling background task like downloading information at particular time.

Steps to Create background task :
1. Create project, specify default activity. The following code to set schedule and cancel schedule for background task
public void btnStartSchedule(View v) {

  try {
   AlarmManager alarms = (AlarmManager) this
     .getSystemService(Context.ALARM_SERVICE);

   Intent intent = new Intent(getApplicationContext(),
     AlaramReceiver.class);
   intent.putExtra(AlaramReceiver.ACTION_ALARM,
     AlaramReceiver.ACTION_ALARM);

   final PendingIntent pIntent = PendingIntent.getBroadcast(this,
     1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);

   alarms.setRepeating(AlarmManager.RTC_WAKEUP,
     System.currentTimeMillis(), 10000, pIntent);
   
   toast("Started...");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 public void btnCancelSchedules(View v) {

  Intent intent = new Intent(getApplicationContext(),
    AlaramReceiver.class);
  intent.putExtra(AlaramReceiver.ACTION_ALARM,
    AlaramReceiver.ACTION_ALARM);

  final PendingIntent pIntent = PendingIntent.getBroadcast(this, 1234567,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);

  AlarmManager alarms = (AlarmManager) this
    .getSystemService(Context.ALARM_SERVICE);

  alarms.cancel(pIntent);
  toast("Canceled...");
 }
2. Create AlarmReceiver, for waking up the task
public static String ACTION_ALARM = "com.alarammanager.alaram";

 @Override
 public void onReceive(Context context, Intent intent) {
  
  Log.i("Alarm Receiver", "Entered");
  Toast.makeText(context, "Entered", Toast.LENGTH_SHORT).show();
  
  Bundle bundle = intent.getExtras();
  String action = bundle.getString(ACTION_ALARM);
  if (action.equals(ACTION_ALARM)) {
 Log.i("Alarm Receiver", "If loop");
 Intent inService = new Intent(context,TaskService.class);
 context.startService(inService);
  }
  else
  {
 Log.i("Alarm Receiver", "Else loop");
 Toast.makeText(context, "Else loop", Toast.LENGTH_SHORT).show();
  }
 }
3. Create a Service do background task
public class TaskService extends IntentService {

 public TaskService() {
  super("TaskService");
  // TODO Auto-generated constructor stub
 }

 @Override
 protected void onHandleIntent(Intent arg0) {
  
  // Do some task
Log.i("TaskService","Service running");
 }

}
4. Specify the receiver and service in the Manifest file.

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AlaramScheduleActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="AlaramReceiver"
            android:process=":remote" >
        </receiver>

        <service android:name=".TaskService" >
        </service>
    </application>
References : download_source_code

Saturday, May 5, 2012

My New Mobile HTC One X and its Reviews

Recently, Went to service center for repairing my old phone Samsung galaxy Captivate. They told that phone screen is damaged and have to replace the screen. i asked him that “How much will you charge for changing screen”. He told that it will cost around 10,000. I shocked and realized it is the time to change the phone. 

I have done market research on android phones as well as apple, Came to know that HTC One series mobile are good as per software and hardware. 

Checked HTC One X features and bought it.

Specifications :
Processor : 1.5 GHz Quad core processor
Display : 4.7-inch (1280 x 720 resolution) - HD 720p touch
RAM : 1 GB
Music: Beats Audio
Connectivity : NFC, Wi Fi, Wi Fi Direct, Bluetooth
Camera : 
  • 8 megapixel camera with auto focus, smart LED flash, and BSI sensor (for better low-light captures)
  • 1.3 megapixel front camera (720p for video chat)
Memory : 32GB Internal memory
Software : ICS - 4.0.3

Reviews : 
  1. Camera quality is extra ordinary
  2. Audio is awesome
  3. Display is mind blowing 
  4. Battery life is not too bad
Coming to negatives, it does not have extra SD card port. i feel 32 GB is enough for mobile devices. Also, phone heats a lot whenever playing games and using camera for more time.




At the end, i feel HTC OneX is the best phone in the market as of now. 


References : http://www.htc.com/www/smartphones/htc-one-x/