Back to project page notify.
The source code is released under:
GNU General Public License
If you think the Android project notify listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.notify; //from w ww. j a va 2s . c o m import android.app.IntentService; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.InetSocketAddress; import java.net.Socket; public class BackgroundService extends IntentService { public static MainActivity ma; public static AccessibilityListenerService aa; public static Boolean reading = false; public static Socket s; public static Uri host = Uri.parse("notify://null:0"); public static DataInputStream dis; public static DataOutputStream dos; public IntentBroadcastReceiver ibr = new IntentBroadcastReceiver(); private Boolean filterRegistered = false; public BackgroundService() { super("BackgroundService"); } protected void onHandleIntent(Intent i) { if (BackgroundService.reading) { stopSelf(); return; } Uri u = i.getData(); BackgroundService.host = u; if (tryConnect(u)) { IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.BATTERY_CHANGED"); registerReceiver(ibr, filter); filterRegistered = true; read(); } else { BackgroundService.s = null; stopSelf(); } } public void onDestroy() { super.onDestroy(); if (filterRegistered) { unregisterReceiver(ibr); } } private boolean tryConnect(Uri host) { try { Integer port = 18081; if (host.getPort() != -1) { port = host.getPort(); } if (BackgroundService.s == null) { BackgroundService.s = new Socket(); BackgroundService.s.connect(new InetSocketAddress(host.getHost(), port), 4000); } BackgroundService.dos = new DataOutputStream(BackgroundService.s.getOutputStream()); BackgroundService.dis = new DataInputStream(BackgroundService.s.getInputStream()); OurUtils.sendMessageToActivity(EnumMessage.connection_succeeded); return true; } catch (Exception e) { OurUtils.sendMessageToActivity(EnumMessage.connection_failed, e.toString()); return false; } } public void sendToServer(String s) { if (!BackgroundService.reading) { return; } try { BackgroundService.dos.writeBytes(s + "\r\n"); } catch (Exception e) { OurUtils.sendMessageToActivity(EnumMessage.connection_died, e.toString()); } } public void read() { if (BackgroundService.reading) { return; } BackgroundService.reading = true; String line; String error = ""; while (BackgroundService.reading) { try { line = BackgroundService.dis.readLine().trim(); } catch (Exception e) { if (BackgroundService.reading) { error = e.toString(); } break; } String[] linesplit = line.split(" "); if (linesplit[0].equals("PING") && linesplit.length > 1) { sendToServer("PONG " + linesplit[1]); } } BackgroundService.reading = false; if (!error.equals("")) { OurUtils.sendMessageToActivity(EnumMessage.connection_died, error); } BackgroundService.s = null; stopSelf(); } }