【问题标题】:Delay a notification build (Android)延迟通知构建 (Android)
【发布时间】:2015-04-17 14:14:12
【问题描述】:

我想将通知的构建延迟到例如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());


    }

【问题讨论】:

  • 你遇到了什么错误?
  • 这是我尝试使用处理程序的时候,但我从原始代码中删除了它。错误:(59, 33) 错误:处理程序是抽象的;无法实例化错误:(60, 16) 错误: 找不到符号方法 postDelayed(,int)
  • 您确定您导入了正确的Handlerjava.util.logging 包中有一个抽象处理程序。你想要android.os中的那个
  • 你可能有错误的导入
  • 这是错误的导入,但现在我不知道将 onClick 放在哪里。请看下面的答案。 @Knossos

标签: android build handler delay android-notifications


【解决方案1】:

一定要导入正确的Handler:

导入android.os.Handler;

创建处理程序和可运行文件。可运行的将是延迟后执行的代码。

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

【讨论】:

  • 这是错误的处理程序类,但我现在不明白如何设置onClick?
  • yourview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 做点什么 } });
  • 非常感谢,它的格式错误,所以我发布了一个新答案,还是应该编辑你的?
  • 它们几乎相同。但我编辑了它。很高兴你想通了
  • 我也做了一些改动。空检查,这对您添加有好处。
【解决方案2】:

请注意,处理程序的使用并不总是精确的,因为“在深度睡眠中花费的时间会增加执行的额外延迟”。因此,如果您想要精确的时间量(例如游戏能量条),您应该选择除 Handler 之外的其他东西。

【讨论】: