0

My App have a GCMIntentService extends on GCMBaseIntentService. When received a message, I will open an activity that have a Webview on it. Here the code:

GCMIntentService:

public class GCMIntentService extends GCMBaseIntentService {
    @Override
    protected void onMessage(Context context, Intent intent) {
        Intent intent = new Intent(context, MyActivity.class);
        context.startActivity(intent);
    }
}

MyActiviy:

public class MyActiviy extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        webView = new WebView(MyActiviy.this); 
        /*This throw an exception "java.lang.IllegalStateException:
        Calling View methods on another thread than the UI thread."*/
    }
}

Normaly, this code work fine, but when i kill app on "Recent apps" and push an message, then app is crash when create a new instance of webview on MyActivity.

I have try this code below:

public class MyActiviy extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webView = new WebView(MyActiviy.this);
                boolean isRunOnUIThread = Looper.myLooper() == Looper.getMainLooper();
                Log.i("CheckThread","is running on UI thread: " + isRunOnUIThread )
                // This log return true
            }
        });

    }
}

But this not work.

Crash log:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package.name/my.package.name.MyActivity}: java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
            at com.android.webview.chromium.WebViewChromium.createThreadException(WebViewChromium.java:287)
            at com.android.webview.chromium.WebViewChromium.checkThread(WebViewChromium.java:309)
            at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:220)
            at android.webkit.WebView.<init>(WebView.java:606)
            at android.webkit.WebView.<init>(WebView.java:542)
            at android.webkit.WebView.<init>(WebView.java:525)
            at android.webkit.WebView.<init>(WebView.java:512)
            at android.webkit.WebView.<init>(WebView.java:502)
            at my.package.name.MyActivity$2.run(MyActivity.java:254)
            at android.app.Activity.runOnUiThread(Activity.java:5511)
            at my.package.name.MyActivity.onCreate(MyActivity.java:246)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I guess my application is running on "wrong UI thread" :D
or the checkThread() method of WebWiew is wrong.

Note: This bug only occur after I kill the application and push a message.

Can anyone help me with this bug? Thanks so much.

Additional information: This bug only occur on android L and up (>= 5.0)

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
NghiaDao
  • 383
  • 1
  • 3
  • 14

2 Answers2

0

When you are starting activity from Service Please add Intent.FLAG_ACTIVITY_NEW_TASK flag in your intent.

    Intent intent = new Intent(context, MyActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
Gani
  • 3
  • 2
0

Change the line of code start activity like that:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {

        @Override
        public void run() {
            Intent intent = new Intent(context, MyActivity.class);
            context.startActivity(intent);
        }
    });
duynt
  • 128
  • 8