从片段开始服务

时间:2013-08-21 12:00:01

标签: android android-fragments android-asynctask android-service

我从片段启动服务时遇到问题。服务已启动,但没有预见到,我不知道为什么isCancelled变量设置为true(所以doInBackground任务未经处理)。

当我从FragmentActivity启动时,它可以正常运行:startService(new Intent(this, Recorder.class));。但是,当我尝试从片段getActivity().startService(new Intent(getActivity(), Recorder.class));启动它时,它无法正常工作(如上所述)。

Recorder.class:

public class Recorder extends Service {

boolean isCancelled = false;


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


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

    Log.v("service","started");
    Intent intent1 = new Intent(this, MainActivity.class);
    intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent1, 0);

    Notification noti = new NotificationCompat.Builder(this)
            .setContentTitle("Recorder")
            .setContentText("running")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pendIntent)
            .setPriority(Notification.PRIORITY_MIN)
            .build();

    startForeground(12345, noti);


    new RecordTask().execute("http://path");

    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();

   isCancelled = true;
}

@Override
public void onCreate() {
    super.onCreate();
}


private class RecordTask extends AsyncTask<String, Void, Boolean> {
    protected Boolean doInBackground(String... StringUrls) {

     // do something
                Log.v("isCancelled", String.valueOf(isCancelled));  
                   // <-- why isCancelled = true?
                if (isCancelled) break;  

            }


        } catch (IOException e) {
            System.err.println("Caught IOException: " + e.getMessage());
        }

       return true;
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(Boolean result) {

    }
  }

}

AddNewRecordFragment:

public class AddNewRecordFragment extends Fragment implements View.OnClickListener {

//   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setRetainInstance(true);
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.addnewrecordfragment, container, false);

    Button b = (Button) v.findViewById(R.id.button);
    Button b2 = (Button) v.findViewById(R.id.button2);
    b.setOnClickListener(this);
    b2.setOnClickListener(this);
    return v;


}


@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button:
            Log.v("start ", "clicked");
            getActivity().startService(new Intent(getActivity(), Recorder.class));

        case R.id.button2:

            getActivity().stopService(new Intent(getActivity(), Recorder.class));

            break;
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.v("fragment", "onAttach");
}

@Override
public void onDetach() {
    super.onDetach();

    Log.v("fragment", "onDetach");
}

@Override
public void onDestroy() {
    super.onDestroy();

    Log.v("fragment", "onDestroy");
  }
}

1 个答案:

答案 0 :(得分:1)

您的问题与从service启动fragment无关。如果您在onDestroy中设置isCancelled为true的唯一时间,那么该服务正在启动,因为您打算在AsyncTask完成之前将其杀死。看看this post。在某些情况下可以杀死服务。问题是为什么要调用onDestroy。如果您在其自己的流程上运行此serviceservice可能会被杀死,因为您所做的只是设置AsyncTask 1}}。如果这是在与托管活动相同的过程上运行,那么为什么onDestroy被调用并不完全清楚(通常是因为系统需要内存,但我无法判断是否有这样的情况)在这种情况下是真的)。