首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓扩展文件不会触发onDownloadStateChanged?

安卓扩展文件不会触发onDownloadStateChanged?
EN

Stack Overflow用户
提问于 2012-12-12 02:09:52
回答 1查看 1.2K关注 0票数 1

我正在尝试让我的应用程序下载一个扩展文件。到目前为止,我的代码如下。

代码语言:javascript
复制
public class main extends Activity implements IDownloaderClient {

TextView tv1;
Button mcButt;

@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  //////////
  tv1 = (TextView)findViewById(R.id.textView1);

  // Check whether the file have been downloaded.
  String mainFileName = Helpers.getExpansionAPKFileName(this, true, 1);
  boolean fileExists = Helpers.doesFileExist(this, mainFileName, 32921796L, false);

  if (!fileExists) {

   Intent launcher = getIntent();
   Intent fromNotification = new Intent(this, getClass());
   fromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
   fromNotification.setAction(launcher.getAction());

           if (launcher.getCategories() != null) {
                for (String cat : launcher.getCategories())
                { fromNotification.addCategory(cat); }
           }

   PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, fromNotification, PendingIntent.FLAG_UPDATE_CURRENT);

   try {

    int result = DownloaderClientMarshaller.startDownloadServiceIfRequired( this, pendingIntent, ExpansionFileDownloaderService.class);
    if (DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED != result) {

        // implement Downloading UI.
        tv1.setText("Need to download.");

     return;
    }

   } catch (Exception e) { 
    Log.e("apk-expansion-files", "NameNotFoundException occurred. " + e.getMessage(), e);
   }
  }

  tv1.setText("Downloaded!");


 }


///////////////////////////////////////////////////


@Override
public void onServiceConnected(Messenger m) {
    tv1.setText("onServiceConnected: " + m.toString() );
}

///////////

@Override
public void onDownloadStateChanged(int newState) {
    tv1.setText("onDownloadStateChanged: " + String.valueOf(newState) );
}

////////////

@Override
public void onDownloadProgress(DownloadProgressInfo progress) {
    ProgressBar mPB = (ProgressBar)findViewById(R.id.progressBar1);

    progress.mOverallTotal = progress.mOverallTotal;
    mPB.setMax((int) (progress.mOverallTotal >> 8));
    mPB.setProgress((int) (progress.mOverallProgress >> 8));
    tv1.setText(Long.toString(progress.mOverallProgress * 100 / progress.mOverallTotal) + "%");
    tv1.setText(Helpers.getDownloadProgressString(progress.mOverallProgress,progress.mOverallTotal));

}

}

文件下载正常,但在下载过程中或下载后不会触发onDownloadProgress或onDownloadStateChanged。

我想到的是,DownloaderClientMarshaller需要以某种方式与活动中实现的IDownloaderClient联系起来……

有什么想法吗?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2013-04-10 15:57:02

您应该创建存根并将其连接到上下文。然后,事件将被发送到您的IDownloaderClient。

代码语言:javascript
复制
if (DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED != result) {
  downloaderClientStub = DownloaderClientMarshaller.CreateStub(this,
                     YourDownloaderServiceClass.class);
  downloaderClientStub.connect(this);       
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13826001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档