4

I just want to use StrictMode.noteSlowCall . I don't know what to do. Developer site/androidxref don't have enough explanation.

Below is my code.

@Override
public void onCreate() {

    StrictMode.ThreadPolicy.Builder builder = new StrictMode.ThreadPolicy.Builder();
    builder.detectCustomSlowCalls();
    builder.penaltyLog();
    builder.penaltyDropBox();
    builder.penaltyDialog();
    builder.penaltyFlashScreen();

    StrictMode.setThreadPolicy(builder.build());

below is other file...

public static BigInteger computeRecursivelyWithCache(int n)

{

    StrictMode.noteSlowCall("jheeTest CustomSlowCall");
    SparseArray<BigInteger> cache = new SparseArray<BigInteger>();
    return computeRecursivelyWithCache(n);
}

OnClickListener mAboutKeyboardImageOnClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        computeRecursivelyWithCache(5);

I've tried to these codes. But it doesn't work at all.Even I pressed the button, it doesn't appear on log file or dropbox. Can you tell me what the problem is?

jhee
  • 41
  • 3
  • `I don't know what to do`. I don't know what it does or what it should do. And you did not explain for what you need it. – greenapps Sep 20 '16 at 09:50
  • I understood custom slow calls can be called in any where if i add it. Is this right? my first question. Second question is if i want to use right format of custom slow calls what should I do? developer site doesn't have enough explanation. With those code, I tried to make note Slow call in purpose, it didn't. it didn't show on log or dropbox at all. – jhee Sep 21 '16 at 10:10

2 Answers2

1

From the documentation ,

StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.

Using this StrictMode,you can detect slow method calls also .

StrictMode.noteSlowCall("jheeTest CustomSlowCall");

When you say "StrictMode.noteSlowCall" you are setting a policy telling the system to notify you when a slow method execution occurs .How to notify depends on what 'penalty' you have set .

Here, "jheeTest CustomSlowCall" is a TAG/msg for you to identify where the violation happened.

When you run your app and when a policy violation occurs ( in this particular policy , StrictMode detected that one of your method executed slowly ) , you will see a log in your logcat provided you have given penaltyLog()

I simulated a slow method call in one of my apps and used the tag "Slow_Call_Method_1"

11-30 15:11:38.574 16892-16892/com.PackageName D/StrictMode: StrictMode policy violation; ~duration=251 ms: android.os.StrictMode$StrictModeCustomViolation: policy=65551 violation=8 msg=Slow_Call_Method_1
    at android.os.StrictMode$AndroidBlockGuardPolicy.onCustomSlowCall(StrictMode.java:1397)
    at android.os.StrictMode.noteSlowCall(StrictMode.java:2340)
    at com.PackageName.TinyDBHelper.init(TinyDBHelper.java:41)
    at com.PackageName.MyApplication.onCreate(MyApplication.java:77)

See the message.

StrictMode policy violation; ~duration=251 ms: android.os.StrictMode$StrictModeCustomViolation: policy=65551 violation=8 msg=Slow_Call_Method_1

If have added lots of methods to be traced, the msg helps you identify which method caused the policy violation. In my case, the method for which I have set the TAG to "Slow_Call_Method_1" is the one which violated the policy

To answer your question,

Even I pressed the button, it doesn't appear on log file or dropbox.

It dint appear on log file because the system dint detect that it is a slow call - meaning the method dint run slow enough for the violation to fail

0

Make sure that you set all this StrictMode stuff not in Application.onCreate, but in Activity.onCreate. Because there is a bug.

But in general your code is correct. You build policy with detectCustomSlowCalls mask. Then you mark your slow (heavy) method with calling StrictMode.noteSlowCall. When your application tries to call this slow method in UI thread, you will know about it.

This thing helps you to be sure that you don't call slow methods in UI thread.