Thegremlin
Thegremlin

Reputation: 89

Sample of handler not working for some reason

Here is my Main Activity:

package com.eddieharari.threadtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity {
    counter MyCounter = new counter("MyCounter");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView txt = findViewById(R.id.textView);
        Button button = findViewById(R.id.button);
        Button skipButton = findViewById(R.id.button2);

         button.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
                 Log.d("threadtest","Button Was Cliecked");
                 MyCounter.start();

             }
         });

         skipButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
                if (MyCounter.myHandler != null) {
                    Log.d("threadtest","Sending message to thread");
                    Message msg = MyCounter.myHandler.obtainMessage();
                    MyCounter.myHandler.sendMessage(msg);
                }
             }
         });


    }
}

Basically 2 buttons , one to start a thread and another one to send a message to that thread, Here is the thread class:

    package com.eddieharari.threadtest;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;

import androidx.annotation.NonNull;



public class counter extends Thread {
    public Handler myHandler;
    int mCounter;
    int mJumps;

    public counter(@NonNull String name) {
        super(name);
        mCounter = 0;
        mJumps = 1;

        }

    @SuppressLint("HandlerLeak")
    @Override
    public void run() {
        super.run();
        Log.d("thradtest","Thread had started!");
        Looper.prepare();

        myHandler=new Handler(){

            public void HandleMessage(Message m) {
                Log.d("threadtest","Inside Handler");
                if (m.what == 0) {
                    Log.d("threadtest","Got Message 0");
                }
            }
        };

        Looper.loop();

    }
}

When i run this sample , the thread starts , however I am not sure what exactly the Looper.loop() does since the thread does not get the message, or at least i can say that the thread handler not sending me the debug log line that it got message 0.

Upvotes: 0

Views: 65

Answers (1)

perflexed925
perflexed925

Reputation: 11

Maybe its a bit late to answer but the reason is because there is a typo in the handleMessage callback function. The h should be lowercase (See here). Because of this typo the handleMessage callback is never being called.

Upvotes: 1

Related Questions