2013-08-03 19 views
0

我有「ComposeActivity」,它在調用SMS類中的metod之後調用onClick之後的「SendSMS」方法。我還註冊了兩個BroadcastReceiver:SmsDeliveredReceiver和SmsSentReceiver,類似於:https://stackoverflow.com/a/17164931/1888738。我怎麼能通知ComposeActivity,短信是成功發送的,並且該活動可以清理一些EditText的,並且可能顯示包含sms發送或不發送信息的信息(以及爲什麼)?我的代碼:http://pastebin.com/LNRuSeBu從BroadcastReceiver到外部類的特定活動的信息

回答

1

好了,5個小時後嘗試,我已經解決了這個:

在廣播接收器中的onReceive:

Intent intent = new Intent(); 
intent.setAction("SOMEACTION"); 
context.sendBroadcast(intent); 

的活動:

public BroadcastReceiver receiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (action.equals("SOMEACTION")) { 
      Log.d(TAG, "Sent"); 
     } 
    } 
}; 

和活動的onCreate我Registered BroadcastReceiver:

registerReceiver(receiver, new IntentFilter("SOMEACTION")); 

這就是所有...

+0

所以這基本上是我描述你做的......無論如何很高興你解決了你的問題/ – dudebrobro

1

如果您有接收器來處理SMS消息發送或未發送。您可以通過創建一個意圖來修改兩個接收器的onReceive以發送並意向ComposeActivity,並調用intent.setComponent來指定意圖應該去的位置。有些數據告訴ComposeActivity嘗試發送消息的結果。

更新:

public void onReceive(Context context, Intent arg1) { 
    Intent i = new Intent(action); 
    i.setComponent(new ComponentName("com.mypackage.compose","ComposeActivity")); 
    switch (getResultCode()) { 
     case Activity.RESULT_OK: 
      Log.d(getClass().getSimpleName(), "SMS delivered"); 
      intent.setAction("com.mypackage.compose.SMS_SENT"); // String you define to match the intent-filter of ComposeActivity. 
      break; 
     case Activity.RESULT_CANCELED: 
      Log.d(getClass().getSimpleName(), "SMS not delivered"); 
      intent.setAction("com.mypackage.compose.SMS_FAILED"); // String you define to match the intent-filter of ComposeActivity. 

      break; 
    } 
    startActivity(intent); // you may not necessarily have to call startActivity but call whatever method you need to to deliver the intent. 

} 

在這一點上,應該僅僅是物質或者通過清單或編程的addind意圖過濾器和接收器,以你的撰寫活動。你的來電。我使用的字符串已組成,但您可以選擇現有的意圖操作字符串或聲明您在意向過濾器中使用的字符串。再次由你決定。也可能有助於查看有關發送明確意圖的問題,例如Android explicit intent with target component 或查看android docs

+0

你能幫我一些例子嗎? – Bresiu

+0

我試圖用幾個小時以不同的方式做到這一點,它仍然沒有工作。在您的指導下,我已經談到了這一點,我需要的是「隱式意圖」,因爲有時信息將被髮送到ComposeActivity,有時會發送到SmsListActivity。回到問題:我不能使用你的方法「BroadcastReceiver」或代碼: 'code' 意圖intent = new Intent(); intent.setAction(「ACTION」); sendBroadcast(intent); 「 」無法解析sendBroadcast方法...「 與您的方法相同。解決「ComponentName」有問題。 – Bresiu