예제 #1
0
    /** Handles sending SMS over Google Voice or built-in SMS */
    @Override
    protected String doInBackground(Void... arg0) {
      String response = "";
      Log.i(TAG, "Async sending message");

      try {

        if (googleVoiceEnabled) {
          Log.i(TAG, "Sending via GV");

          // Get the authtoken
          OAuth2Helper oauthHelper = new OAuth2Helper();
          String authToken = oauthHelper.getRefreshedAuthToken(activity, GV_SERVICE);
          Log.i(TAG, "authToken = " + authToken);

          if (gvHelper == null) {
            gvHelper = new GoogleVoiceUtil(authToken);
          }
          if (gvHelper.isInitialized()) {
            response = gvHelper.sendGvSms();
            Log.i(TAG, "Sent SMS, response = " + response);
          } else {
            return "IO Error: unable to create GvHelper";
          }
        } else {
          Log.i(TAG, "Sending via built-in Sms");

          PendingIntent pendingIntent =
              PendingIntent.getBroadcast(activity, 0, new Intent(SENT), 0);

          // Receiver for when the SMS is sent
          BroadcastReceiver sendReceiver =
              new BroadcastReceiver() {
                @Override
                public synchronized void onReceive(Context arg0, Intent arg1) {
                  try {
                    handleSentMessage(arg0, null, getResultCode(), message);
                    activity.unregisterReceiver(this);
                  } catch (Exception e) {
                    Log.e("BroadcastReceiver", "Error in onReceive for msgId " + arg1.getAction());
                    Log.e("BroadcastReceiver", e.getMessage());
                    e.printStackTrace();
                  }
                }
              };
          // This may result in an error -- a "sent" or "error" message will be displayed
          activity.registerReceiver(sendReceiver, new IntentFilter(SENT));
          smsManager.sendTextMessage(phoneNumber, null, message, pendingIntent, null);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return response;
    }