Showing posts with label service. Show all posts
Showing posts with label service. Show all posts

Sunday, 17 November 2013

Difference between Android AsyncTask,Thread,IntentService and Service


Service
Thread
IntentService
AsyncTask
When to use ?
Task with no UI, but shouldn't be too long. Use threads within service for long tasks.
- Long task in general.

- For tasks in parallel use Multiple threads (traditional mechanisms)
- Long task usually with no communication to main thread.
(Update)- If communication is required, can use main thread handler or broadcast intents

- When callbacks are needed (Intent triggered tasks). 
- Small task having to communicate with main thread.

- For tasks in parallel use multiple instances OR Executor
 
Trigger
Call to method
onStartService()
Thread start() method
Intent
Call to method execute()
Triggered From (thread)
Any thread
Any Thread
Main Thread (Intent is received on main thread and then worker thread is spawed)
Main Thread
Runs On (thread)
Main Thread
Its own thread
Separate worker thread
Worker thread. However, Main thread methods may be invoked in between to publish progress.
Limitations /
Drawbacks
May block main thread
- Manual thread management

- Code may become difficult to read
- Cannot run tasks in parallel.

- Multiple intents are queued on the same worker thread.
- one instance can only be executed once (hence cannot run in a loop) 

- Must be created and executed from the Main thread

Content from : techtej

Saturday, 22 October 2011

Android Alarm Service Example

Here below code is start alarm on particular time and stop alarm .

Here i create 2 buttons one is for start alarm after clicking on button 30 sec's and other stop alarm
button for stop.

Android UI Layout(main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/startalarm"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start Alarm" />

    <Button
        android:id="@+id/stopalarm"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Stop Alarm" />

</LinearLayout>

Android Alarm Service (AlarmService.java)

package com.androidsurya.androidalarm;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class AlarmService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "AlaramService onCreate() called.",
Toast.LENGTH_SHORT).show();
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "AlaramService onBind() called.",
Toast.LENGTH_SHORT).show();
return null;

}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "AlaramService onDestroy() called.",
Toast.LENGTH_SHORT).show();

}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
// Here you can add your own code to play any sound using media player
Toast.makeText(this, "AlaramService onStart() called.",
Toast.LENGTH_SHORT).show();
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
// Here you can add your own code to media player which is all ready
// started early in onStart() method
Toast.makeText(this, "AlaramService onUnbind() called.",
Toast.LENGTH_SHORT).show();
return super.onUnbind(intent);

}
}

Android Activity(MainActivity.java)

package com.androidsurya.androidalarm;

import java.util.Calendar;

import com.androidsurya.androidalarm.R;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
PendingIntent pendingIntent;
Button startAlarm, stopAlarm;
AlarmManager alarmManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startAlarm = (Button) findViewById(R.id.startalarm);
stopAlarm = (Button) findViewById(R.id.stopalarm);
startAlarm.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getApplicationContext(),
AlarmService.class);
pendingIntent = PendingIntent.getService(
getApplicationContext(), 0, myIntent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(getApplicationContext(), "Start Alarm",
Toast.LENGTH_LONG).show();

}
});

stopAlarm.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(getApplicationContext(), "Stop Alarm!",
Toast.LENGTH_LONG).show();
}
});

}

}

Register Activity and Service in Android Manifest file

        <activity
            android:name="com.androidsurya.androidalarm.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service android:name=".AlarmService" />

Output Screenshot:



























































Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...