Android:bindService始终返回false(扩展APK api)
我试图使用Google的APK扩展扩展来下载我与他们托管的扩展文件。我也使用SampleDownloadActivity的代码来做到这一点,虽然稍作修改以适应我的应用程序。Android:bindService始终返回false(扩展APK api)
我的问题是,永远不会启动下载。在实现IDownloadClient的类中,调用onStart(),但onServiceConnected()不是。
我已经追查这归因于该行DownloaderClientMarshaller:
if(c.bindService(bindIntent, mConnection, Context.BIND_DEBUG_UNBIND)) {
这始终返回false,因此该服务没有约束。
我在TabHost中使用调用活动,这对其他人造成了问题。他们说你不能传递TabHost上下文,而是将Application上下文传递给connect函数。我这样做改变了这个:代替
mDownloaderClientStub.connect(getApplicationContext());
:
mDownloaderClientStub.connect(this);
,但它并没有帮助,我仍然得到错误。如果这有所帮助,我正在模拟器上进行所有测试。
我真的把我的头发拉出来。如果有人有任何想法,我会非常感激!
在大多数情况下,如果服务未在应用程序的清单文件中声明,则bindService()
方法将返回false
。
在我的情况下,问题是我给了DownloaderClientMarshaller.CreateStub()
方法错误的类对象。我不小心使用了DownloaderService.class
而不是MyDownloaderService.class
。
使用下载器API时,请务必传递扩展基址DownloaderService
的正确类对象。
我推荐使用包含在Better APK Expansion包中的更新后的下载程序库。它解决了这个问题和其他问题,并且还提供了简化的API,从而最大限度地减少了在脚中拍摄自己的机会。
要获得下载进度,您只需要扩展BroadcastDownloaderClient
。
public class SampleDownloaderActivity extends AppCompatActivity {
private final DownloaderClient mClient = new DownloaderClient(this);
// ...
@Override
protected void onStart() {
super.onStart();
mClient.register(this);
}
@Override
protected void onStop() {
mClient.unregister(this);
super.onStop();
}
// ...
class DownloaderClient extends BroadcastDownloaderClient {
@Override
public void onDownloadStateChanged(int newState) {
if (newState == STATE_COMPLETED) {
// downloaded successfully...
} else if (newState >= 15) {
// failed
int message = Helpers.getDownloaderStringResourceIDFromState(newState);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDownloadProgress(DownloadProgressInfo progress) {
if (progress.mOverallTotal > 0) {
// receive the download progress
// you can then display the progress in your activity
String progress = Helpers.getDownloadProgressPercent(
progress.mOverallProgress, progress.mOverallTotal);
Log.i("SampleDownloaderActivity", "downloading progress: " + progress);
}
}
}
}
查看图书馆的page的完整文档。
检查此:可能帮助.. http://stackoverflow.com/a/2916829/1777090 – 2012-12-12 05:45:39
你解决了这个问题? – Bolein95 2017-07-18 11:15:56