2014-01-30 86 views
0

我有這個應用程序的AppWidget,其中當用戶點擊它,它會發送一條短信。通過點擊小部件發送短信可以正常工作。現在,我想知道SMS的發送是成功還是失敗。我將如何做到這一點?如何在BroadcastReceiver AppWidgetProvider類中註冊broadcastreceiver?

這裏是發送短信

String sms2 = id + " " + name + " " + cn.getName() + " " + message 
            + " " + lati + " " + longi 
            + " https://maps.google.com/?q=" + lati + "," + longi + " -alertoapp"; 
          String cp = cn.getPhoneNumber(); 
          PendingIntent piSent=PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0); 
          PendingIntent piDelivered=PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0); 
          SmsManager sms = SmsManager.getDefault(); 
          sms.sendTextMessage(cp, null, sms2, piSent, piDelivered); 

的代碼清單

<receiver android:name=".Widget" android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      <!-- Broadcast Receiver that will also process our self created action --> 
      <action android:name="de.thesmile.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER"/> 
     </intent-filter> 
     <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" /> 
    </receiver> 

我想用這個方法下面這對烤如果短信發送或不工作,但問題是registerReceiver方法在BroadCastReceiver類中不可用。

switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       clear1(); 
       clear2(); 
       clear3(); 
       clear4(); 
       Toast.makeText(getBaseContext(), "SMS has been sent", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
       Toast.makeText(getBaseContext(), "Generic Failure", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NO_SERVICE: 
       Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NULL_PDU: 
       Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_RADIO_OFF: 
       Toast.makeText(getBaseContext(), "Radio Off", Toast.LENGTH_SHORT).show(); 
       break; 
      default: 
       Toast.makeText(getApplicationContext(), "Coordinates is null, Try again", Toast.LENGTH_LONG).show(); 
       break; 
      } 

     } 
    }; 
    smsDeliveredReceiver=new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      // TODO Auto-generated method stub 
      switch(getResultCode()) { 
      case Activity.RESULT_OK: 
       Toast.makeText(getBaseContext(), "SMS Delivered", Toast.LENGTH_SHORT).show(); 
       break; 
      case Activity.RESULT_CANCELED: 
       Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show(); 
       break; 
      } 
     } 
    }; 
    registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT")); 
    registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED")); 

任何建議傢伙?

回答

1

爲了那些我遇到了同樣的問題,我已經用this

的幫助,所以在這裏固定這是

smsSentReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context ctx, Intent arg1) { 
     String msg = ""; 
     switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       msg = "SMS has been sent"; 
       break; 
      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
       msg = "Generic Failure"; 
       break; 
      case SmsManager.RESULT_ERROR_NO_SERVICE: 
       msg = "No Service"; 
       break; 
      case SmsManager.RESULT_ERROR_NULL_PDU: 
       msg = "Null PDU"; 
       break; 
      case SmsManager.RESULT_ERROR_RADIO_OFF: 
       msg = "Radio Off"; 
       break; 
      default: 
       msg = "Coordinates is null, Try again"; 
       break; 
     } 
     Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 
    } 
}; 

smsDeliveredReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context ctx, Intent arg1) { 
     String msg = ""; 
     switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       msg = "SMS Delivered"; 
       break; 
      case Activity.RESULT_CANCELED: 
       msg = "SMS not delivered"; 
       break; 
     } 
     Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 
    } 
}; 

ctx.getApplicationContext() 
    .registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT")); 
ctx.getApplicationContext() 
    .registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED")); 

結論:因此,要註冊的BroadcastReceiver類中的接收器。

而不是registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));

使用context.getApplicationContext().registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));

相關問題