Android 在4.2以上版本的后台运行服务

Android 在4.2以上版本的后台运行服务,android,Android,我正在开发一个应用程序,它在下面的Jellybean4.1版本中正确运行 我在后台运行android 4.2以上版本的服务时遇到问题。请帮助解决此问题 public int onStartCommand(Intent intent, int flags, int startId) { File root = android.os.Environment.getExternalStorageDirectory(); File dir = new File(root

我正在开发一个应用程序,它在下面的Jellybean4.1版本中正确运行

我在后台运行android 4.2以上版本的服务时遇到问题。请帮助解决此问题

    public int onStartCommand(Intent intent, int flags, int startId) {

      File root = android.os.Environment.getExternalStorageDirectory();
      File dir = new File(root.getAbsolutePath() + "/AutoResponseLog");
     File log = new File(dir, "AlarmLog.txt");

      BufferedWriter out;
    try {
        out = new BufferedWriter(
                                new FileWriter(log.getAbsolutePath(),
                                                log.exists()));
        out.write(new Date().toString());
          out.write("\n");
          out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     Context c = getApplicationContext();
     SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(c);

     String er = app_preferences.getString("set", "done");
    if(er.equals("done"))
    {
        //Toast.makeText(this, "registering the recever", Toast.LENGTH_SHORT).show();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(receiver, filter);
        Uri mediaUri = android.provider.CallLog.Calls.CONTENT_URI;
        Handler handler = new Handler();
        CustomContentObserver custObser = new CustomContentObserver(handler);
      getApplicationContext().getContentResolver().registerContentObserver(mediaUri, false, custObser);

    }
      return START_STICKY;
  }

您是否有任何错误?请显示logcat错误。请完整准确地解释您看到的“在后台运行服务时出现的问题”。我不清楚到底发生了什么,它在4.1版本中运行正确,但在OnReceive函数中设置问题时没有运行。