Android:我需要延迟通知 [英] Android: I need to delay a Notification

查看:87
本文介绍了Android:我需要延迟通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've create a broadcast receiver that listen to the android.provider.Telephony.SMS_RECEIVED event and creates its own notification.

当通过回调收到短信时,我还使用应用程序中的同一接收器来更新活动.

I also use the same receiver in the app to update the activities when an sms is received using a callback.

问题在于,在我的通知后调度了短信应用通知事件,因此,当我更新短信时,content://sms中未预设短信

如果可能的话,我想延迟通知,找不到解决方法.

I would like to delay my notification if it's possible, can't find how to do it.

这是代码:

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);
}

}

推荐答案

如果只需要在特定时间内执行此操作,则最简单的方法可能是使用处理程序并使用postDelayed().像这样:

If you just need to do it for a specific amount of time, probably the easiest way is to use a handler and use a postDelayed(). Something like this:

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

如果您要等待其他操作,则要复杂一些.

If you want to wait for another action, that's a bit more complicated.

这篇关于Android:我需要延迟通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆