Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not crash when log called on a background thread #7

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2,13 +2,16 @@

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Switch;

import com.hannesdorfmann.debugoverlay.DebugOverlay;

public class MainActivity extends AppCompatActivity {
Expand All @@ -22,16 +25,26 @@ public class MainActivity extends AppCompatActivity {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Switch threadSwitch = findViewById(R.id.thread_switch);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
boolean showDebugOverlay = Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(MainActivity.this);
if (showDebugOverlay) {
DebugOverlay.with(MainActivity.this).log("Message test " + i++);
final boolean inForeground = threadSwitch.isChecked();
if (inForeground) {
logMessage();
} else {
AsyncTask.execute(this::logMessage);
}
} else {
requestOverlayPermission();
}
}

private void logMessage() {
DebugOverlay.with(MainActivity.this).log("Message test " + i++);
}
});
}

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/res/layout/content_main.xml
Expand Up @@ -13,8 +13,10 @@
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">

<TextView
android:text="Hello World!"
<Switch
android:checked="true"
android:id="@+id/thread_switch"
android:text="Foreground thread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content" />
</RelativeLayout>
Expand Up @@ -3,6 +3,7 @@
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.View;
Expand Down Expand Up @@ -42,7 +43,11 @@ public void logMsg(String msg) {
});
}

view.addMessage(msg);
runOnUiThread(() -> view.addMessage(msg));
}

private void runOnUiThread(Runnable runnable) {
new Handler(getMainLooper()).post(runnable);
}

@Override public void onDestroy() {
Expand Down