Local service demo
package app.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
class BackgroundService extends Service
{
private static final String TAG = "BackgroundService";
private NotificationManager notificationMgr;
private ThreadGroup myThreads = new ThreadGroup("ServiceWorker");
@Override
public void onCreate() {
super.onCreate();
notificationMgr =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
displayNotificationMessage("Background Service is running");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
int counter = intent.getExtras().getInt("counter");
new Thread(myThreads, new ServiceWorker(counter), "BackgroundService")
.start();
return START_NOT_STICKY;
}
class ServiceWorker implements Runnable
{
private int counter = -1;
public ServiceWorker(int counter) {
this.counter = counter;
}
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
@Override
public void onDestroy()
{
myThreads.interrupt();
notificationMgr.cancelAll();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void displayNotificationMessage(String message)
{
Notification notification = new Notification(R.drawable.icon,message, System.currentTimeMillis());
notification.flags = Notification.FLAG_NO_CLEAR;
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, TAG, message, contentIntent);
notificationMgr.notify(0, notification);
}
}
public class Test extends Activity
{
private int counter = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void doClick(View view) {
switch(view.getId()) {
case R.id.startBtn:
Intent intent = new Intent(Test.this,
BackgroundService.class);
intent.putExtra("counter", counter++);
startService(intent);
break;
case R.id.stopBtn:
stopService();
}
}
private void stopService() {
if(stopService(new Intent(Test.this,
BackgroundService.class)))
Log.v("", "stopService was successful");
else
Log.v("", "stopService was unsuccessful");
}
@Override
public void onDestroy()
{
stopService();
super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<Button android:id="@+id/startBtn"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Start Service" android:onClick="doClick" />
<Button android:id="@+id/stopBtn"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Stop Service" android:onClick="doClick" />
</LinearLayout>
Related examples in the same category