gpt4 book ai didi

Android:我需要延迟通知

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:43 26 4
gpt4 key购买 nike

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

我还在应用程序中使用相同的接收器,以便在使用回调接收到短信时更新 Activity 。

问题是短信应用程序通知事件是在我收到通知后发送的,所以当我更新短信时,content://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);
}

最佳答案

如果您只需要在特定时间内执行此操作,最简单的方法可能是使用处理程序并使用 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);

如果你想等待另一个 Action ,那就有点复杂了。

关于Android:我需要延迟通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19096475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com