开发者

How to call "isInitialStickyBroadcast()" and avoid problems on 1.6?

isInitialStickyBroadcast() is obviously only available after 2.0 (SDK 5).

I'm getting this error:

"Uncaught handler: thread main exiting due to uncaught exception java.lang.VerifyError"

It's only happening on 1.6. Android 2.0 and up doesn't have any problems, but that's the main point of all.

I can't catch the Error/Exception (java.lang.VerifyError), and I know it's being caused by calling isInitialStickyBroadcast() which is not available in SDK 4, that's why it's wrapped in the SDK check.

I just need this BroadcastReceiver to work on 2.0+ and not break in 1.6, it's an app in the market, the UNDOCK feature is needed for users on 2.0+ but obviously not in 1.6 but there is a fairly substantial number of users still on 1.6.

Here's an easy-to-read version of part of the code I'm using. Notice that it's wrapped in an SDK check to only run on 2.0+, but the VerifyError is still showing up.

private BroadcastReceiver 开发者_如何学JAVAmUndockedReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //FROM ECLAIR FORWARD, BEFORE DONUT THIS INTENT WAS NOT IMPLEMENTED
        if (Build.VERSION.SDK_INT >= 5)
        {
            if (!isInitialStickyBroadcast()) {
                //Using constant instead of Intent.EXTRA_DOCK_STATE to avoid problems in older SDK versions
                int dockState = intent.getExtras().getInt("android.intent.extra.DOCK_STATE", 1);
                if (dockState == 0)
                {
                    finish();
                }
            }
        }
    }
}; 


Your problem is that while you would not be executing isInitialStickyBroadcast(), the classloader attempts to resolve all methods when the class is loaded, so your SDK 4 devices fail at that point, since there is no isInitialStickyBroadcast().

You have two main options:

  1. Use reflection.
  2. Create two editions of your BroadcastReceiver, as public classes in their own files. One has the SDK 4 logic, one has the SDK 5+ logic. Register the one you want based on an SDK check at the time you call registerReceiver().
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜