bindservice总是返回false

时间:2016-01-05 01:45:32

标签: android

我有一个在API 14中工作的项目。现在我转到API 21,所以我正在进行我需要的更改。

这是一个使用位置来跟踪路线的应用。我有一个服务,负责处理位置的东西。但是当我尝试绑定到该服务时,它仍然会返回错误,我无法弄清楚原因。

以下是我的代码。我甚至不知道如何开始真正地看这个。服务没有约束力的原因是什么?

以下是我的代码:

服务连接类

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the object we can use to
        // interact with the service. We are communicating with the
        // service using a Messenger, so here we get a client-side
        // representation of that from the raw IBinder object.
        mServiceMessenger = new Messenger(service);

        // Now that we have the service messenger, lets send our messenger
        Message msg = Message.obtain(null, LOCATION_CHANGED, 0, 0);
        msg.replyTo = mClientMessenger;

        /*
         * In case we would want to send extra data, we could use Bundles:
         * Bundle b = new Bundle(); b.putString("key", "hello world");
         * msg.setData(b);
         */

        try {
            mServiceMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        mBound = true;
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        mServiceMessenger = null;
        mBound = false;
    }
};

bindService方法调用 - val始终为false

public boolean bindService() {
    /*
     * Note that this is an implicit Intent that must be defined in the
     * Android Manifest.
     */
    Intent i = new Intent();
    i.setPackage("com.example.conor.routetracker.ACTION_BIND");

    boolean val =  getBaseContext().getApplicationContext().bindService(i, mConnection,
            Context.BIND_AUTO_CREATE);

    return val;
}

Android清单

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/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.example.conor.routetracker.GPSService"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="com.example.conor.routetracker.ACTION_BIND" />
        </intent-filter>
    </service>

    <activity
        android:name="com.example.conor.routetracker.ListFiles"
        android:label="@string/title_activity_list_files" >
    </activity>
</application>

2 个答案:

答案 0 :(得分:1)

一行更改可以节省一天。

当我创建我的意图时,它应该是

Intent i = new Intent(getApplicationContext(), GPSService.class);

答案 1 :(得分:0)

public boolean bindService() {
    Intent i = new Intent("com.example.conor.routetracker.ACTION_BIND");
    i.setPackage("com.example.conor.routetracker")
    return getBaseContext().getApplicationContext().bindService(i, mConnection, Context.BIND_AUTO_CREATE);
}