Licenced under the Creative Commons Attribution 4.0 licence. For full text see
http://creativecommons.org/licenses/by/4.0/
If you think the Android project 101AndroidApps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.hulzenga.ioi.android.app_009;
/*fromwww.java2s.com*/import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.hulzenga.ioi.android.AppActivity;
import com.hulzenga.ioi.android.R;
/**
* Created by jouke on 20-4-14.
*/publicclass ThumbsUpActivity extends AppActivity {
privatestaticfinal String TAG = "ThumbsUpActivity";
private ImageButton mThumbsUpButton;
private TextView mServerStatusTextView;
private ThumbsUpService mThumbsUpService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
publicvoid onServiceConnected(ComponentName name, IBinder service) {
ThumbsUpService.ThumbsUpBinder binder = (ThumbsUpService.ThumbsUpBinder) service;
mThumbsUpService = binder.getService();
setBound(true);
if (mThumbsUpService.isServerRunning()) {
setServerRunning(true);
}
}
@Override
publicvoid onServiceDisconnected(ComponentName name) {
setBound(false);
}
};
privateboolean mBound;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_009_activity_thumbs_up);
mThumbsUpButton = (ImageButton) findViewById(R.id.app_009_thumbsUpButton);
mServerStatusTextView = (TextView) findViewById(R.id.app_009_serverStatusTextView);
mServerStatusTextView.setMovementMethod(LinkMovementMethod.getInstance());
setBound(false);
setServerRunning(false);
mThumbsUpButton.setOnClickListener(new View.OnClickListener() {
@Override
publicvoid onClick(View v) {
//mBound should be true if button is enabled, but it can't hurt to check
if (mBound) {
if (!mThumbsUpService.isServerRunning()) {
if (mThumbsUpService.startServer()) {
setServerRunning(true);
} else {
Toast.makeText(ThumbsUpActivity.this, R.string.app_009_server_start_failure, Toast.LENGTH_SHORT).show();
}
} else {
mThumbsUpService.stopServer();
setServerRunning(false);
}
} else {
Log.w(TAG, "ThumbsUpButton clicked but the service is not bound !");
}
}
});
}
privatevoid setBound(boolean bound) {
if (bound) {
mBound = true;
mThumbsUpButton.setEnabled(true);
} else {
mBound = false;
mThumbsUpButton.setEnabled(false);
}
}
privatevoid setServerRunning(boolean running) {
if (running) {
mServerStatusTextView.setText(Html.fromHtml(getResources().getString(R.string.app_009_server_running_message)
+ " <a href=\"http://"+mThumbsUpService.getServerAddress()+"\">"+mThumbsUpService.getServerAddress()+"</a>"));
mThumbsUpButton.setImageDrawable(getResources().getDrawable(R.drawable.app_009_thumbs_up));
} else {
mServerStatusTextView.setText(R.string.app_009_start_server_instruction);
mThumbsUpButton.setImageDrawable(getResources().getDrawable(R.drawable.app_009_thumbs_down));
}
}
@Override
protectedvoid onStart() {
super.onStart();
Intent intent = new Intent(this, ThumbsUpService.class);
startService(intent);
bindService(intent, mServiceConnection, Context.BIND_ABOVE_CLIENT);
}
@Override
protectedvoid onStop() {
super.onStop();
unbindService(mServiceConnection);
if (mThumbsUpService != null && !mThumbsUpService.isServerRunning()) {
Intent intent = new Intent(this, ThumbsUpService.class);
stopService(intent);
}
}
}