extends IntentService : Intent « Core Class « Android






extends IntentService

    
package app.test;

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class Test extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    logEvent("CREATE");
  }

  @Override
  public void onStart() {
    super.onStart();
    logEvent("START");
  }

  @Override
  public void onResume() {
    super.onResume();
    logEvent("RESUME");
  }

  @Override
  public void onPause() {
    super.onPause();
    logWarning("PAUSE");
  }

  @Override
  public void onStop() {
    super.onStop();
    logWarning("STOP");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    logWarning("DESTROY");
  }

  private void logEvent(String event) {
    Intent intent = new Intent(this, OperationsManager.class);
    intent.setAction(OperationsManager.ACTION_EVENT);
    intent.putExtra(OperationsManager.EXTRA_NAME, event);

    startService(intent);
  }

  private void logWarning(String event) {
    Intent intent = new Intent(this, OperationsManager.class);
    intent.setAction(OperationsManager.ACTION_WARNING);
    intent.putExtra(OperationsManager.EXTRA_NAME, event);

    startService(intent);
  }
}

class OperationsManager extends IntentService {

  public static final String ACTION_EVENT = "ACTION_EVENT";
  public static final String ACTION_WARNING = "ACTION_WARNING";
  public static final String ACTION_ERROR = "ACTION_ERROR";
  public static final String EXTRA_NAME = "eventName";

  private static final String LOGTAG = "EventLogger";

  private IntentFilter matcher;

  public OperationsManager() {
    super("OperationsManager");
    matcher = new IntentFilter();
    matcher.addAction(ACTION_EVENT);
    matcher.addAction(ACTION_WARNING);
    matcher.addAction(ACTION_ERROR);
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    if (!matcher.matchAction(intent.getAction())) {
      Toast.makeText(this, "OperationsManager: Invalid Request",
          Toast.LENGTH_SHORT).show();
      return;
    }
    if (TextUtils.equals(intent.getAction(), ACTION_EVENT)) {
      logEvent(intent.getStringExtra(EXTRA_NAME));
    }
  }

  private void logEvent(String name) {
    try {
      Thread.sleep(5000);
      Log.i(LOGTAG, name);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

   
    
    
    
  








Related examples in the same category

1.Using Intent to open other Activity
2.Create Intent to open a Uri
3.Phone Intent
4.Map Intent
5.Market Intent
6.Load Activity with Intent
7.Using Intent to make another phone call
8.extends IntentService to create your own Intent
9.Adding data bundle to Intent
10.Using Intent to show other Activities
11.Start Intent with Utility class
12.Load library Activity with Intent
13.Capture Image with Intent
14.Intent.ACTION_MEDIA_MOUNTED
15.Using Intent to record audio
16.Video Player Intent
17.Video Capture Intent
18.Implementing an application service that will run in response to an alarm, allowing us to move long duration work out of an intent receiver.
19.Example of various Intent flags to modify the activity stack.
20.Sample code that invokes the speech recognition intent API.
21.extends IntentService to upload a file
22.factory class for generating various intents
23.Open Web Page Intent
24.Is Intent Available
25.start MMS Intent
26.Access the Internet
27.Pdf viewer
28.Rotation One Demo
29.Media activity