我正在开发一个需要从后台调用Web服务的应用程序。我已经编写了一个代码,但问题是如果Activity被系统销毁(在后台长时间销毁),那么调用就停止了。因为处理程序无法获取Activity实例。我看到许多代码使用各种代码,但没有任何帮助。所以,如果有人帮助我,那么我会感激他。
MyService class:
public class MyService extends Service {
Callbacks activity;
private long startTime = 0;
private long millis = 0;
private final IBinder mBinder = new LocalBinder();
Handler handler = new Handler();
Runnable serviceRunnable = new Runnable() {
@Override
public void run() {
millis = System.currentTimeMillis() - startTime;
activity.updateClient(millis); //Update Activity (client) by the implementd callback
handler.postDelayed(this, 5000);
Log.e("I am here", "++++++++++++++++++++++++++");
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Do what you need in onStartCommand when service has been started
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
//returns the instance of the service
public class LocalBinder extends Binder {
public MyService getServiceInstance() {
return MyService.this;
}
}
//Here Activity register to the service as Callbacks client
public void registerClient(Activity activity) {
this.activity = (Callbacks) activity;
}
public void startCounter() {
startTime = System.currentTimeMillis();
handler.postDelayed(serviceRunnable, 0);
Toast.makeText(getApplicationContext(), "Counter started", Toast.LENGTH_SHORT).show();
}
public void stopCounter() {
handler.removeCallbacks(serviceRunnable);
}
//callbacks interface for communication with service clients!
public interface Callbacks {
void updateClient(long data);
}
}
MainActivity类:
public class MainActivity extends AppCompatActivity implements MyService.Callbacks {
ToggleButton toggleButton;
ToggleButton tbStartTask;
TextView tvServiceState;
TextView tvServiceOutput;
Intent serviceIntent;
MyService myService;
int seconds;
int minutes;
int hours;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent = new Intent(MainActivity.this, MyService.class);
setViewsWidgets();
}
private void setViewsWidgets() {
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(btListener);
tbStartTask = (ToggleButton) findViewById(R.id.tbStartServiceTask);
tbStartTask.setOnClickListener(btListener);
tvServiceState = (TextView) findViewById(R.id.tvServiceState);
tvServiceOutput = (TextView) findViewById(R.id.tvServiceOutput);
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
Toast.makeText(MainActivity.this, "onServiceConnected called", Toast.LENGTH_SHORT).show();
// We've binded to LocalService, cast the IBinder and get LocalService instance
MyService.LocalBinder binder = (MyService.LocalBinder) service;
myService = binder.getServiceInstance(); //Get instance of your service!
myService.registerClient(MainActivity.this); //Activity register in the service as client for callabcks!
tvServiceState.setText("Connected to service...");
tbStartTask.setEnabled(true);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
Toast.makeText(MainActivity.this, "onServiceDisconnected called", Toast.LENGTH_SHORT).show();
tvServiceState.setText("Service disconnected");
tbStartTask.setEnabled(false);
}
};
View.OnClickListener btListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == toggleButton) {
if (toggleButton.isChecked()) {
startService(serviceIntent); //Starting the service
bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE); //Binding to the service!
Toast.makeText(MainActivity.this, "Button checked", Toast.LENGTH_SHORT).show();
} else {
unbindService(mConnection);
stopService(serviceIntent);
Toast.makeText(MainActivity.this, "Button unchecked", Toast.LENGTH_SHORT).show();
tvServiceState.setText("Service disconnected");
tbStartTask.setEnabled(false);
}
}
if (v == tbStartTask) {
if (tbStartTask.isChecked()) {
myService.startCounter();
} else {
myService.stopCounter();
}
}
}
};
@Override
public void updateClient(long millis) {
seconds = (int) (millis / 1000) % 60;
minutes = (int) ((millis / (1000 * 60)) % 60);
hours = (int) ((millis / (1000 * 60 * 60)) % 24);
tvServiceOutput.setText((hours > 0 ? String.format("%d:", hours) : "") + ((this.minutes < 10 && this.hours > 0) ? "0" + String.format("%d:", minutes) : String.format("%d:", minutes)) + (this.seconds < 10 ? "0" + this.seconds : this.seconds));
Toast.makeText(MainActivity.this, "I am here", Toast.LENGTH_SHORT).show();
}
}
从后台销毁应用后,此toast Toast.makeText(MainActivity.this, "I am here", Toast.LENGTH_SHORT).show();
未显示。那么它也不会调用Web服务。实际上它不会调用此函数updateClient()
。我也试过了Log.e()
。