Nov 28, 2011

Download using DownloadManager

The class android.app.DownloadManager (Since: API Level 9) is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots.

It's a basic code to download file from internet using DownloadManager.

Download using DownloadManager
Download using DownloadManager

package com.AndroidDownload;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidDownloadActivity extends Activity {

String Download_path = "http://goo.gl/Mfyya";
String Download_ID = "DOWNLOAD_ID";

SharedPreferences preferenceManager;
DownloadManager downloadManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

preferenceManager = PreferenceManager.getDefaultSharedPreferences(this);
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);

Button btnDownload = (Button)findViewById(R.id.download);
btnDownload.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Uri Download_Uri = Uri.parse(Download_path);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
long download_id = downloadManager.enqueue(request);

//Save the download id
Editor PrefEdit = preferenceManager.edit();
PrefEdit.putLong(Download_ID, download_id);
PrefEdit.commit();
}});
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, intentFilter);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();

unregisterReceiver(downloadReceiver);
}

private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(preferenceManager.getLong(Download_ID, 0));
Cursor cursor = downloadManager.query(query);

if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);

if(status == DownloadManager.STATUS_SUCCESSFUL){
//Retrieve the saved download id
long downloadID = preferenceManager.getLong(Download_ID, 0);

ParcelFileDescriptor file;
try {
file = downloadManager.openDownloadedFile(downloadID);
Toast.makeText(AndroidDownloadActivity.this,
"File Downloaded: " + file.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidDownloadActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}

}else if(status == DownloadManager.STATUS_FAILED){
Toast.makeText(AndroidDownloadActivity.this,
"FAILED!\n" + "reason of " + reason,
Toast.LENGTH_LONG).show();
}else if(status == DownloadManager.STATUS_PAUSED){
Toast.makeText(AndroidDownloadActivity.this,
"PAUSED!\n" + "reason of " + reason,
Toast.LENGTH_LONG).show();
}else if(status == DownloadManager.STATUS_PENDING){
Toast.makeText(AndroidDownloadActivity.this,
"PENDING!",
Toast.LENGTH_LONG).show();
}else if(status == DownloadManager.STATUS_RUNNING){
Toast.makeText(AndroidDownloadActivity.this,
"RUNNING!",
Toast.LENGTH_LONG).show();
}
}
}

};

}


<?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" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/download"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Download" />

</LinearLayout>


remark: "android.permission.INTERNET" is needed in AndroidManifest.xml.

next: Remove from DownloadManager



Download using DownloadManager

The class android.app.DownloadManager (Since: API Level 9) is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots.

It's a basic code to download file from internet using DownloadManager.

Download using DownloadManager
Download using DownloadManager

package com.AndroidDownload;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidDownloadActivity extends Activity {

String Download_path = "http://goo.gl/Mfyya";
String Download_ID = "DOWNLOAD_ID";

SharedPreferences preferenceManager;
DownloadManager downloadManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

preferenceManager = PreferenceManager.getDefaultSharedPreferences(this);
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);

Button btnDownload = (Button)findViewById(R.id.download);
btnDownload.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Uri Download_Uri = Uri.parse(Download_path);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
long download_id = downloadManager.enqueue(request);

//Save the download id
Editor PrefEdit = preferenceManager.edit();
PrefEdit.putLong(Download_ID, download_id);
PrefEdit.commit();
}});
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, intentFilter);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();

unregisterReceiver(downloadReceiver);
}

private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(preferenceManager.getLong(Download_ID, 0));
Cursor cursor = downloadManager.query(query);

if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);

if(status == DownloadManager.STATUS_SUCCESSFUL){
//Retrieve the saved download id
long downloadID = preferenceManager.getLong(Download_ID, 0);

ParcelFileDescriptor file;
try {
file = downloadManager.openDownloadedFile(downloadID);
Toast.makeText(AndroidDownloadActivity.this,
"File Downloaded: " + file.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidDownloadActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}

}else if(status == DownloadManager.STATUS_FAILED){
Toast.makeText(AndroidDownloadActivity.this,
"FAILED!\n" + "reason of " + reason,
Toast.LENGTH_LONG).show();
}else if(status == DownloadManager.STATUS_PAUSED){
Toast.makeText(AndroidDownloadActivity.this,
"PAUSED!\n" + "reason of " + reason,
Toast.LENGTH_LONG).show();
}else if(status == DownloadManager.STATUS_PENDING){
Toast.makeText(AndroidDownloadActivity.this,
"PENDING!",
Toast.LENGTH_LONG).show();
}else if(status == DownloadManager.STATUS_RUNNING){
Toast.makeText(AndroidDownloadActivity.this,
"RUNNING!",
Toast.LENGTH_LONG).show();
}
}
}

};

}


<?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" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/download"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Download" />

</LinearLayout>


remark: "android.permission.INTERNET" is needed in AndroidManifest.xml.

Nov 7, 2011

Interactive between Activity and Service

Modify from last post Pass data from Activity to Service via Intent in startService(); a BroadcastReceiver is added in MyService, such that our main activity (AndroidServiceTestActivity) can send data (CMD) to service via another intent(MY_ACTION_FROMACTIVITY).

Interactive between Activity and Service

MyService.java
package com.AndroidServiceTest;

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

public class MyService extends Service {

MyServiceReceiver myServiceReceiver;

final static String MY_ACTION = "MY_ACTION";
final static String MY_ACTION_FROMACTIVITY = "MY_ACTION_FROMACTIVITY";

public static final String CMD = "CMD";
public static final int CMD_STOP = 1;
boolean running;

String initData;

@Override
public void onCreate() {
// TODO Auto-generated method stub
myServiceReceiver = new MyServiceReceiver();
super.onCreate();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MY_ACTION_FROMACTIVITY);
registerReceiver(myServiceReceiver, intentFilter);
running = true;

initData = intent.getStringExtra("INIT_DATA");

MyThread myThread = new MyThread();
myThread.start();

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(myServiceReceiver);
super.onDestroy();
}

public class MyThread extends Thread{

@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while(running){
try {
Thread.sleep(5000);
Intent intent = new Intent();
intent.setAction(MY_ACTION);

intent.putExtra("DATAPASSED", i);
intent.putExtra("DATA_BACK", initData);

sendBroadcast(intent);

i++;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopSelf();
}

}

public class MyServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int hostCmd = arg1.getIntExtra(CMD, 0);
if(hostCmd == CMD_STOP){
running = false;
stopSelf();

Toast.makeText(MyService.this,
"Service stopped by main Activity!",
Toast.LENGTH_LONG).show();
}
}

}

}


Modify main activity (AndroidServiceTestActivity.java) to send command to stop our service, by pressing of the Stop button.
package com.AndroidServiceTest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidServiceTestActivity extends Activity {

MyReceiver myReceiver;

Button btnStop;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStop = (Button)findViewById(R.id.stop);
btnStop.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(MyService.MY_ACTION_FROMACTIVITY);
intent.putExtra(MyService.CMD, MyService.CMD_STOP);
sendBroadcast(intent);
}});

}

@Override
protected void onStart() {
// TODO Auto-generated method stub

//Register BroadcastReceiver
//to receive event from our service
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);

//Start our own service
Intent intent = new Intent(AndroidServiceTestActivity.this,
com.AndroidServiceTest.MyService.class);
intent.putExtra("INIT_DATA", "Data passed from Activity to Service in startService");
startService(intent);

super.onStart();
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onStop();
}

private class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub

int datapassed = arg1.getIntExtra("DATAPASSED", 0);
String orgData = arg1.getStringExtra("DATA_BACK");

Toast.makeText(AndroidServiceTestActivity.this,
"Triggered by Service!\n"
+ "Data passed: " + String.valueOf(datapassed) + "\n"
+ "original Data: " + orgData,
Toast.LENGTH_LONG).show();

}

}
}


Modify layout to add the Stop button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop"
/>
</LinearLayout>

Nov 3, 2011

Pass data from Activity to Service via Intent in startService()

In this example, modify from last post Pass data from Service to Activity, Extra is pass from Activity to Service in startService(). It will be retrieved in onStartCommand() of Service.

Pass data from Activity to Service via Intent in startService()

AndroidServiceTestActivity.java
package com.AndroidServiceTest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class AndroidServiceTestActivity extends Activity {

MyReceiver myReceiver;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

@Override
protected void onStart() {
// TODO Auto-generated method stub

//Register BroadcastReceiver
//to receive event from our service
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);

//Start our own service
Intent intent = new Intent(AndroidServiceTestActivity.this,
com.AndroidServiceTest.MyService.class);
intent.putExtra("INIT_DATA", "Data passed from Activity to Service in startService");
startService(intent);

super.onStart();
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onStop();
}

private class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub

int datapassed = arg1.getIntExtra("DATAPASSED", 0);
String orgData = arg1.getStringExtra("DATA_BACK");

Toast.makeText(AndroidServiceTestActivity.this,
"Triggered by Service!\n"
+ "Data passed: " + String.valueOf(datapassed) + "\n"
+ "original Data: " + orgData,
Toast.LENGTH_LONG).show();

}

}
}


package com.AndroidServiceTest;

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

public class MyService extends Service {

final static String MY_ACTION = "MY_ACTION";
String initData;

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

initData = intent.getStringExtra("INIT_DATA");

MyThread myThread = new MyThread();
myThread.start();

return super.onStartCommand(intent, flags, startId);
}

public class MyThread extends Thread{

@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<10; i++){
try {
Thread.sleep(5000);
Intent intent = new Intent();
intent.setAction(MY_ACTION);

intent.putExtra("DATAPASSED", i);
intent.putExtra("DATA_BACK", initData);

sendBroadcast(intent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopSelf();
}

}

}


Note: Modify AndroidManifest.xml to add service ".MyService", refer to the post Create our own Service and BroadcastReceiver.

Next post:
- Interactive between Activity and Service



Nov 2, 2011

Pass data from Service to Activity

Refer to the last post Create our own Service and BroadcastReceiver, data can be passed from Service using intent.putExtra(); on the other hand, the data passed can be retrieved using getIntExtra() in BroadcastReceiver.

Pass data from Service to Activity

Modify from the last post Create our own Service and BroadcastReceiver.
AndroidServiceTestActivity.java
package com.AndroidServiceTest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class AndroidServiceTestActivity extends Activity {

MyReceiver myReceiver;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

@Override
protected void onStart() {
// TODO Auto-generated method stub

//Register BroadcastReceiver
//to receive event from our service
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);

//Start our own service
Intent intent = new Intent(AndroidServiceTestActivity.this,
com.AndroidServiceTest.MyService.class);
startService(intent);

super.onStart();
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onStop();
}

private class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub

int datapassed = arg1.getIntExtra("DATAPASSED", 0);

Toast.makeText(AndroidServiceTestActivity.this,
"Triggered by Service!\n"
+ "Data passed: " + String.valueOf(datapassed),
Toast.LENGTH_LONG).show();

}

}
}


MyService.java
package com.AndroidServiceTest;

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

public class MyService extends Service {

final static String MY_ACTION = "MY_ACTION";

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

MyThread myThread = new MyThread();
myThread.start();

return super.onStartCommand(intent, flags, startId);
}

public class MyThread extends Thread{

@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<10; i++){
try {
Thread.sleep(5000);
Intent intent = new Intent();
intent.setAction(MY_ACTION);

intent.putExtra("DATAPASSED", i);

sendBroadcast(intent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopSelf();
}

}

}


Note: Modify AndroidManifest.xml to add service ".MyService", refer to the post Create our own Service and BroadcastReceiver.

Next:
- Pass data from Activity to Service via Intent in startService()



Nov 1, 2011

Create our own Service and BroadcastReceiver

In this onStart() of the main activity(AndroidServiceTestActivity.java), we instance and register our own BroadcastReceiver(myReceiver), and also start our service(MyService.java). In MyService.java, It will send 10 actions with MY_ACTION. It will trigger the myReceiver in AndroidServiceTestActivity.java.

Create our own Service and BroadcastReceiver

Main activity AndroidServiceTestActivity.java
package com.AndroidServiceTest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class AndroidServiceTestActivity extends Activity {

MyReceiver myReceiver;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

@Override
protected void onStart() {
// TODO Auto-generated method stub

//Register BroadcastReceiver
//to receive event from our service
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);

//Start our own service
Intent intent = new Intent(AndroidServiceTestActivity.this,
com.AndroidServiceTest.MyService.class);
startService(intent);

super.onStart();
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onStop();
}

private class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Toast.makeText(AndroidServiceTestActivity.this,
"Triggered by Service!",
Toast.LENGTH_LONG).show();

}

}
}


MyService.java

package com.AndroidServiceTest;

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

public class MyService extends Service {

final static String MY_ACTION = "MY_ACTION";

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

MyThread myThread = new MyThread();
myThread.start();

return super.onStartCommand(intent, flags, startId);
}

public class MyThread extends Thread{

@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<10; i++){
try {
Thread.sleep(5000);
Intent intent = new Intent();
intent.setAction(MY_ACTION);
sendBroadcast(intent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopSelf();
}

}

}


Modify AndroidManifest.xml to add service ".MyService"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidServiceTest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidServiceTestActivity"
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=".MyService"/>
</application>
</manifest>


next:
- Pass data from Service to Activity



Infolinks In Text Ads