延迟通知生成 (Android)
Delay a notification build (Android)
我想延迟构建通知,例如1分钟,我该怎么做?我尝试插入一个处理程序并对其进行 postDelaying,但出现多个错误。
public void customizedOnClick (View view){
String ticker = textTicker.getText().toString();
String title = textTitle.getText().toString();
String body = textBody.getText().toString();
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setTicker(ticker);
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle(title);
notification.setContentText(body);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
//Builds notification and issues it
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
确保导入正确的处理程序:
import android.os.Handler;
创建处理程序和可运行对象。 runnable 将是您的代码,将在延迟后执行。
private Handler handler = new Handler();
private NotificationCompat.Builder notification;
private static final int uniqueID = 12345;
private EditText textTicker;
private EditText textTitle;
private EditText textBody;
public void customizedOnClick (View view) {
if(TextUtils.isEmpty(textTicker.getText()
|| TextUtils.isEmpty(textTitle.getText())
|| TextUtils.isEmpty(textBody.getText()){
Log.e("NotificationClick", "A TextView was empty or null");
return;
}
String ticker = textTicker.getText().toString();
String title = textTitle.getText().toString();
String body = textBody.getText().toString();
notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher);
.setTicker(ticker);
.setWhen(System.currentTimeMillis());
.setContentTitle(title);
.setContentText(body);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
handler.postDelayed(runnable, 20 * 1000); //twenty seconds delay
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
NotificationManager nm =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
};
请注意,处理程序的使用并不总是精确的,因为 "Time spent in deep sleep will add an additional delay to execution"。所以如果你想要一个精确的时间量(比如游戏能量棒)你应该选择 Handler 以外的东西。
我想延迟构建通知,例如1分钟,我该怎么做?我尝试插入一个处理程序并对其进行 postDelaying,但出现多个错误。
public void customizedOnClick (View view){
String ticker = textTicker.getText().toString();
String title = textTitle.getText().toString();
String body = textBody.getText().toString();
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setTicker(ticker);
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle(title);
notification.setContentText(body);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
//Builds notification and issues it
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
确保导入正确的处理程序:
import android.os.Handler;
创建处理程序和可运行对象。 runnable 将是您的代码,将在延迟后执行。
private Handler handler = new Handler();
private NotificationCompat.Builder notification;
private static final int uniqueID = 12345;
private EditText textTicker;
private EditText textTitle;
private EditText textBody;
public void customizedOnClick (View view) {
if(TextUtils.isEmpty(textTicker.getText()
|| TextUtils.isEmpty(textTitle.getText())
|| TextUtils.isEmpty(textBody.getText()){
Log.e("NotificationClick", "A TextView was empty or null");
return;
}
String ticker = textTicker.getText().toString();
String title = textTitle.getText().toString();
String body = textBody.getText().toString();
notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher);
.setTicker(ticker);
.setWhen(System.currentTimeMillis());
.setContentTitle(title);
.setContentText(body);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
handler.postDelayed(runnable, 20 * 1000); //twenty seconds delay
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
NotificationManager nm =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
};
请注意,处理程序的使用并不总是精确的,因为 "Time spent in deep sleep will add an additional delay to execution"。所以如果你想要一个精确的时间量(比如游戏能量棒)你应该选择 Handler 以外的东西。