Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PingService extends Service
- {
- private InetAddress serverAddress;
- private Socket socket;
- @Override public int onStartCommand(Intent intent, int flags, int startID)
- {
- serverAddress = InetAddress.getByName(intent.getStringExtra("proximityAddress"));
- new TCPreceiver().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this);
- return START_STICKY;
- }
- class TCPreceiver extends AsyncTask
- {
- InputStream inputStream;
- OutputStream outputStream;
- Context c;
- @Override protected Object doInBackground(Object[] objects)
- {
- c = (Context) objects[0];
- try
- {
- socket = new Socket(serverAddress, 4000);
- inputStream = socket.getInputStream();
- outputStream = socket.getOutputStream();
- } catch (IOException e) {
- e.printStackTrace();
- stopSelf();
- return null;
- }
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(c) //blabla, robienie notyfikacji
- startForeground(10, notificationBuilder.build()); //myślałem, że startForeground w tym miejscu powiąże AsyncTask z Servicem i nie zabije tego pierwszego, ale nie
- while (true)
- {
- try {
- inputStream.read(//czytaj bajty z czujnika);
- outputStream.write(//pisz bajty do czujnika);
- } catch(Exception e){/*wyjątki*/}
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment