首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用onServiceConnected

未调用onServiceConnected
EN

Stack Overflow用户
提问于 2017-02-20 21:35:48
回答 1查看 418关注 0票数 0

我正在开发一个cordava插件来管理我的android扩展文件。读取扩展文件是可行的,但我在下载它们时遇到了问题。

这是我的插件:

代码语言:javascript
复制
public class ExpansionFileReader extends CordovaPlugin implements IDownloaderClient {

    private final String MEDIA_FOLDER_NAME = "mediafiles";
    private final String MAIN_EXPANSION = "main_expansion";
    private final String URL = "url";
    private final String TAG = "expansionFileReader";

    private Context context;
    private CallbackContext callbackContext;   


    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
    }

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        JSONObject callbackValue = new JSONObject();
        context = this.cordova.getActivity().getApplicationContext();
        if(action.equalsIgnoreCase("downloadExpansionFileIfNecessary")) {
            this.callbackContext = callbackContext;
            downloadExpansionFileIfNecessary();

        } else if(action.equalsIgnoreCase("getFile")){

            [getFileAction]

        }

        return true;
    }

    private void downloadExpansionFileIfNecessary() {

        if (!expansionFilesDelivered()) {
            Log.d(TAG, "NO EXPANSION FILE FOUND");
            try {
                Intent launchIntent = this.cordova.getActivity().getIntent();
                Intent intentToLaunchThisActivityFromNotification = new Intent((context), context.getClass());
                intentToLaunchThisActivityFromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());

                if (launchIntent.getCategories() != null) {
                    for (String category : launchIntent.getCategories()) {
                        intentToLaunchThisActivityFromNotification.addCategory(category);
                    }
                }

                PendingIntent pendingIntent = PendingIntent.getActivity((context), 0, intentToLaunchThisActivityFromNotification, PendingIntent.FLAG_UPDATE_CURRENT);
                // Request to start the download
                Log.d(TAG, "REQUEST TO START DOWNLOAD");
                int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(context, pendingIntent, DownloaderService.class);

                if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {

                    mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, DownloaderService.class);
                    if (null != mDownloaderClientStub) {
                        mDownloaderClientStub.connect(context);
                    }

                    return;
                } 
                else {
                    Log.d(TAG, "DOWNLOAD NOT NECESSARY");
                    callbackContext.success();
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "Cannot find package!", e);
            }
        } else {
            validateXAPKZipFiles();
        }
    }


    /////////////////////////////
    // INIT DOWNLOADS
    /////////////////////////////

    private IDownloaderService mRemoteService;
    private IStub mDownloaderClientStub;
    private int mState;
    private boolean mCancelValidation;

    // region Expansion Downloader
    private static class XAPKFile {
        public final boolean mIsMain;
        public final int mFileVersion;
        public final long mFileSize;

        XAPKFile(boolean isMain, int fileVersion, long fileSize) {
            mIsMain = isMain;
            mFileVersion = fileVersion;
            mFileSize = fileSize;
        }
    }

    private static final XAPKFile[] xAPKS = {
            new XAPKFile(
                    true, // true signifies a main file
                    1000006, // the version of the APK that the file was uploaded against
                    443975466L // the length of the file in bytes
            )
    };
    static private final float SMOOTHING_FACTOR = 0.005f;

    @Override
    public void onStart() {
        Log.d(TAG, "ON START");
        if (null != mDownloaderClientStub) {
            mDownloaderClientStub.connect(context);
        }
        super.onStart();
    }

    @Override
    public void onStop() {
        Log.d(TAG, "ON STOP");
        if (null != mDownloaderClientStub) {
            mDownloaderClientStub.disconnect(context);
        }
        super.onStop();
    }

    @Override
    public void onServiceConnected(Messenger m) {
        Log.d(TAG, "ON SERVICE CONNECTED");
        mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
        mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
    }

    @Override
    public void onDownloadStateChanged(int newState) {
        Log.d(TAG, "ON DOWNLOAD STATE CHANGED: " + newState);
        setState(newState);
        boolean showDashboard = true;
        boolean showCellMessage = false;
        boolean paused;
        boolean indeterminate;
        switch (newState) {
            case IDownloaderClient.STATE_IDLE:
                // STATE_IDLE means the service is listening, so it's
                // safe to start making calls via mRemoteService.
                paused = false;
                indeterminate = true;
                break;
            case IDownloaderClient.STATE_CONNECTING:
            case IDownloaderClient.STATE_FETCHING_URL:
                showDashboard = true;
                paused = false;
                indeterminate = true;
                break;
            case IDownloaderClient.STATE_DOWNLOADING:
                paused = false;
                showDashboard = true;
                indeterminate = false;
                break;

            case IDownloaderClient.STATE_FAILED_CANCELED:
            case IDownloaderClient.STATE_FAILED:
            case IDownloaderClient.STATE_FAILED_FETCHING_URL:
            case IDownloaderClient.STATE_FAILED_UNLICENSED:
                paused = true;
                showDashboard = false;
                indeterminate = false;
                break;
            case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:
            case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:
                showDashboard = false;
                paused = true;
                indeterminate = false;
                showCellMessage = true;
                break;

            case IDownloaderClient.STATE_PAUSED_BY_REQUEST:
                paused = true;
                indeterminate = false;
                break;
            case IDownloaderClient.STATE_PAUSED_ROAMING:
            case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE:
                paused = true;
                indeterminate = false;
                break;
            case IDownloaderClient.STATE_COMPLETED:
                showDashboard = false;
                paused = false;
                indeterminate = false;
                validateXAPKZipFiles();
                return;
            default:
                paused = true;
                indeterminate = true;
                showDashboard = true;
        }            
    }


    @Override
    public void onDownloadProgress(DownloadProgressInfo progress) {
        Log.d(TAG, "ON DOWNLOAD PROGESS: " + progress);            
    }


    boolean expansionFilesDelivered() {
        Log.d(TAG, "IF EXPANSION FILE IS DELIVERED");
        for (XAPKFile xf : xAPKS) {
            String fileName = Helpers.getExpansionAPKFileName(context, xf.mIsMain, xf.mFileVersion);
            if (!Helpers.doesFileExist(context, fileName, xf.mFileSize, false)) {
                Log.d(TAG, "EXPANSION FILE DOESN'T EXIST");
                return false;
            }
        }
        Log.d(TAG, "EXPANSION FILE EXIST");
        return true;
    }

}

我的问题是,尽管我调用了mDownloaderClientStub.connect(context);,但从未调用过onServiceConnected

有人知道为什么不叫它吗?我也没有收到任何错误。

EN

回答 1

Stack Overflow用户

发布于 2017-08-02 01:12:35

无法建立服务连接,因为您将错误的Class对象传递到扩展库方法中。

您应该将DownloaderClientMarshaller调用中的参数从DownloaderService.class更改为MyDownloaderService.class或用于扩展基类DownloaderService的任何类。此外,请确保在应用程序的清单中定义了您的服务。

代码语言:javascript
复制
// use the correct service class!
int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(context, pendingIntent, MyDownloaderService.class);

// when creating a stub also
mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, MyDownloaderService.class);

我推荐使用Better APK Expansion包中包含的更新后的下载器库。它解决了这个问题和其他问题,还提供了简化的API,最大限度地减少了搬起石头砸自己的脚的机会。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42346002

复制
相关文章

相似问题

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