我一直在使用NotificationManager
类来处理此代码以发出通知。
但是,即使应用程序处于后台,我也希望显示通知。有什么想法吗?
@Override
protected void onPostExecute(String result) {
try
{
Thread t=new Thread()
{
public void run()
{
try
{
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("user");
for (int i = 0; i < jsonMainNode.length(); i++)
{
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("userid");
String number = jsonChildNode.optString("unreadmesage");
Thread.sleep(5000);
NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification n=new Notification(R.drawable.mainicon,"New Msg From ASKCOTTON",System.currentTimeMillis());
//n.defaults=n.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent in=new Intent(getApplicationContext(),message.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, in, 0);
// n.flags |= Notification.FLAG_ONGOING_EVENT;
n.setLatestEventInfo(getBaseContext(), "ASKCOTTON", "You Have New Message(s)", pi);
nm.notify(1,n);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
};t.start();
}catch(Exception e)
{
}
}
答案 0 :(得分:1)
您必须构建一个Service
类。
即使您的应用程序已最小化,服务也将在后台运行。 它将构建您的通知并通过NotificationManager发送它们。
请看一下这个链接:http://it-ride.blogspot.it/2010/10/android-implementing-notification.html