微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 为什么我的意图没有正确传递?

我在Android应用程序中启动服务时遇到问题.以下代码应该可以正常工作.但通常并非总是如此,startService命令的Intent未正确传递给onStartCommand方法.这意味着服务类接收了intent,但是额外的内容丢失了:

D/TestService: onStartCommand() called with: intent = [Intent { cmp=***/.services.hostConnection.TestService launchParam=MultiScreenLaunchParams { mdisplayId=0 mFlags=0 } (has extras) }], flags = [0], startId = [6]
D/TestService: onStartCommand: Bundle[mParcelledData.dataSize=0]

另外我有时会遇到RuntimeException:无法创建服务***.services.hostConnection.TestService(下面的完整堆栈跟踪)

我现在已经连续几天了,但找不到这个bug …

一个极简主义的服务类,它可以重现这个问题:

public class TestService extends Service {
    private static final String TAG = "TestService";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() called with: intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");
        if(intent != null && intent.getExtras() != null) Log.d(TAG, "onStartCommand: " + intent.getExtras().toString());
        return START_NOT_STICKY;
    }
}

启动服务的方法

public void startLongUpdates(long hostId){
    Log.d(TAG, "startLongUpdates() called");

    Intent intent = new Intent(Application.getInstance(), TestService.class).putExtra(Constants.SERVICE_ACTION_INTENT_EXTRA, HostConnectionService.LONG_UPDATE).putExtra(Constants.CONNECTED_HOST_ID_INTENT_EXTRA, hostId);
    Log.d(TAG, "startLongUpdates: " + intent.getExtras().toString());
    Application.getInstance().startService(intent);

}

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: ***, PID: 24974
                                                           java.lang.RuntimeException: Unable to create service ***.services.hostConnection.TestService: java.lang.SecurityException: Binder invocation to an incorrect interface
                                                               at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
                                                               at android.app.ActivityThread.-wrap6(ActivityThread.java)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1740)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:154)
                                                               at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                            Caused by: java.lang.SecurityException: Binder invocation to an incorrect interface
                                                               at android.os.Parcel.readException(Parcel.java:1693)
                                                               at android.os.Parcel.readException(Parcel.java:1646)
                                                               at android.hardware.display.IdisplayManager$Stub$Proxy.getdisplayInfo(IdisplayManager.java:718)
                                                               at android.hardware.display.displayManagerGlobal.getdisplayInfo(displayManagerGlobal.java:143)
                                                               at android.hardware.display.displayManagerGlobal.getCompatibledisplay(displayManagerGlobal.java:200)
                                                               at android.hardware.display.displayManagerGlobal.getRealdisplay(displayManagerGlobal.java:214)
                                                               at android.app.ContextImpl.createAppContext(ContextImpl.java:2183)
                                                               at android.app.ActivityThread.handleCreateService(ActivityThread.java:3499)
                                                               at android.app.ActivityThread.-wrap6(ActivityThread.java) 
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1740) 
                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                               at android.os.Looper.loop(Looper.java:154) 
                                                               at android.app.ActivityThread.main(ActivityThread.java:6688) 
                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

有时也会出现此异常:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                         Process: ***, PID: 5355
                                                         java.lang.RuntimeException: Unable to start service ***.services.hostConnection.TestService@9386a65 with Intent { cmp=***/.services.hostConnection.TestService (has extras) }: java.lang.RuntimeException: UnkNown exception code: 2 msg ServiceActionIntentExtra
                                                             at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027)
                                                             at android.app.ActivityThread.-wrap17(ActivityThread.java)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
                                                             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.RuntimeException: UnkNown exception code: 2 msg ServiceActionIntentExtra
                                                             at android.os.Parcel.readException(Parcel.java:1634)
                                                             at android.os.Parcel.readException(Parcel.java:1573)
                                                             at android.app.ActivityManagerProxy.serviceDoneExecuting(ActivityManagerNative.java:3814)
                                                             at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3019)
                                                             at android.app.ActivityThread.-wrap17(ActivityThread.java) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442) 
                                                             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) 

表现:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="***">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name="***.Application"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">


    <activity
        android:launchMode="singletop"
        android:name="***.gui.menu.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    <activity
        android:name=".gui.SplashActivity"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="***.gui.menu.KNownHostInfoActivity"
        android:theme="@style/AppTheme.NoActionBar" />

    <service android:name="***.services.hostConnection.TestService" />

</application>

Application.java:

public class Application extends android.app.Application{
    private static final String TAG = "Application";
    private static Application application;
    public static Application getInstance(){
        return application;
    }
    public void onCreate() {
        super.onCreate();
        application = this;
    }
}

解决方法:

解决了这个问题!它与应用程序另一端使用的包裹有关…实际上,循环中使用的包裹被提前回收并由应用程序的其他部分重用.因此,这导致应用程序的许多部分出现各种问题.

还是要谢谢你的帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐