我有一个带有Service和AsyncTask类的简单应用程序。当我启动服务时,扩展AsyncTask的BackgroundTask类将运行一些模拟任务并显示一些进度。 在同一BackgroundTask类中,我无法理解为什么服务在类构造函数中起作用,为什么在MyService类的onStartCommand方法中,BackgroundTask构造函数将其作为参数接受。
MainActivity类:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnStartService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartService = findViewById(R.id.btnStartService);
btnStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MyService.class);
startService(intent);
}
});
}
}
MyService类:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
private static final int TASK_COUNT = 3;
private boolean serviceOn = false;
public MyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!serviceOn){
Toast.makeText(this, "Service starting...", Toast.LENGTH_SHORT).show();
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(TASK_COUNT);
serviceOn = true;
}else{
Toast.makeText(this, "Service has already started...", Toast.LENGTH_SHORT).show();
}
return START_STICKY;
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service stoped", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
BackgroundTask类:
import android.app.Service;
import android.os.AsyncTask;
import android.widget.Toast;
public class BackgroundTask extends AsyncTask<Integer, Integer, String> {
private Service service;
public BackgroundTask(Service service) {
this.service = service;
}
@Override
protected String doInBackground(Integer... integers) {
int taskCount = integers[0];
for (int i = 0; i < taskCount;i++){
performLongTask();
publishProgress((int) ((i+1) / (float) taskCount * 100));
}
String result = taskCount + " "+service.getString(R.string.finished);
return result;
}
private void performLongTask() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
protected void onProgressUpdate(Integer... values) {
String text = values[0] + "% " + service.getResources().getString(R.string.finished);
Toast.makeText(service, text, Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute(String s) {
Toast.makeText(service, s+" mark", Toast.LENGTH_SHORT).show();
service.stopSelf();
}
}
问题:
答案 0 :(得分:0)
在您使用的情况下,我认为您不需要“ MyService”作为开始。只需使用AsyncTask即可。特别是“ MyService”仍然在UI线程上运行,因此我看不到将AsyncTask与服务包装在一起的问题。
另外不要将Context / Service / Activity传递给AsyncTask,因为这会形成显式引用,并可能导致内存泄漏。