0

我从启动服务的一个fragment.The服务已启动,但它没有foreground,我不知道为什么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"); 
    } 
} 
+0

你在哪里开始它在你的片段类?在使用GetActivity()之前,您必须确保活动已创建并且片段正确连接。否则它将无法工作! –

回答

1

您的问题不涉及从fragment开始service。如果您设置的唯一时间isCancelled为true,则为onDestroy,则服务开始,因为您意图在您的AsyncTask完成之前它也刚刚被杀死。看看this post。服务可能在某些情况下被杀害。问题是为什么onDestroy被调用。如果您在自己的进程上运行此service,则可能会因为您正在设置AsyncTask而导致service死机。如果这是在托管活动的同一个进程上运行,那么为什么onDestroy被调用并不完全清楚(通常是因为系统需要内存,但在这种情况下我不能说这是真的)。

+0

该问题与从片段启动服务有关,因为当我从FragmentActivity(主机活动)启动它时,它起作用。 – Kristopher

+0

但是你的服务开始了,它只是被杀死了。发生这种情况时,你的碎片仍然活跃吗?我建议在某些生命周期方法中放置Log标签,以查看何时onDestroy与片段和活动生命周期关系被调用。我不知道为什么onDestroy从上面发布的内容中调用。 – Rarw

+0

我从我开始服务的地方添加了'AddNewRecordFragment'。 – Kristopher