Toast Stop - getConnectedNodes

I made a service to determine if my smartphone can interact with my smartwat.

If I add this line to my code:

NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mApiClient).await();

When I close the application, it stops receiving Toast messages. I do not know why.

My service code

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    initGoogleApiClient();
    new Thread( new Runnable() {
        @Override
        public void run() {
            while(true) {
                    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mApiClient).await();
                    Message messageWithoutSignal= new Message();
                    messageWithoutSignal.arg1 = 1;
                    messages.sendMessage(messageWithoutSignal);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
    return START_STICKY;
}

 Handler messages= new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        if(msg.arg1==1)
            Toast.makeText(getApplicationContext(), "Doesn't have signal!", Toast.LENGTH_SHORT).show();
        return false;
    }
});

Thanks.

+4
source share
1 answer

I used TimerTask, and now it works instead of Thread.sleep. http://developer.android.com/reference/java/util/TimerTask.html

0
source

Source: https://habr.com/ru/post/1623423/


All Articles