Guest User

Untitled

a guest
Sep 30th, 2016
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. public class PingService extends Service
  2. {
  3.     private InetAddress serverAddress;
  4.     private Socket socket;
  5.  
  6.     @Override public int onStartCommand(Intent intent, int flags, int startID)
  7.     {
  8.         serverAddress = InetAddress.getByName(intent.getStringExtra("proximityAddress"));
  9.         new TCPreceiver().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this);
  10.         return START_STICKY;
  11.     }
  12.  
  13.     class TCPreceiver extends AsyncTask
  14.     {
  15.         InputStream inputStream;
  16.         OutputStream outputStream;
  17.         Context c;
  18.  
  19.         @Override protected Object doInBackground(Object[] objects)
  20.         {
  21.             c = (Context) objects[0];
  22.             try
  23.             {
  24.                 socket = new Socket(serverAddress, 4000);
  25.                 inputStream = socket.getInputStream();
  26.                 outputStream = socket.getOutputStream();
  27.             } catch (IOException e) {
  28.                 e.printStackTrace();
  29.                 stopSelf();
  30.                 return null;
  31.             }
  32.             NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(c) //blabla, robienie notyfikacji
  33.             startForeground(10, notificationBuilder.build()); //myślałem, że startForeground w tym miejscu powiąże AsyncTask z Servicem i nie zabije tego pierwszego, ale nie
  34.             while (true)
  35.             {
  36.                 try {
  37.                     inputStream.read(//czytaj bajty z czujnika);
  38.                             outputStream.write(//pisz bajty do czujnika);
  39.                 } catch(Exception e){/*wyjątki*/}
  40.             }
  41.         }
  42.     }
  43. }
Add Comment
Please, Sign In to add comment