Back to project page ServicesTutorial.
The source code is released under:
Apache License
If you think the Android project ServicesTutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yanlu.android.services.app.service; //from w w w . j av a 2 s . c o m import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.SystemClock; import android.support.v4.app.NotificationCompat; import com.yanlu.android.services.app.MainActivity; import com.yanlu.android.services.app.R; import com.yanlu.android.services.app.utils.ThreadUtils; public class MyIntentService extends IntentService { private static final String TAG = "MyIntentService"; private static final String ACTION_FOO = "com.yanlu.android.services.app.service.action.FOO"; private static final String ACTION_BAZ = "com.yanlu.android.services.app.service.action.BAZ"; private static final String EXTRA_PARAM1 = "com.yanlu.android.services.app.service.extra.PARAM1"; private static final String EXTRA_PARAM2 = "com.yanlu.android.services.app.service.extra.PARAM2"; /** * Starts this service to perform action Foo with the given parameters. If * the service is already performing a task this action will be queued. * * @see IntentService */ public static void startActionFoo(Context context, String param1, String param2) { Intent intent = new Intent(context, MyIntentService.class); intent.setAction(ACTION_FOO); intent.putExtra(EXTRA_PARAM1, param1); intent.putExtra(EXTRA_PARAM2, param2); context.startService(intent); } /** * Starts this service to perform action Baz with the given parameters. If * the service is already performing a task this action will be queued. * * @see IntentService */ public static void startActionBaz(Context context, String param1, String param2) { Intent intent = new Intent(context, MyIntentService.class); intent.setAction(ACTION_BAZ); intent.putExtra(EXTRA_PARAM1, param1); intent.putExtra(EXTRA_PARAM2, param2); context.startService(intent); } public MyIntentService() { super("MyIntentService"); ThreadUtils.logThreadSignature(); } @Override protected void onHandleIntent(Intent intent) { ThreadUtils.logThreadSignature(); if (intent != null) { final String action = intent.getAction(); if (ACTION_FOO.equals(action)) { final String param1 = intent.getStringExtra(EXTRA_PARAM1); final String param2 = intent.getStringExtra(EXTRA_PARAM2); handleActionFoo(param1, param2); } else if (ACTION_BAZ.equals(action)) { final String param1 = intent.getStringExtra(EXTRA_PARAM1); final String param2 = intent.getStringExtra(EXTRA_PARAM2); handleActionBaz(param1, param2); } } } /** * Handle action Foo in the provided background thread with the provided * parameters. */ private void handleActionFoo(String param1, String param2) { displayNotificationMessage(param1 + " = " + param2); } /** * Handle action Baz in the provided background thread with the provided * parameters. */ private void handleActionBaz(String param1, String param2) { // TODO: Handle action Baz throw new UnsupportedOperationException("Not yet implemented"); } private void displayNotificationMessage(String message) { NotificationManager notificationMgr =(NotificationManager)getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); builder.setContentTitle("MyIntentService") .setContentText(message) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) .setOngoing(true) .setTicker("MyIntentService") .setContentIntent(contentIntent); for(int i = 0; i < 20; i++) { builder.setProgress(20, i, false); notificationMgr.notify(1, builder.build()); SystemClock.sleep(2000); } builder.setProgress(20, 20, false); Notification notification = builder.build(); notificationMgr.notify(1, notification); } }