gpt4 book ai didi

android - bindservice 总是返回 false

转载 作者:行者123 更新时间:2023-11-29 01:24:52 27 4
gpt4 key购买 nike

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

这是一个使用位置来跟踪路线的应用程序。我有一项服务可以处理位置信息。但是当我尝试绑定(bind)到该服务时,它总是返回 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 始终为 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 list

<?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>

最佳答案

一行更改可以挽救一天。

当我创建我的 Intent 时,它应该是

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

关于android - bindservice 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34602939/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com