我正在构建一个使用 Web 套接字的聊天应用程序。我写了一个服务,在 Application 类中调用。问题是,有时当我在崩溃后启动应用程序时 mConnection 变为空。我认为这是因为即使我退出应用程序,服务也在后台运行,当我再次启动它时,它无法创建新服务并绑定到它。我的问题是:这是编写代码的好方法吗?有没有办法在应用程序销毁时停止服务?
public class MyApplication extends Application{
private final AtomicInteger refCount = new AtomicInteger();
public ConnectionService mConnectionService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
ConnectionService.MyBinder b = (ConnectionService.MyBinder) binder;
mConnectionService = b.getService();
Log.d("MyApplication", "MyApplication has been bounded");
}
public void onServiceDisconnected(ComponentName className) {
mConnectionService = null;
Log.d("MyApplication", "MyApplication has been unbounded");
}
};
@Override
public void onCreate() {
Intent intent = new Intent(this, ConnectionService.class);
getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
public ConnectionService getConnectionService() {
refCount.incrementAndGet();
Log.d("MyApplication", "getConnectionService, current: "+refCount);
return mConnectionService;
}
public void releaseConnectionService() {
if (refCount.get() == 0 || refCount.decrementAndGet() == 0) {
mConnectionService.stopSelf();
Log.d("MyApplication", "MyApplication has been stopped ");
}
Log.d("MyApplication", "releaseConnectionService, current: "+refCount);
}
}
另一个类看起来像这样:
public class LobbyActivity extends Activity{
ListView lvContacts;
Gson gson;
LoginData mLoginData;
MyReceiver receiver;
ArrayList<User> users;
LobbyAdapter lobbyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gson = new Gson();
Intent intent = getIntent();
String loginDataJson = intent.getStringExtra("LoginData");
mLoginData = gson.fromJson(loginDataJson, LoginData.class);
getActionBar().setTitle(mLoginData.getUsername());
setContentView(R.layout.activity_lobby);
users = new ArrayList<User>();
lvContacts = (ListView)findViewById(R.id.lvContacts);
lobbyAdapter=new LobbyAdapter(users, this);
lvContacts.setAdapter(lobbyAdapter);
Commands cmd = new Commands(getApplicationContext());
cmd.sendCommand(Commands.COMMAND_GET_MANAGER_LIST);
cmd.sendCommand(Commands.COMMAND_GET_USER_LIST);
receiver = new MyReceiver(new Handler()) {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra(ConnectionService.MESSAGE);
String notification = intent.getStringExtra(ConnectionService.NOTIFICATION);
if (message!=null){
try {
switch (Utils.getCommand(message)) {
case Commands.COMMAND_GET_USER_LIST:
ArrayList<User> user = new ArrayList<User>();
user = (gson.fromJson(message, UserList.class)).users;
users.addAll(user);
lobbyAdapter.notifyDataSetChanged();
break;
default:
break;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(receiver,
new IntentFilter(MainActivity.BROADCAST_ACTION));
}
class UserList{
String cmd;
ArrayList<User> users;
}
@Override
protected void onDestroy() {
super.onDestroy();
((MyApplication)getApplicationContext()).releaseConnectionService();
}
}
我将服务放在 Application 类中的原因是我希望它在应用程序运行时一直运行;并且所有活动都需要访问它。