コード例 #1
0
  @Override
  public final void onHandleIntent(Intent intent) {

    System.out.println("Received intent " + intent);

    try {
      Context context = getApplicationContext();
      String action = intent.getAction();
      if (action.equals(INTENT_FROM_GCM_REGISTRATION_CALLBACK)) {
        GCMRegistrar.setRetryBroadcastReceiver(context);
        handleRegistration(context, intent);
      } else if (action.equals(INTENT_FROM_GCM_MESSAGE)) {
        // checks for special messages
        String messageType = intent.getStringExtra(EXTRA_SPECIAL_MESSAGE);
        if (messageType != null) {
          if (messageType.equals(VALUE_DELETED_MESSAGES)) {
            String sTotal = intent.getStringExtra(EXTRA_TOTAL_DELETED);
            if (sTotal != null) {
              try {
                int total = Integer.parseInt(sTotal);
                mLogger.log(
                    Log.VERBOSE, "Received notification for %d deleted" + "messages", total);
                onDeletedMessages(context, total);
              } catch (NumberFormatException e) {
                mLogger.log(
                    Log.ERROR, "GCM returned invalid " + "number of deleted messages (%d)", sTotal);
              }
            }
          } else {
            // application is not using the latest GCM library
            mLogger.log(Log.ERROR, "Received unknown special message: %s", messageType);
          }
        } else {
          onMessage(context, intent);
        }
      } else if (action.equals(INTENT_FROM_GCM_LIBRARY_RETRY)) {
        String packageOnIntent = intent.getPackage();
        if (packageOnIntent == null
            || !packageOnIntent.equals(getApplicationContext().getPackageName())) {
          mLogger.log(
              Log.ERROR, "Ignoring retry intent from another package (%s)", packageOnIntent);
          return;
        }
        // retry last call
        if (GCMRegistrar.isRegistered(context)) {
          GCMRegistrar.internalUnregister(context);
        } else {
          String[] senderIds = getSenderIds(context);
          GCMRegistrar.internalRegister(context, senderIds);
        }
      }
    } finally {
      // Release the power lock, so phone can get back to sleep.
      // The lock is reference-counted by default, so multiple
      // messages are ok.

      // If onMessage() needs to spawn a thread or do something else,
      // it should use its own lock.
      synchronized (LOCK) {
        // sanity check for null as this is a public method
        if (sWakeLock != null) {
          sWakeLock.release();
        } else {
          // should never happen during normal workflow
          mLogger.log(Log.ERROR, "Wakelock reference is null");
        }
      }
    }
  }
コード例 #2
0
 {
     int i = Integer.parseInt(intent);
     Log.v("GCMBaseIntentService", (new StringBuilder()).append("Received deleted messages notification: ").append(i).toString());
     onDeletedMessages(((Context) (obj)), i);
     continue; /* Loop/switch isn't completed */
 }