我试图创建一个从服务器下载(定期)数据的应用程序,并在下载时通知用户。
我尝试使用Toast消息,但我需要在状态栏中显示通知。 我使用了developer.android.com中的一些示例代码,单击按钮后会发出通知。但是我需要在不点击按钮的情况下发送通知。
最精确的问题是
如何在" .execute"之后调用sendNotification。 in" run"函数,我应该在sendNotification中使用哪些参数(?这里有什么?)?
此h.postDelayed
内容采用OnCreate
方法。
以下是代码:
h.postDelayed(new Runnable() {
public void run() {
new WebServiceHandler()
.execute("http://maciekb94.cba.pl/");
Toast.makeText(MainActivity.this, "Pobrano dane", Toast.LENGTH_SHORT).show();
//sendNotification();
h.postDelayed(this, delay);
}
},delay);
}
public void sendNotification(View view) {
Intent intent = new Intent(this,MainActivity.class);
//Uri.parse("http://developer.android.com/reference/android/app/Notification.html"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// END_INCLUDE(build_action)
// BEGIN_INCLUDE (build_notification)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_stat_new_message);
// Set the intent that will fire when the user taps the notification.
builder.setContentIntent(pendingIntent);
// Set the notification to auto-cancel. This means that the notification will disappear
// after the user taps it, rather than remaining until it's explicitly dismissed.
builder.setAutoCancel(true);
builder.setContentTitle("Pobrano dane");
builder.setContentText("Wejdź by je zobaczyć");
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
//END_INCLUDE(send_notification)
}
答案 0 :(得分:0)
地点:
notificationManager.notify(NOTIFICATION_ID);
在运行功能中。通知更新或创建通知。 请参阅:http://developer.android.com/reference/android/app/NotificationManager.html
答案 1 :(得分:0)
在WebServiceHandler()任务的onPostExecute()中发送通知。
public class WebServiceHandler extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
..........
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}