private int NOTIFICATION_ID = 4711;
private NotificationManager mNM;
private String message;
private Notification notification;
private Server server;
private boolean isRunning = false;
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
private void showNotification() {
updateNotifiction("");
// startForeground(NOTIFICATION_ID, notification);
}
public void startServer(Handler handler, String documentRoot, int port) {
try {
isRunning = true;
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ipAddress = intToIp(wifiInfo.getIpAddress());
if( wifiInfo.getSupplicantState() != SupplicantState.COMPLETED) {
new AlertDialog.Builder(this).setTitle("Error").setMessage("Please connect to a WIFI-network for starting the webserver.").setPositiveButton("OK", null).show();
throw new Exception("Please connect to a WIFI-network.");
}
server = new Server(handler, documentRoot, ipAddress, port, getApplicationContext());
server.start();
Intent i = new Intent(this, StartActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);
updateNotifiction("Webserver is running on port " + ipAddress + ":" + port);
Message msg = new Message();
Bundle b = new Bundle();
b.putString("msg", "Webserver is running on port " + ipAddress + ":" + port);
msg.setData(b);
handler.sendMessage(msg);
} catch (Exception e) {
isRunning = false;
Log.e("Webserver", e.getMessage());
updateNotifiction("Error: " + e.getMessage());
}
}
public static String intToIp(int i) {
return ((i ) & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
((i >> 16 ) & 0xFF) + "." +
( i >> 24 & 0xFF);
}
public void stopServer() {
if(null != server) {
server.stopServer();
server.interrupt();
isRunning = false;
}
}
public void updateNotificationold(String message) {
CharSequence text = message;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, StartActivity.class), 0);
if (notification == null) {
notification = new Notification(R.mipmap.ic_launcher, text, System.currentTimeMillis());
// notification.setLatestEventInfo(this, getString(R.string.app_name), text, contentIntent);
mNM.notify(NOTIFICATION_ID, notification);
} else {
// notification.setLatestEventInfo(this, getString(R.string.app_name), text, contentIntent);
mNM.notify(NOTIFICATION_ID, notification);
}
}
public void updateNotifiction(String message) {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = message;
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, StartActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher) // the status icon
.setTicker(text) // the status text
.setWhen(System.currentTimeMillis()) // the time stamp
.setContentTitle("Noti") // the label of the entry
.setContentText(text) // the contents of the entry
.setContentIntent(contentIntent) // The intent to send when the entry is clicked
.build();
// Send the notification.
mNM.notify(NOTIFICATION_ID, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
// Cancel the persistent notification.
mNM.cancel(NOTIFICATION_ID);
// Tell the user we stopped.
Toast.makeText(this, "Service Stop", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
ServerService getService() {
return ServerService.this;
}
}
public boolean isRunning() {
return isRunning;
}