获取java.lang.ClassCastException:android.os.BinderProxy每次申报并运行两个服务 [英] Getting java.lang.ClassCastException: android.os.BinderProxy every time i declare and run two services

查看:663
本文介绍了获取java.lang.ClassCastException:android.os.BinderProxy每次申报并运行两个服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到我每次申报并运行两个服务如下binder.proxy例外。一个服务于不同的进程(专用于应用程序)运行和其他服务运行在同一进程中我的应用程序中(默认应用程序)与粘合剂实施运行。

I am encountering following binder.proxy exception every time i declare and run two services. One service runs in different Process(Private to app) and another service runs in same process as My Application is running in(Default App Process) with a Binder Implementation.

的Andr​​oidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.service.check"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:name="com.service.check.MainApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

         <service
            android:name="com.service.check.SecondService"
            android:exported="false"/>

        <service
            android:name="com.service.check.FirstService"
            android:process=":newProcess" >
        </service>
    </application>

</manifest>

我的按钮点击启动在MainActivity我的第一个服务为:

I am launching my first service in MainActivity on Button click as:

MainActivity.java

public class MainActivity extends ActionBarActivity implements OnClickListener {

    private Button mLanchServiceBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLanchServiceBtn=(Button) findViewById(R.id.launch_btn);

        mLanchServiceBtn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
       //Starting first service
        Intent launch=new Intent(this,FirstService.class);
        startService(launch);

    }
}

和第二服务MainApplication类。

And second service in MainApplication class as.

MainApplication.java

    public class MainApplication extends Application {

        private SecondService.LocalBinder mBinder;
        private ServiceConnection mConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName className, IBinder service) {
                mBinder = (LocalBinder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName arg0) {
            }
        };

        @Override
        public void onCreate() {
            super.onCreate();

            //starting second service               
            Intent launch=new Intent(this,SecondService.class);
            startService(launch);

            //Binding to it 
            bindService(launch, mConnection, BIND_AUTO_CREATE);
        }

    }

FirstService.java

public class FirstService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

SecondService.java

public class SecondService extends Service{

    //Service Containing Local Binder
    private LocalBinder mBinder=new LocalBinder();
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    class LocalBinder extends Binder{

        public LocalBinder() {
        }
    }
}

堆栈跟踪:

 02-05 10:32:25.035: E/AndroidRuntime(1424): Process:

 com.service.check:newProcess, PID: 1424 02-05 10:32:25.035:
 E/AndroidRuntime(1424): java.lang.ClassCastException:
 android.os.BinderProxy cannot be cast to
 com.service.check.SecondService$LocalBinder 02-05 10:32:25.035:
 E/AndroidRuntime(1424):    at
 com.service.check.MainApplication$1.onServiceConnected(MainApplication.java:23)
 02-05 10:32:25.035: E/AndroidRuntime(1424):    at
 android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1101)

我已经提到了以下链接进行梳理它说的问题, 如果我的活动和服务都在单独的进程那么我们不应该结合我做的方式。

I have referred the following links to sort out the issue which says, if my activity and service are in separate processes then we should not bind the way I have done.

<一个href="http://stackoverflow.com/questions/10573931/android-service-android-os-binderproxy-error">Android服务android.os.BinderProxy错误

<一个href="http://stackoverflow.com/questions/25049176/java-lang-classcastexception-android-os-binderproxy-cannot-be-cast-to-localbind">java.lang.ClassCastException: android.os.BinderProxy不能转换为LocalBinder

但是,在我的情况: 我结合 SecondService MainApplication 无一不是在同一个进程(即默认应用程序)运行。不过,我在面对binderProxy异常 SecondService ,然后我的 FirstService 运行中,我甚至不绑定独立的过程。

But in my case: I am binding to SecondService from MainApplication and both are running in same Process(i.e Default Application Process). Still I am facing binderProxy exception in SecondService , And my FirstService runs in separate process which I am not even binding to.

请帮我了这一情况,并推荐了我一个最好的方式,这样我可以实现相同的情况下,没有任何碰撞。

Please help me out with this situation and, Suggest me a best possible way so that I can implement same scenario without any crash.

推荐答案

做一些研究和调试后找到了答案,

Found an answer after doing some research and debugging,

如果我们创建和绑定任何服务到MainApplication类(然后服务被绑定到整体的ApplicationContext或BaseContext),如果同一应用程序包含了被绑定到活动的具体语境(S),

If we create and bind any service to a MainApplication class(then service gets binded to whole ApplicationContext or BaseContext) and if same application contains other services which are binded to Activity specific Context(s),

//Declared in MainApplication
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
                mBinder = (LocalBinder) service;
     }

在OnServiceConnected(),我们将得到的粘合剂对象既服务( SecondService 开始在MainApplication(注册BaseContext会得到当地binderObject)类和 FirstService 开始MainActivity(将获得的 android.os.binderProxyObject从而导致ClassCastException异常)。

In OnServiceConnected() We will get binder object for both the Services( SecondService Started in MainApplication(registered with BaseContext will get local binderObject) class and FirstService started MainActivity(will get android.os.binderProxyObject hence causing ClassCastException).

  • 因此​​,为确定这个问题人们必须启动所有应用程序 服务从任何活动上下文,而不是使用任何全球 应用程序上下文。另外这个问题的独立进程

  • So, to fix this issue one has to start all the application services from any Activity Context rather than using any Global Application Context. Also this issue is independent of the Processes

因此​​,我既感动和SecondService到FirstService MainActivity Context,它解决了该问题。

Hence, I moved both SecondService and FirstService into MainActivity Context which fixed the issue.

MainActivity.java

    private Button mLanchServiceBtn;
    private SecondService.LocalBinder mBinder;
    private ServiceConnection mConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName className, IBinder service) {
                mBinder = (LocalBinder) service;
            }
            @Override
            public void onServiceDisconnected(ComponentName arg0) {
            }
     };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mLanchServiceBtn=(Button) findViewById(R.id.launch_btn);

            mLanchServiceBtn.setOnClickListener(this);



            //starting second service in activity

            Intent launch=new Intent(this,SecondService.class);
            startService(launch);

            //Binding to it 
            bindService(launch, mConnection, BIND_AUTO_CREATE);
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }


        @Override
        public void onClick(View v) {

           //Starting FirstService also from MainActivity
            Intent launch=new Intent(this,FirstService.class);
            startService(launch);

        }
    }

这篇关于获取java.lang.ClassCastException:android.os.BinderProxy每次申报并运行两个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆