Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 在bindService()之后没有连接的AIDL服务大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用AIDL开发2个应用程序(服务应用客户端应用程序)的设置.我目前有3个模块的设置:

> android-agent-framework(只保存AIDL文件的android库模块)
> android-agent(服务)
> android-example-client(客户端)

android-agent和android-agent-framework对第一个访问接口的依赖.

每当客户端调用bindservice()时,它都会返回false,而在serviceConnection中,不会调用onserviceConnected().同样在服务实现中,不调用onBind().日志中没有错误.

这是代码

android-agent活动:

public class MyCompanyStartActivity extends Activity {

  @Override
    protected void onCreate(Bundle savedInstanceStatE) {
        Log.i(MyCompanyStartActivity.class.toString(),"Create MyCompanyStartActivity");
        super.onCreate(savedInstanceStatE);
        setContentView(R.layout.activity_main);

        ComponentName service = startservice(new Intent(this,MyCompany@R_801_10613@estservice.class));
        Log.i("tag",service.getClassName() + "::" + service.getPackagename());
    }

}

android-agent服务:

public class MyCompany@R_801_10613@estservice extends service {

@Override
public IBinder onBind(Intent intent) {
    Log.i(MyCompany@R_801_10613@estservice.class.toString(),"StarTing smartRest service");
    return mBinder;
}

private final IMyCompany@R_801_10613@estservice.stub mBinder = new IMyCompany@R_801_10613@estservice.stub() {

    @Override
    public void sendData(String xid,String authentication,String data) throws remoteexception{
        Log.i(MyCompany@R_801_10613@estservice.class.toString(),"sending data: " + data);
    }
};

}

android-agent清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://scheR_152_11845@as.android.com/apk/res/android"
    package="com.mycompany.android.agent" >

    <application
        android:allowBACkup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@String/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyCompanyStartActivity"
            android:label="@String/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!-- services -->
        <service
            android:name="com.mycompany.android.agent.framework.MyCompany@R_801_10613@estservice"
            android:process=":remote"
            android:exported="true"
            android:enabled="true">
            <intent-filter>
                <action android:name="MyCompany@R_801_10613@estservice"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>

        <!-- Permissions -->

    </application>

</manifest>

android-example-client活动:

public class ClientStarter extends Activity {

    protected IMyCompany@R_801_10613@estservice mycompany@R_801_10613@estservice = null;

    @Override
    public void onCreate(Bundle savedInstanceStatE) {
        Log.i("tag","create client");
        super.onCreate(savedInstanceStatE);
        setContentView(R.layout.activity_main);

    }
    @Override
    protected void onStart() {
        super.onStart();
        if (mycompany@R_801_10613@estservice == null) {
            printservices();
            Intent it = new Intent("MyCompany@R_801_10613@estservice");
            it.setPackage("com.mycompany.android.agent.framework");
            Log.i("tag","before binding service: " + it.getAction() + "::" + it.getPackage());
            Boolean serviceBinding = getApplicationContext().bindservice(it,connection,Context.bIND_AUTO_create);
            Log.i("tag","service is bound: " + serviceBinding);
        }
        Handler handler = new Handler();
        handler.postDelayed(new Runner(),10000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindservice(connection);
    }

    private serviceConnection connection = new serviceConnection() {
        @Override
        public void onserviceConnected(ComponentName name,IBinder servicE) {
            Log.i("service","service connected");
            mycompany@R_801_10613@estservice = IMyCompany@R_801_10613@estservice.stub.asInterface(servicE);
            Toast.makeText(getApplicationContext(),"service Connected",Toast.LENGTH_SHORT).show();
            Log.i("service","service connected");
        }
        @Override
        public void onserviceDisconnected(ComponentName Name) {
            Log.i("service","service disconnected");
            mycompany@R_801_10613@estservice = null;
            Toast.makeText(getApplicationContext(),"service Disconnected","service disconnected");
        }
    };

    private void printservices() {
        Activitymanager manager = (Activitymanager) getSystemservice(Context.ACTIVITY_serviCE);
        for (Activitymanager.RunningserviceInfo service : manager.getRunningservices(Integer.max_value)) {
            Log.d("service",service.service.getClassName());
        }
    }

    private class Runner implements Runnable {

        @Override
        public void run() {
            Log.i("tag","starTing");
            LOCATIOnManager LOCATIOnManager = (LOCATIOnManager) getSystemservice(Context.LOCATION_serviCE);
            LOCATIOn loc;
            try {
                Thread.sleep(10000);
            } catch (InterruptedException E) {
                Log.e(ClientStarter.class.toString(),"Error",E);
            }            while(true) {
                try {
                    if (mycompany@R_801_10613@estservice != null) {
                        loc = LOCATIOnManager.getLastKNownLOCATIOn(LOCATIOnManager.GPS_PROVIDER);
                        Log.i(ClientStarter.class.toString(),loc.getLatitude() + " - " + loc.getLongitude() + " - " + loc.getAltitude());
                        mycompany@R_801_10613@estservice.sendData("test","auth",String.valueOf(loc.getLatitude()) + "," + String.valueOf(loc.getLongitude()) + "," + String.valueOf(loc.getAltitude()));
                    } else {
                        Log.i(ClientStarter.class.toString(),"service not yet available");
                    }
                    Thread.sleep(5000);
                } catch (InterruptedException E) {
                    Log.e(ClientStarter.class.toString(),E);
                } catch (remoteexception E) {
                    Log.e(ClientStarter.class.toString(),E);
                }
            }
        }


    }

}

尝试绑定服务之前的printservices()调用实际上列出了服务,因此它正在运行.

日志不包含任何错误,客户端最终在循环中运行,但服务仍为空.

也许有人之前遇到过类似的问题.

解决方法

经过所有文件的另一轮后,我发现了我的错误.

我需要改变:

Intent it = new Intent("MyCompany@R_801_10613@estservice");
it.setPackage("com.mycompany.android.agent.framework");

至:

Intent it = new Intent("MyCompany@R_801_10613@estservice");
it.setPackage("com.mycompany.android.agent");

Intent的包需要匹配应用程序的包而不是服务的包.

大佬总结

以上是大佬教程为你收集整理的android – 在bindService()之后没有连接的AIDL服务全部内容,希望文章能够帮你解决android – 在bindService()之后没有连接的AIDL服务所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。