服务类:
private IBinder mBinder ;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
}
@Override
public void onDestroy() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("service","started");
new Connect().execute("");
return Service.START_STICKY;
}
private class Connect extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
ConnectionConfiguration connConfig = new ConnectionConfiguration(
HOST, PORT, SERVICE);
final XMPPConnection connection = new XMPPConnection(connConfig);
Thread t= new Thread(new Runnable() {
@Override
public void run() {
try {
connection.connect();
// SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.login(USERNAME, PASSWORD);
Log.i("NetWorkConnection",
"Logged in as " + connection.getUser());
System.out.println(connection);
setConnection(connection);
} catch (XMPPException ex) {
Log.e("NetWorkConnection", "Failed to log in as "
+ USERNAME);
Log.e("NetWorkConnection", ex.toString());
setConnection(null);
}
}});
t.start();
return null;
}
}
public void setConnection(XMPPConnection connection) {
NetworkConnection.connection = connection;
}
public class MyBinder extends Binder
{
NetworkConnection getService() {
return NetworkConnection.this;
}
}
public XMPPConnection getconnection()
{
if (connection != null) {
Log.d("NetworkConnection","connection send");
return connection;
}
else
{
Log.d("NetworkConnection","connection null");
return null;
}
}
}
活动类:
private NetworkConnection service;
XMPPConnection connection=NetworkConnection.connection;
Button next;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
next=(Button)findViewById(R.id.bRetry);
if(!isMyServiceRunning())
{
Intent i=new Intent(this,NetworkConnection.class);
startService(i);
}
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println(connection);
if(connection!=null)
{
Intent i = new Intent (getApplicationContext(),UserIndex.class);
startActivity(i);
}else{
Intent i = new Intent(SignIn.this,SignIn.class);
startActivity(i);
}
}
});
}
@Override
protected void onResume() {
bindService(new Intent(this, NetworkConnection.class), mConnection,
Context.BIND_AUTO_CREATE);
super.onResume();
}
@Override
protected void onPause() {
unbindService(mConnection);
super.onPause();
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
service = ((NetworkConnection.MyBinder) binder).getService();
Log.d("Service","Connected");
System.out.println("service connection methoddd");
connection=service.getconnection();
}
public void onServiceDisconnected(ComponentName className) {
connection=null;
service = null;
}
};
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (NetworkConnection.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
我正在使用XMPP和asmack创建聊天应用程序,所以我使用了服务。但问题是,当我启动服务onServiceConnected方法永远不会执行。这就是为什么当我从服务中检索连接对象时,它总是给出null值。我对此并不太了解。请指导我。