2017-07-25 91 views
1

我正在创建一个应用程序,该应用程序在wordpress网站发布新帖子后立即显示通知。从android服务创建实时通知

现在,我在wordpress网站上发布帖子后立即生成通知。

但40-45分钟后应用进行了坠毁,并与下面的错误后台就会被杀死 -

the error that showed after the app crashes

我已经尝试了许多解决方案都没有它的工作。
我不想使用firebase进行通知。

我必须从wordpress实时获取数据,然后创建通知。

这是用于产生通知以下代码: -

私人无效setupNotificationListener(){

StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String s) { 
      flag=1; 
      //System.out.println(flag); 
      gson = new Gson(); 
      list = (List) gson.fromJson(s, List.class); 
      postTitle = new String[list.size()]; 
      postContent=new String[list.size()]; 

      System.out.println("shiv class:"+list.size()); 
      for(int i=0;i<list.size();++i) { 
       mapPost = (Map<String, Object>) list.get(i); 
       mapTitle = (Map<String, Object>) mapPost.get("title"); 
       postTitle[i] = (String) mapTitle.get("rendered"); 
       mapContent = (Map<String, Object>) mapPost.get("content"); 
       postContent[i] = (String) mapContent.get("rendered"); 
      } 
      if(!alreadyNotified(postTitle[0])){ 
       Log.d("not notified",""); 
       createNotif(postContent[0],postTitle[0]); 
       saveNotificationKey(postTitle[0]); 
      }else{ 
       System.out.print("already notified"); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError volleyError) { 
      Log.d("Error","Error"); 
     } 
    }); 

    rQueue = Volley.newRequestQueue(NotifierService.this); 
    rQueue.add(request); 
    NotifierService.getInstance().getRequestQueue().getCache().clear(); 
} 
+1

好像你在后台创建多个线程来获取新的岗位这么多对象的OutOfMemoryError可能发生!由于内存不足,会发生此错误!你在什么时间间隔检查新帖子? –

+0

这是我正在写的代码 – aditya1508

+0

发布您的当前代码! –

回答

1

您从计时器创建请求队列,所以每5秒。在您执行的代码中,您始终创建一个新的请求队列。这不起作用,你应该保持requestQueue作为最终的全局变量,并在你的构造函数中只创建一次。

public class NotifierService extends Service { 
    private final RequestQueue rQueue; 

    NotifierService() { 
     this.rQueue = Volley.newRequestQueue(this); 
    } 

    private void setupNotificationListener() { 
     // do your request handling 
    } 
} 

,因为你是制造一种不正确的垃圾回收

+0

中使用了报警管理器,所以在这里我只需要将请求添加到队列中! rQueue = Volley.newRequestQueue(NotifierService.this);这行只能写一次。对? – aditya1508

+0

它给出一个空指针异常无法实例化服务.NotifierService:java.lang.NullPointerException:试图调用虚拟方法'java.io.File android.content.Context.getCacheDir()'对一个空对象引用 – aditya1508