Android:我需要延迟通知

时间:2013-09-30 14:13:57

标签: android android-notifications android-broadcast

我创建了一个广播接收器,可以收听android.provider.Telephony.SMS_RECEIVED事件并创建自己的通知。

我还在应用程序中使用相同的接收器来更新使用回调接收短信时的活动。

问题是我的通知后会调度短信应用通知事件,所以当我更新短信时没有预设内容://短信

如果有可能,我想延迟通知,无法找到怎么做。

这是代码:

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 个答案:

答案 0 :(得分: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); 

如果你想等待另一个动作,那就更复杂了。