2013-09-30 50 views
2

我已經創建了一個廣播接收器,用於監聽android.provider.Telephony.SMS_RECEIVED事件並創建自己的通知。Android:我需要延遲通知

我也在應用程序中使用相同的接收器來更新使用回調接收到短信時的活動。

的問題是,短消息應用程序通知事件是我通知後出動,所以當我更新的SMS沒有在內容預設://短信

我想推遲我的通知,如果有可能,無法找到如何去做。

下面的代碼:

public class SmsReceiver extends BroadcastReceiver { 

Context context; 

int nmessages = 0; 


@Override 
public void onReceive(Context context, Intent intent) { 
//—get the SMS message passed in— 
Bundle bundle = intent.getExtras(); 
SmsMessage[] msgs = null; 
String messages = ""; 

this.context = context; 

if (bundle != null) 
{ 
    //—retrieve the SMS message received— 
    Object[] smsExtra = (Object[]) bundle.get("pdus"); 

    msgs = new SmsMessage[smsExtra.length]; 

    nmessages = SmsHolder.getNumberUnreadSms(context) +1; 


    for (int i=0; i<msgs.length; i++) 
    { 
     SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]); 
     //take out content from sms 
     String body = sms.getMessageBody().toString(); 
     String address = sms.getOriginatingAddress(); 

     messages += "SMS from " + address + " :\n"; 
     messages += body + "\n"; 

     putSmsToDatabase(sms, context); 
    } 
    //—display the new SMS message— 

    createNotification(SmsMessage.createFromPdu((byte[])smsExtra[0]), context); 

    updateActivity(); 
} 
} 

public void updateActivity(){ 

} 

private void putSmsToDatabase(SmsMessage sms, Context context) 
{ 



    String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); 
    // Create SMS row 
    ContentValues values = new ContentValues(); 

    values.put("address", sms.getOriginatingAddress().toString()); 
    values.put("date", mydate); 
    values.put("body", sms.getMessageBody().toString()); 
    // values.put(READ, MESSAGE_IS_NOT_READ); 
    // values.put(STATUS, sms.getStatus()); 
    // values.put(TYPE, MESSAGE_TYPE_INBOX); 
    // values.put(SEEN, MESSAGE_IS_NOT_SEEN); 



} 

private void createNotification(SmsMessage sms, Context context){ 

    NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

    String contentTitle; 
    if (nmessages < 2){ 
     contentTitle = "SMS: " + ContactsInterface.getContactDisplayNameByNumber(sms.getOriginatingAddress(), context); 
    }else { 
     contentTitle = nmessages + " " + context.getResources().getString(R.string.new_messages); 
    } 

    // construct the Notification object. 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
     .setContentTitle(contentTitle) 
     .setContentText(sms.getMessageBody()) 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setLargeIcon(getIconBitmap()) 
     .setNumber(nmessages); 

     builder.setAutoCancel(true); 

     //(R.drawable.stat_sample, tickerText, 
      //  System.currentTimeMillis()); 

     // Set the info for the views that show in the notification panel. 
     //notif.setLatestEventInfo(this, from, message, contentIntent); 
     /* 
     // On tablets, the ticker shows the sender, the first line of the message, 
     // the photo of the person and the app icon. For our sample, we just show 
     // the same icon twice. If there is no sender, just pass an array of 1 Bitmap. 
     notif.tickerTitle = from; 
     notif.tickerSubtitle = message; 
     notif.tickerIcons = new Bitmap[2]; 
     notif.tickerIcons[0] = getIconBitmap();; 
     notif.tickerIcons[1] = getIconBitmap();; 
     */ 

    // Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(context, Login.class); 

     // Because clicking the notification opens a new ("special") activity, there's 
     // no need to create an artificial back stack. 
     PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
      context, 
      0, 
      resultIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT 
     ); 


     // Ritardo in millisecondi 



    builder.setContentIntent(resultPendingIntent); 


     // Note that we use R.layout.incoming_message_panel as the ID for 
     // the notification. It could be any integer you want, but we use 
     // the convention of using a resource id for a string related to 
     // the notification. It will always be a unique number within your 
     // application. 
     nm.notify(R.drawable.ic_drawer, builder.build()); 
} 




private Bitmap getIconBitmap() { 
    BitmapFactory f = new BitmapFactory(); 
    return f.decodeResource(context.getResources(), R.drawable.ic_sms); 
} 

}

+1

您是否需要延遲特定時間或直到應用程序中發生其他事情? – SBerg413

+0

它會更好,如果它延遲,直到默認的短信應用程序啓動它的通知,但延遲一定的時間應該無論如何工作 – codareee

回答

2

如果您需要做的僅僅是爲一個特定的時間量,可能是最簡單的方法是使用一個處理程序,並使用postDelayed()。像這樣:

// SLEEP 5 SECONDS HERE ... 
Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
    public void run() { 
      // createNotification(SmsMessage.createFromPdu((byte[])smsExtra[0]), context); 
      updateActivity(); 
    } 
}, 5000); 

如果你想等待另一個動作,這有點複雜。

+0

我注意到,我打電話: 'createNotification(SmsMessage.createFromPdu((byte [])smsExtra [0]),上下文); updateActivity();' 如果.notify()被固有延遲並在之前調用updateActivity()會怎麼樣? – codareee

+1

你是說你還需要在createNotification運行後調用updateActivity()? – SBerg413

+0

是的。現在我延遲updateActivity(),而不是它的工作原理 – codareee