Android RemoteService无法启动服务意图

时间:2013-12-06 06:53:57

标签: android android-intent android-service

我正在尝试构建一个远程服务,我可以将其绑定到将经常使用和创建的活动。我认为这是处理我想要实现的目标的最佳方法。我不断收到这个错误。

无法启动服务Intent {act = com.services.OverheadService} U = 0:未找到

我觉得我的清单可能有问题吗?但我看不到什么,我的清单将在下面相关代码的底部。

以下是我的活动中onCreate()内的呼叫:

// TESTING SERVICE IMPLMENTATION TODO
Intent serviceIntent = new Intent("com.services.OverheadService");
boolean ok = this.bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
Log.v("ok", String.valueOf(ok));

这是Connection方法:

/** SERVICE IMPLEMENTATION TESTING **/
    private ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // get instance of the aidl binder
            mRemoteService = IRemoteService.Stub.asInterface(service);
            try {
                String message = mRemoteService.sayHello("Mina");
                Log.v("message", message);
            } catch (RemoteException e) {
                Log.e("RemoteException", e.toString());
            }

        }
    };

这是服务类:

package com.services;

import com.services.IRemoteService.Stub;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class OverheadService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    // implementation of the aidl interface
    private final IRemoteService.Stub mBinder = new Stub() {

        @Override
        public String sayHello(String message) throws RemoteException {
            return "Hello " + message;
        }
    };

}

正在设置服务的AndroidManifest:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name="com.services.OverheadService"
            android:enabled="true"
            android:icon="@drawable/failed_load" >

            <!--
            intent-filter>
                <action android:name="com.cdkdevelopment.BaseActivity" />
            </intent-filter>
            -->
        </service>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,它正在改变onCreate

Intent serviceIntent = new Intent(this, OverheadService.class);
boolean ok = this.getApplicationContext().bindService(serviceIntent,
    mServiceConnection, Context.BIND_AUTO_CREATE);
Log.v("ok", String.valueOf(ok));