【问题标题】:Application won't bind to service应用程序不会绑定到服务
【发布时间】:2014-12-03 21:52:12
【问题描述】:

我有一个应用程序 A 必须绑定到另一个包中的服务。我已经放置了一个自定义意图过滤器以使其工作。可悲的是应用程序不会绑定。日志说找不到服务。

应用程序 A 在包“com.example.app_a”中 该服务在另一个包“com.example.app_talker_service”中

所以我只是不能用 xxx.class 解决方案引用服务,所以我的猜测是在服务包的清单文件中使用意图过滤器。

另一方面,应用程序 A 需要绑定到服务以使其启动(如果它尚未启动),稍后它将通过使用广播接收器与其通信。我做了一些实验,发现广播工作正常,但问题是由于某种原因,应用程序 A 在绑定期间似乎无法找到我的服务....

这是绑定在 onStart() 中的应用程序 A 的代码:

    @Override
protected void onStart() 
{
    // TODO Auto-generated method stub
    super.onStart();

    //Bind to service
    getApplicationContext().bindService(new Intent("com.example.talker_service.SERVICE"), mConnection,Context.BIND_AUTO_CREATE);


}
    private boolean mIsBound = false;

private ServiceConnection mConnection = new ServiceConnection()
{

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub
        mIsBound = true;
        Toast.makeText(getApplicationContext(), "CONNECTED TO SERVICE!", Toast.LENGTH_SHORT).show();

         Intent intent = new Intent();
         intent.setAction("com.example.talker_service.SERVICE");
         intent.putExtra("REQUEST", "REGISTER APP");

         intent.putExtra("FILTER", "com.example.app_a");
         intent.putExtra("NAME", "Applicazione A");

         String[] components = {"NUMBER_SENT","CHANGE_TEXT_COLOR","CHANGE_TEXTVIEW_SIZE"};
         intent.putExtra("COMPONENTS", components);

         MainActivityA.this.sendBroadcast(intent);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        mIsBound = false;
    }

};

这里是我称为 Talker_service 的服务的 cmanifest:

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name=".Talker_Service"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
            <intent-filter >
                <action android:name="com.example.talker_service.SERVICE"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain" />
            </intent-filter>
        </service>

        <activity
            android:name=".ConnectionManagerActivity"
            android:label="@string/title_activity_connection_manager" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

我不明白为什么它不绑定..我放了意图过滤器,我错过了什么吗?啊,日志是这样说的:

12-03 22:45:13.786: W/ContextImpl(26076): startService 的隐式意图是 不安全:意图{ 行动=com.example.talker_service.SERVICE } android.content.ContextWrapper.bindService:517 com.example.app_a.MainActivityA.onStart:81 android.app.Instrumentation.callActivityOnStart:1171 12-03 22:45:13.786: W/ActivityManager(764): 无法启动服务 Intent { >act=com.example.talker_service.SERVICE } U=0: 不 找到了

这是应用程序 A 的清单文件

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" ></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivityA"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

【问题讨论】:

    标签: android service bind


    【解决方案1】:

    我不知道为什么使用意图过滤器不起作用(它说它找不到它)但是通过使用以下代码,我能够使我的应用程序连接到远程服务:

    Intent i = new Intent();
        i.setClassName("com.example.app_talker_service", "com.example.app_talker_service.Talker_Service");
        bindService(i, conn, Context.BIND_AUTO_CREATE);
    

    所以使用 seClassName 方法并提供 pacake 的名称和服务的完整路径,我能够使其工作。我不确定这是否属于此类问题的“最佳实践”领域,但对我来说它有效。任何解决方案都比没有解决方案好。我找到了这个链接的解决方案:

    Remote Service Tutorial

    【讨论】:

    • 提供的这个链接很老了,我想现在方法调用setClass的签名已经改变了。
    猜你喜欢
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2016-07-20
    • 1970-01-01
    相关资源
    最近更新 更多