Home>

The program is running on Android Studio.
I want to output the contents of Log.d () of Android program (Java) to the actual Android device.

Error message

Currently, we are investigating whether Android internally distinguishes between mouse and trackpad.
By design, when the mouse is connected to an Android device, an arrow cursor appears on the screen and can be operated like a PC. We confirmed that the arrow cursor appears even when the trackpad is connected.

After investigating the method,
I tried to move the sample program because it can be identified with the method MotionEvent.getSource (), but with Log.d, the output is only Logcat, and actually connect the mouse and trackpad to the terminal Cannot check the output.
I would like to display the contents of this Log.d on the screen of the actual Android device, and confirm how.

Reference website: https://techbooster.org/android/application/2449/

Applicable source code
package jp.study.test4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.InputDevice;
import android.view.MotionEvent;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
    }
    @Override
    public boolean onTouchEvent (MotionEvent event) {
        // Get InputDevice
        InputDevice inputDevice = event.getDevice ();
        int deviceId = event.getDeviceId ();// Get device ID
        int source = event.getSource ();
        Log.d ("TouchEvent", "Input Device" + deviceId + "Source:" + source
                + "X:" + event.getX () + ", Y:" + event.getY ());
        return true;
    }
}
Supplemental information (FW/tool version etc.)

Device used: Huawei nova lite2 (Android8.0 Oreo)

  • Answer # 1

    Old, but there was an article like this:

    Use Timber + Log4J for Android app log output

      

    Use Log class to output logs on Android. Logs are output to the Android logging system and can be viewed with the logcat command.
      In most cases, this Log class is sufficient, but in business applications, it may be necessary to save the operation log as a file.
      The Log class does not have a function to format messages, so it is not suitable for outputting detailed operation logs.
      (Omitted)

    If it can be output to a file, I think it is easy to display it.

  • Answer # 2

    Is it wrong for toast display?
    A program that displays toasts when tapping and releasing the screen
    Kotlin is the one I wrote in the past, but for reference

    override fun onTouchEvent (event: MotionEvent): Boolean {
            var Event = ""
            when (event.action) {
                MotionEvent.ACTION_DOWN->Event = "tap"
                MotionEvent.ACTION_UP->Event = "released"
            }
            Toast.makeText (applicationContext, Event, Toast.LENGTH_SHORT) .show ()
            SystemClock.sleep (100)
            return super.onTouchEvent (event)
        }
    Code

  • Answer # 3

    How about using a tool called Hyperion-Android as a way to achieve it without much effort? Plug-ins that work with Timber are also available, so just install them and replace Log.d with Timber.d.

    Add the following to build.gradle

    debugImplementation 'com.willowtreeapps.hyperion: hyperion-core: 0.9.27'
    debugImplementation 'com.willowtreeapps.hyperion: hyperion-timber: 0.9.27'

    Hyperion-Android starts when you shake your device.

  • Answer # 4

    Thank you very much for your answers.
    I tried each answer and reference, but with the following code, the result of the method was successfully displayed on the actual machine screen.
    (I didn't know that TextView can also be used to change the dynamic text display on the screen)

    package jp.study.myapplication;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.InputDevice;
    import android.view.MotionEvent;
    import android.widget.TextView;
    public class MainActivity extends AppCompatActivity {
        private TextView textView;
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate (savedInstanceState);
            setContentView (R.layout.activity_main);
            textView = findViewById (R.id.action_source);
        }
        @Override
        public boolean onTouchEvent (MotionEvent event) {
            // Get InputDevice (instantiate)
            InputDevice inputDevice = event.getDevice ();
            // Get device ID
            int source = event.getSource ();
            String source_text = Integer.toString (source);
            textView.setText (source_text);
            return false;
        }
    }