Add stopHotspotAndDismissNotification()

This commit is contained in:
Adeel Zafar 2019-08-12 17:30:44 +05:00
parent c2cb7feae3
commit a3dbdda23a

View File

@ -51,25 +51,15 @@ public class HotspotService extends Service {
hotspotManager = new WifiHotspotManager(this); hotspotManager = new WifiHotspotManager(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopReceiver = new BroadcastReceiver() { stopReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction().equals(ACTION_STOP)) { if (intent != null && intent.getAction().equals(ACTION_STOP)) {
stopHotspot(); stopHotspotAndDismissNotification();
} }
} }
}; };
} else {
stopReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction().equals(ACTION_STOP)) {
dismissNotification();
}
}
};
}
registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP)); registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP));
webServerHelper = new WebServerHelper(this); webServerHelper = new WebServerHelper(this);
@ -98,7 +88,7 @@ public class HotspotService extends Service {
case ACTION_TURN_OFF_AFTER_O: case ACTION_TURN_OFF_AFTER_O:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopHotspot(); stopHotspotAndDismissNotification();
} }
break; break;
@ -110,7 +100,7 @@ public class HotspotService extends Service {
break; break;
case ACTION_STOP_SERVER: case ACTION_STOP_SERVER:
dismissNotification(); stopHotspotAndDismissNotification();
break; break;
default: default:
break; break;
@ -152,16 +142,10 @@ public class HotspotService extends Service {
} }
//Dismiss notification and turn off hotspot for devices>=O //Dismiss notification and turn off hotspot for devices>=O
@RequiresApi(Build.VERSION_CODES.O) void stopHotspotAndDismissNotification() {
void stopHotspot() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
hotspotManager.turnOffHotspot(); hotspotManager.turnOffHotspot();
webServerHelper.stopAndroidWebServer(serverStateListener);
stopForeground(true);
stopSelf();
} }
//Dismiss notification and turn off hotspot for devices < O
void dismissNotification() {
webServerHelper.stopAndroidWebServer(serverStateListener); webServerHelper.stopAndroidWebServer(serverStateListener);
stopForeground(true); stopForeground(true);
stopSelf(); stopSelf();