我对通知有疑问。我想要实现的是,当更新XML的项目或发布时给我一些解决此问题的提示时,应该触发通知。
public class ReadRss extends AsyncTask <Void, Void, Void> {
public Context context;
public String address ;
public ProgressDialog progressDialog;
public ArrayList<FeedItem>feedItems;
public RecyclerView recyclerView;
public URL url;
public ReadRss(Context context,RecyclerView recyclerView, String address) {
this.recyclerView=recyclerView;
this.context = context;
progressDialog = new ProgressDialog(context ,R.style.MyTheme);
/* progressDialog.setCancelable(false);*/
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Trasparent Color #00F0F8FF 00000000
progressDialog.setIndeterminate(true);
progressDialog.setIndeterminateDrawable(context.getResources()
.getDrawable(R.drawable.progressbar_handler));
progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
progressDialog.dismiss();
this.address = address;
}
@Override
protected void onPreExecute() {
progressDialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
super.onPostExecute(aVoid);
MyAdapter adapter=new MyAdapter(context,feedItems);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
/*recyclerView.addItemDecoration(new VerticalSpace(5));*/
recyclerView.setAdapter(adapter);
}
@Override
protected Void doInBackground(Void... params) {
ProcessXml(Getdata());
return null;
}
private void ProcessXml(Document data) {
if (data != null) {
feedItems=new ArrayList<>();
Element root = data.getDocumentElement();
Node channel = root.getChildNodes().item(1);
NodeList items = channel.getChildNodes();
for (int i = 0; i < items.getLength(); i++) {
Node cureentchild = items.item(i);
if (cureentchild.getNodeName().equalsIgnoreCase("item")) {
FeedItem item=new FeedItem();
NodeList itemchilds = cureentchild.getChildNodes();
for (int j = 0; j < itemchilds.getLength(); j++) {
Node cureent = itemchilds.item(j);
if (cureent.getNodeName().equalsIgnoreCase("title")){
item.setTitle(cureent.getTextContent());
}else if (cureent.getNodeName().equalsIgnoreCase("content:encoded")){
item.setDescription(cureent.getTextContent());
}else if (cureent.getNodeName().equalsIgnoreCase("pubDate")){
item.setPubDate(cureent.getTextContent());
}else if (cureent.getNodeName().equalsIgnoreCase("link")){
item.setLink(cureent.getTextContent());
}/*else if (cureent.getNodeName().equalsIgnoreCase("media:thumbnail")){
//this will return us thumbnail url
String url=cureent.getAttributes().item(0).getTextContent();
item.setThumbnailUrl(url);
}*/
}
feedItems.add(item);
}
}
}else {
feedItems.add(null);
}
}
public Document Getdata() {
try {
url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDoc = builder.parse(inputStream);
return xmlDoc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
我的通知代码是
Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext..."); //API level 16
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);