2

我有一個應用程序,通過點擊通知中的操作按鈕發送短信。我想在短信發送後顯示Toast,但這不起作用。我擔心它與AsyncTask和/或BroadcastReceiver有關。如何在通過Android執行點擊操作後發送通知Toast?

發送短信的工作流程是這樣的:

  • 我掃描的AsyncTask我的設備接觸和創建從AsyncTask的的NotificationonPostExecute()
  • 我用NotificationCompat.Builder用於創建Notification
  • 我向Notification添加一個PendingIntent,看起來像這樣:

    PendingIntent.getBroadcast(mContext, (int) _person.getId(), i, PendingIntent.FLAG_ONE_SHOT);

  • 我送從BroadcastReceiver的短信onReceive()

  • 我試圖從那裏發送吐司這樣的:

    Toast.makeText(_context, "SMS sent!", Toast.LENGTH_SHORT).show();

我試着調試這個,但不幸的是Eclipse不會在onReceive()中顯示變量的內容。

我還讀了一些關於「處理兼容性」的通知Google的開發人員網站here,但我無法找到一個教程,其中詳細解釋了這一點。

編輯:

我想這可能是有益的解釋是通過所有的課程通過什麼語境:

  • 在我MainActivity我顯示PreferenceFragment爲主要內容的
  • 在那PreferenceFragment我叫新MyAsyncTask(getActivity()).execute();,這樣我的MainActivity應該是我的Context
  • MyAsyncTask構造我通過這個來,我創建Notification類,有保存爲一個成員(mContext
  • 我已經提到,其餘...

編輯2:

下面是有關這塊我BroadcastReceiver的代碼,監聽Notification的行動:

@Override 
public void onReceive(Context _context, Intent _intent) 
{ 
    String type = _intent.getStringExtra("type"); 
    if (type.equals("SMS")) 
    { 
     String phoneNumber = _intent.getStringExtra("phoneNumber"); 
     String message = _context.getResources().getString(
      R.string.smstext); 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage(phoneNumber, null, message, null, null); 

     Toast.makeText(_context, "SMS sent!", Toast.LENGTH_SHORT).show();   
    } 
} 
+0

是否有錯誤信息?如果是這樣,請發佈。什麼是_context和它在哪裏設置? – Lenny

+0

請詳細說明「這不起作用」 – Tyler

+0

好吧,短信發送,但吐司不會顯示。 _context只是從onReceive傳遞的上下文(Context _context,Intent _intent)。 (我爲傳遞的變量使用下劃線)我沒有收到錯誤消息。 – kaolick

回答

0

使用可運行的。每當我在一個線程中執行命令,我成立了一個處理程序,並且讓我跑的東西在主線程,當一切都完成後可運行:

// Declare a global handler for the class 
final Handler mHandler = new Handler(); 
// Declare a runnable that will do things app-side when your thread is finished 
final Runnable mMessageSent= new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     Toast.makeText(_context, "SMS sent!", Toast.LENGTH_SHORT).show(); 
    } 
}; 

// Set up your thread to post the runnable when it's finished sending an SMS 
private void sendMessage() 
{ 
    Thread t = new Thread() 
    { 
     public void run() 
     { 
      // Send your SMS here 
      // When finished, notify the handler so it knows to show a toast notification 
      mHandler.post(mMessageSent); 
     } 
    }; 
    t.start(); 
} 
+0

不幸的是,這是行不通的。我之前嘗試過的基本一樣,也沒有工作。不過謝謝! – kaolick

0

MasterKale接近,問題是你需要從送麪包UI線程或它不會工作。我不確定他認爲他是如何使用該線程做的。

final Handler mHandler = new Handler(Looper.getMainLooper()); 

mHandler.post(new Runnable(){ 
     @Override 
     public void run() { 
      Toast.makeText(mContext, "Message", Toast.LENGTH_SHORT).show(); 
     } 

}); 
+0

不幸的是,這也行不通。我編輯了我的帖子並添加了我的'BroadcastReceiver'的'onReceive()'。我用你的代碼用'Toast'替換了行(必須將'mContext'改爲'_context',並且必須使它成爲'final')。但它仍然沒有工作。這讓我瘋狂。不知怎的,'Context'不能錯,因爲我可以訪問應用程序的'Resources'。 – kaolick

+0

嘗試將您的邏輯移至服務,讓您的接收器啓動服務以發送短信並顯示敬酒。 – schwiz

+0

你的意思是代替使用'AsyncTask'來掃描聯繫人?或者只是在接收器的'onReceive()'中發生的部分? – kaolick