我有一個在API 14中工作的項目。現在我正在轉向API 21,因此我正在進行需要的更改。bindservice總是返回false
這是一個使用位置來跟蹤路線的應用程序。我有一個服務,照顧的位置的東西。但是,當我嘗試綁定到該服務時,它不斷返回假,我不知道爲什麼。
以下是我的代碼。我甚至不知道如何開始真正地看待這個。服務沒有約束力的原因是什麼?
下面是我的代碼:
服務連接類
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始終是假
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>