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