bodeme / androidwebserver Goto Github PK
View Code? Open in Web Editor NEWAndroid Webserver
License: GNU General Public License v3.0
Android Webserver
License: GNU General Public License v3.0
/*
*/
package com.bolutions.webserver;
import android.app.AlertDialog;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.*;
public class ServerService extends Service {
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;
}
}
Hi, I was trying the app, which i found awesome. My plan is to run a miniwebsite inside the classroom using my Andro-mobile and school wifinetwork.
While testing: Opening my Firefox and writing the local server address and port (example : 192.168.1.110:8080) nothing is loading.
If i write : 192.168.1.110:8080/ inde (purposefully wrong) -> i get the 404 page which a good sign.
if i write : 192.168.1.110:8080/index.html -> rather than opening the page on the Firefox, i get download pop message.
Also, it is possible to navigate between page inside the same root directory and is this code supporting php, apache, sql??
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.