2015-03-31 103 views
0

我想知道是否可以在android中发送notification延迟。我希望收到notification消息几秒钟(即5秒)。该nofication应该以正常的方式创建:发送延迟通知

Intent intent = new Intent(this, MyActivity.class); 
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

Notification n = new Notification.Builder(this) 
     .setContentTitle("Title") 
     .setContentText("Text") 
     .setSmallIcon(R.drawable.icon) 
     .setContentIntent(pIntent) 
     .setAutoCancel(true) 
     .addAction(R.drawable.icon, "Open", pIntent).build(); 

我想避免发送notification之前调用Thread.sleep(5000)的做法,因为它会阻止应用程序。

+0

使用处理程序或定时器或报警管理器 – Apurva 2015-03-31 19:08:50

回答

1
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     // your code 
    } 
}, 5000); 

这将在5秒后在主线程上运行您的代码。如果在当前线程上运行正常,则可以删除Looper.getMainLooper()