Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么即使Android中的应用程序仍在运行(如应用程序使用情况所示),在关闭应用程序后通知仍会停止?_Java_Android_Service_Broadcastreceiver - Fatal编程技术网

Java 为什么即使Android中的应用程序仍在运行(如应用程序使用情况所示),在关闭应用程序后通知仍会停止?

Java 为什么即使Android中的应用程序仍在运行(如应用程序使用情况所示),在关闭应用程序后通知仍会停止?,java,android,service,broadcastreceiver,Java,Android,Service,Broadcastreceiver,我的服务运行良好,即使应用程序被关闭。我可以证明它仍然有效(在应用程序使用中) 问题是,只有当应用程序尚未被终止时,才会出现通知。在我杀了它之后,我再也不会收到通知了 这是我的密码: public class NotifyService extends Service { public static boolean ServiceIsRun=false; int i = 1;int last; @Override public int onStartCommand(Intent

我的服务运行良好,即使应用程序被关闭。我可以证明它仍然有效(在应用程序使用中)

问题是,只有当应用程序尚未被终止时,才会出现通知。在我杀了它之后,我再也不会收到通知了

这是我的密码:

public class NotifyService extends Service {
  public static boolean ServiceIsRun=false;
  int i = 1;int last;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    NotifyHelper notifyHelper = new NotifyHelper(getApplicationContext());
    last = notifyHelper.checkBabiaNews();
    Timer t = new Timer();
    if(ServiceIsRun){
      t.scheduleAtFixedRate(new TimerTask() {
          @Override
          public void run() {
            new notifyAsync().execute(last);
            Log.i("broadService", "hello from Service"+i);
            i++;
          }

        },

        //Set how long before to start calling the TimerTask (in milliseconds)
        0,
        //Set the amount of time between each execution (in milliseconds)
        10000);
      }

    return START_STICKY;
  }

  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  public class notifyAsync extends AsyncTask<Integer,String,Void>{
    String msg ="";
    int count=0;

    @Override
    protected Void doInBackground(Integer... integers) {
      int last=integers[0];
      String myurl= getString(R.string.link)+"android.php?do=checklatest&last_babia_new="+last;
      String result="";

      InputStream isr = null;
      try {
        URL url = new URL(myurl);
        URLConnection urlConnection = url.openConnection();
        isr = new BufferedInputStream(urlConnection.getInputStream());
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
        //convert response to string
      try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
          sb.append(line + "\n");
        }
        isr.close();
        result = sb.toString();
      } catch (Exception e) {
        Log.e("log_tag", "Error  converting result " + e.toString());
      }
      try {
        JSONObject jObject = new JSONObject(result);
        JSONArray jArray=jObject.getJSONArray("latest_babia_new");
        for (int i = 0; i < jArray.length(); i++) {
          JSONObject json = jArray.getJSONObject(i);
          msg+=json.getString("title")+" - "+json.getString("date")+"\n";
          count ++;
        }
      } catch (JSONException e) {
        e.printStackTrace();
      }
      // creat intent To Broadcast
      if(count>0) {
        Intent ii = new Intent();
        ii.setAction("com.latestBabiaNews");
        ii.putExtra("msg", msg);
        ii.putExtra("count",count);
        sendBroadcast(ii);
      } else {
        Log.i("sqlerreor",count+"");
      }
      try{
        Thread.sleep(10000);
      } catch (Exception ex){

      }
      return null;
    }
  }
这是
接收器中的问题吗?或者这是
服务中的一个问题

public class NotifyBroadcast extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    final  Bundle bundle = intent.getExtras();
    if (intent.getAction().equalsIgnoreCase("com.latestBabiaNews")){
      Log.i("broadReceiver","hello from Broadcast");
      MyNotification notification = new MyNotification();
      int count = bundle.getInt("count");
      String message = "bla bla bla bal hey";
      notification.notify(context,message,count,bundle.getString("msg"));
      //Toast.makeText(context,bundle.getString("msg")+"/ called ",Toast.LENGTH_LONG).show();
    }
  }
}