服务如何与多个活动(一个接一个)进行通信?

时间:2017-07-19 20:23:23

标签: java android

我想在两个活动上使用ServiceConnection,但是在第二个活动中,“mBoundService已被弃用”。事实上我不能再使用这个变量了。有没有人能解决我的问题? 如果你能帮助我,我会很激动! :)

服务

public class LocalService extends Service implements MessageListener{
private int myself;
private Connection c;
private Client cl;
private String ip;
private LocalService ls = this;

private final IBinder mBinder = new LocalBinder();



public class LocalBinder extends Binder {
    LocalService getService() {
        return LocalService.this;
    }
}
public void setIp(String s){
    ip = s;
}

public IBinder onBind(Intent intent) {
    return mBinder;
}


public int onStartCommand(Intent intent, int flags, int startId) {
    System.out.println("START");
    c = new Connection();
    c.execute();
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    System.out.println("STOP");
}

private class Connection extends AsyncTask<String, Void, String> {

    private Exception exception;
public void bla(){
    System.out.println("bla");
}
    protected String doInBackground(String ... arr) {
        try {
            cl = new Client(ip);
            cl.addListener(ls);
            cl.start();
            cl.send(ProtocolFactory.createGameListRequest());


            return "connected";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}

第一项活动

public class HostActivity extends AppCompatActivity {

boolean bound = false;
GameListResponse glresp;

private LocalService mBoundService;

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder      service) {
        mBoundService = ((LocalService.LocalBinder)service).getService();
        bound = true;
    }

    public void onServiceDisconnected(ComponentName className) {
        mBoundService = null;
        bound = false;
    }
};

void doBindService() {
    bindService(new Intent(this,
            LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_host);

}

@Override
protected void onStart() {
    super.onStart();
    doBindService();
}

@Override
protected void onStop() {
    super.onStop();
    if(bound){
        //unbindService(mConnection);
        bound = false;
    }
}

public void connect(View view) throws IOException, InterruptedException,ExecutionException {

    mBoundService.setIp("192.168.1.4");
    startActivity(new Intent(this, GameJoinActivity.class));
    finish();


Intent intent = new Intent(this, GameJoinActivity.class);
 startActivity(intent);
finish();

}

}

第二项活动

public class GameJoinActivity extends AppCompatActivity {

String name;
ClientRole role=ClientRole.PLAYER_LITERAL;
String ip;
String port;
int gameID;
GameListResponse glr;
ListView lw;
Spinner sp;
String spint;
ProcessingRequestReply prr;
boolean bound = false;

private LocalService mBoundService;

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        mBoundService = ((LocalService.LocalBinder)service).getService();
        bound = true;
    }

    public void onServiceDisconnected(ComponentName className) {
        mBoundService = null;
        bound = false;
    }
};

void doBindService() {
    bindService(new Intent(this,
            LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStart() {
    super.onStart();
    doBindService();
}

@Override
protected void onStop() {
    super.onStop();
    if(bound){
        unbindService(mConnection);
        bound = false;
    }
}

/**
@Override
protected void onCreate(Bundle savedInstanceState) {
    doBindService();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gamejoin);
 }


}

0 个答案:

没有答案