Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

// modify notification for SDK 23+ #2

Open
winaungcho opened this issue May 29, 2020 · 0 comments
Open

// modify notification for SDK 23+ #2

winaungcho opened this issue May 29, 2020 · 0 comments

Comments

@winaungcho
Copy link

/*

  • Copyright (C) 2009-2014 Markus Bode
  • Licensed under the GNU General Public License v3
  • This program is free software: you can redistribute it and/or modify
  • it under the terms of the GNU General Public License as published by
  • the Free Software Foundation, either version 3 of the License, or
  • (at your option) any later version.
  • This program is distributed in the hope that it will be useful,
  • but WITHOUT ANY WARRANTY; without even the implied warranty of
  • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  • GNU General Public License for more details.
  • You should have received a copy of the GNU General Public License
  • along with this program. If not, see http://www.gnu.org/licenses/.

*/

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;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant