Code example for Intent

Methods: getActiongetBundleExtragetParcelableExtragetStringExtra

0
    super("InterpreterService"); 
  } 
   
  @Override 
  protected void onHandleIntent(Intent intent) {
    String action=intent.getAction();
    I_Interpreter interpreter=interpreters.get(action);  
     
    if (interpreter==null) {
      try { 
        interpreter=(I_Interpreter)Class.forName(action).newInstance();
        interpreters.put(action, interpreter);
      } 
      catch (Throwable t) {
        Log.e("InterpreterService", "Error creating interpreter", t);
      } 
    } 
     
    if (interpreter==null) {
      failure(intent, "Could not create interpreter: "+intent.getAction());
    } 
    else { 
      try { 
        success(intent, interpreter.executeScript(intent.getBundleExtra(BUNDLE)));
      } 
      catch (Throwable t) {
        Log.e("InterpreterService", "Error executing script", t);
         
        try { 
          failure(intent, t);
        } 
        catch (Throwable t2) {
          Log.e("InterpreterService",
                "Error returning exception to client", 
                t2);
        } 
      } 
    } 
  } 
   
  private void success(Intent intent, Bundle result) {
    Intent data=new Intent();
 
    data.putExtras(result);
    data.putExtra(RESULT_CODE, SUCCESS);
 
    send(intent, data);
  } 
   
  private void failure(Intent intent, String message) {
    Intent data=new Intent();
 
    data.putExtra(ERROR, message);
    data.putExtra(RESULT_CODE, FAILURE);
 
    send(intent, data);
  } 
   
  private void failure(Intent intent, Throwable t) {
    Intent data=new Intent();
 
    data.putExtra(ERROR, t.getMessage());
    data.putExtra(TRACE, getStackTrace(t));
    data.putExtra(RESULT_CODE, FAILURE);
 
    send(intent, data);
  } 
   
  private void send(Intent intent, Intent data) {
    String broadcast=intent.getStringExtra(BROADCAST_ACTION);
     
    if (broadcast==null) {
      PendingIntent pi=(PendingIntent)intent.getParcelableExtra(PENDING_RESULT);
       
      if (pi!=null) {
        try { 
          pi.send(this, Activity.RESULT_OK, data);
        } 
        catch (PendingIntent.CanceledException e) {
          // no-op -- client must be gone 
        } 
      } 
    } 
    else { 
      data.setPackage(intent.getStringExtra(BROADCAST_PACKAGE));
      data.setAction(broadcast);
 
      sendBroadcast(data);
    } 
  }