2012-04-30 90 views
1

我正在設計一個應用程序,它是爲了捕獲手機中任何短信應用程序發送的短信的發送報告。如何生成由SMS上的任何短信應用程序發送的短信的發送報告android

我曾參與過發送並收集發送報告的消息應用程序。它是通過設置未決的意圖來完成:

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, id, intent_delivery, 0); 

然後

registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
       Bundle extras = new Bundle(); 
       extras = arg1.getExtras(); 
       switch (getResultCode()) { 
       case Activity.RESULT_OK: 
        notifyMessage("SMS delivered", getBaseContext(), extras.getInt("id")); 

        break; 
       case Activity.RESULT_CANCELED: 
        notifyMessage("SMS not delivered", getBaseContext(), extras.getInt("id")); 

        break; 
       default : 
        notifyMessage("Unable to generate delivery Report", getBaseContext(), extras.getInt("id")); 
       } 
      } 
     }, new IntentFilter(DELIVERED)); 

但現在我想知道如何能夠收集通過安裝在手機上的任何短信應用發來的短信。

目前我只是想爲此設置一個廣播列表器,但我不知道如何以及要設置什麼樣的廣播列表器和檢查wht參數。我想我必須檢查PDU的信息,但我不確定。我試圖搜索網絡,但我找不到與我需要相關的東西。

幫我出來 謝謝。

在活動類
+0

時需要同樣的問題,你拿到了,如果有,請做答覆 – Sathyapradeep

回答

1

,聲明這些全局變量:您的活動

private BroadcastReceiver sendBroadcastReceiver; 
private BroadcastReceiver deliveryBroadcastReceiver; 
String SENT = "SMS_SENT"; 
String DELIVERED = "SMS_DELIVERED"; 

覆蓋的onCreate和註冊您的接收器:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    sendBroadcastReceiver = new BroadcastReceiver() 
    { 

    public void onReceive(Context arg0, Intent arg1) 
    { 
     switch (getResultCode()) 
     { 
     case Activity.RESULT_OK: 
      Toast.makeText(getBaseContext(), "SMS 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; 
     } 
    } 
}; 

deliveryBroadcastReceiver = new BroadcastReceiver() 
{ 
    public void onReceive(Context arg0, Intent arg1) 
    { 
     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(deliveryBroadcastReceiver, new IntentFilter(DELIVERED)); 
registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT)); 
} 

接下來,您發送短信的方式只留下了6條線路代碼:

public void sendSMS(String phoneNumber, String message) 
{ 
    String SENT = "SMS_SENT"; 
    String DELIVERED = "SMS_DELIVERED"; 
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); 
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); 
    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 
} 

最後,取消註冊您的接收器停止您的活動:

@Override 
protected void onStop() 
{ 
    unregisterReceiver(sendBroadcastReceiver); 
    unregisterReceiver(deliveryBroadcastReceiver); 
    super.onStop(); 
} 
+0

謝謝老兄,它的工作對我罰款。你節省了我的時間。謝謝 – 2014-02-12 10:06:54

+0

不客氣 – Jeba