2016-01-05 62 views
2

我有一个在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> 

回答

1

一号线变化节省了一天。

当我创建我的意图应该是

Intent i = new Intent(getApplicationContext(), GPSService.class); 
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); 
}