服务下载进度时Android UI冻结

服务下载进度时Android UI冻结,android,android-service,freeze,bitcoinj,Android,Android Service,Freeze,Bitcoinj,我正在使用bitcoinj库创建一个Android钱包。情况是,我希望区块链在后台下载,以便用户可以开始使用应用程序的其他功能,但当它开始下载并打开新活动时,应用程序冻结,你不能使用任何东西,甚至不能返回到上一个屏幕。这是代码的一部分: public class BitcoinService extends Service { private final PeerDataEventListener blockchainDownloadListener = new AbstractPeer

我正在使用bitcoinj库创建一个Android钱包。情况是,我希望区块链在后台下载,以便用户可以开始使用应用程序的其他功能,但当它开始下载并打开新活动时,应用程序冻结,你不能使用任何东西,甚至不能返回到上一个屏幕。这是代码的一部分:

public class BitcoinService extends Service {
    private final PeerDataEventListener blockchainDownloadListener = new AbstractPeerEventListener() {
        private final long CALLBACK_TIME = 1000L;
        private final AtomicLong lastMessageTime = new AtomicLong(0);

        private int height;
        private int blocksLeft;
        private Block block;

        @Override
        public void onBlocksDownloaded(final Peer peer, final Block block, final FilteredBlock filteredBlock, final int blocksLeft) {
            config.maybeIncrementBestChainHeightEver(blockChain.getChainHead().getHeight());

            delayHandler.removeCallbacksAndMessages(null);

            final long now = System.currentTimeMillis();

            this.block = block;
            this.height = (int) peer.getBestHeight() - blocksLeft;
            this.blocksLeft = blocksLeft;

            if (now - lastMessageTime.get() > CALLBACK_TIME) {
                delayHandler.post(RUNNER);
            } else {
                delayHandler.postDelayed(RUNNER, CALLBACK_TIME);
            }
        }

        private final Runnable RUNNER = new Runnable() {
            @Override
            public void run() {
                notifyBlockchainProgress(height, (height + blocksLeft));
                Log.e(TAG, "LAST_BLOCK=" + height + ", REMAINS=" + blocksLeft);
                if (blocksLeft == 0) {
                    broadcastBlockchainDownloaded();
                }
            }
        };
    };

    private final BroadcastReceiver connectivityReceiver = new BroadcastReceiver() {
        @SuppressLint("Wakelock")
        @Override
        public void onReceive(final Context context, final Intent intent) {
            Log.d(TAG, "acquiring wakelock");
            wakeLock.acquire();

            // consistency check
            final int walletLastBlockSeenHeight = wallet.getLastBlockSeenHeight();
            final int bestChainHeight = blockChain.getBestChainHeight();
            if (walletLastBlockSeenHeight != -1 && walletLastBlockSeenHeight != bestChainHeight) {
                final String message = "wallet/blockchain out of sync: " + walletLastBlockSeenHeight + "/" + bestChainHeight;
                Log.e(TAG, message);
            }

            Log.i(TAG, "starting peergroup");
            peerGroup = new PeerGroup(Constants.WALLET.NETWORK_PARAMETERS, blockChain);
            peerGroup.setDownloadTxDependencies(false);

            peerGroup.setUserAgent("TestWallet", "0.0.1");

            peerGroup.setMaxConnections(6);
            peerGroup.setConnectTimeoutMillis(15000);
            peerGroup.setPeerDiscoveryTimeoutMillis(10000);

            // start peergroup
            peerGroup.startAsync();
            peerGroup.startBlockChainDownload(blockchainDownloadListener);
        }
    };

    private final Handler delayHandler = new Handler();
    private PeerGroup peerGroup;
    private WakeLock wakeLock;
    private PeerConnectivityListener peerConnectivityListener;
    private NotificationManager nm;
    private Configuration config;
    private Wallet wallet;

    @Override
    public void onCreate()  {
        wallet = new Wallet(Constants.WALLET.NETWORK_PARAMETERS);
        nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        final String lockName = getPackageName() + " blockchain sync";

        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, lockName);
        config = Configuration.getInstance();

        broadcastPeerState(0);

        blockChainFile = Constants.WALLET.BLOCKCHAIN_FILE;
        final boolean blockChainFileExists = blockChainFile.exists();

        if (!blockChainFileExists) {
            new File(Constants.WALLET.WALLET_PATH).mkdirs();
        }
        try {
            blockStore = new SPVBlockStore(Constants.WALLET.NETWORK_PARAMETERS, blockChainFile);
            blockStore.getChainHead(); // detect corruptions as early as possible

            final long earliestKeyCreationTime = wallet.getEarliestKeyCreationTime();

            if (!blockChainFileExists && earliestKeyCreationTime > 0){
                try {
                    config.setDeletingBlockchain(true);
                    final long start = System.currentTimeMillis();
                    final InputStream checkpointsInputStream = getAssets().open("bitcoin/" + Constants.WALLET.CHECKPOINTS_FILENAME);
                    CheckpointManager.checkpoint(Constants.WALLET.NETWORK_PARAMETERS, checkpointsInputStream, blockStore, earliestKeyCreationTime);
                    Log.i(TAG, String.format("checkpoints loaded from '%1$s', took %2$dms", Constants.WALLET.CHECKPOINTS_FILENAME, System.currentTimeMillis() - start));
                } catch (final IOException x) {
                    Log.e(TAG, "problem reading checkpoints, continuing without", x);
                }
            }
        } catch (final BlockStoreException x) {
            blockChainFile.delete();

            final String msg = "blockstore cannot be created";
            Log.e(TAG, msg, x);
        }

        try {
            blockChain = new BlockChain(Constants.WALLET.NETWORK_PARAMETERS, walletHelper.getMainWallet(), blockStore);
        } catch (final BlockStoreException x) {
            throw new Error("blockchain cannot be created", x);
        }

        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
        intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
        registerReceiver(connectivityReceiver, intentFilter); // implicitly start PeerGroup
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        Log.e(TAG, "onStartCommand");
        ...
        return START_NOT_STICKY;
    }

    private void notifyBlockchainProgress(int progress, int max) {
        boolean isCompleted = progress == max;
        if (config.isDeletingBlockchain()) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
            long percent = Math.round(Math.floor((progress * 100) / max));
            mBuilder.setContentTitle("Blockchain download")
                .setContentText(" Synchronization in progress  - " + percent + "%")
                .setSmallIcon(R.drawable.ic_logo_splash)
                .setProgress(max, progress, false)
                .setAutoCancel(false);

            if (isCompleted) {
                mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setProgress(0, 0, false)
                    .setAutoCancel(true)
                    .setContentText(getString(R.string.synchronization_completed));
                config.setDeletingBlockchain(false);
            }

            Notification notif = mBuilder.build();

            if (isCompleted) {
                notif.flags = Notification.FLAG_FOREGROUND_SERVICE;
            } else {
                notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE;
            }

            nm.notify(2, notif);
        }

    }

    public void broadcastBlockchainDownloaded() {
        Intent i = new Intent(UiRefreshReceiver.REFRESH_UI);
        LocalBroadcastManager.getInstance(this).sendBroadcast(i);
    }
}

我使用了android中运行在UI线程中的

服务的官方项目的部分代码。您需要将所有作业放入服务内的不同线程中

您需要使用
IntentService
进行下载过程

public class BitcoinService extends IntentService {
    private final PeerDataEventListener blockchainDownloadListener = new AbstractPeerEventListener() {
        private final long CALLBACK_TIME = 1000L;
        private final AtomicLong lastMessageTime = new AtomicLong(0);

        private int height;
        private int blocksLeft;
        private Block block;

        @Override
        public void onBlocksDownloaded(final Peer peer, final Block block, final FilteredBlock filteredBlock, final int blocksLeft) {
            config.maybeIncrementBestChainHeightEver(blockChain.getChainHead().getHeight());

            delayHandler.removeCallbacksAndMessages(null);

            final long now = System.currentTimeMillis();

            this.block = block;
            this.height = (int) peer.getBestHeight() - blocksLeft;
            this.blocksLeft = blocksLeft;

            if (now - lastMessageTime.get() > CALLBACK_TIME) {
                delayHandler.post(RUNNER);
            } else {
                delayHandler.postDelayed(RUNNER, CALLBACK_TIME);
            }
        }

        private final Runnable RUNNER = new Runnable() {
            @Override
            public void run() {
                notifyBlockchainProgress(height, (height + blocksLeft));
                Log.e(TAG, "LAST_BLOCK=" + height + ", REMAINS=" + blocksLeft);
                if (blocksLeft == 0) {
                    broadcastBlockchainDownloaded();
                }
            }
        };
    };

    private final BroadcastReceiver connectivityReceiver = new BroadcastReceiver() {
        @SuppressLint("Wakelock")
        @Override
        public void onReceive(final Context context, final Intent intent) {
            Log.d(TAG, "acquiring wakelock");
            wakeLock.acquire();

            // consistency check
            final int walletLastBlockSeenHeight = wallet.getLastBlockSeenHeight();
            final int bestChainHeight = blockChain.getBestChainHeight();
            if (walletLastBlockSeenHeight != -1 && walletLastBlockSeenHeight != bestChainHeight) {
                final String message = "wallet/blockchain out of sync: " + walletLastBlockSeenHeight + "/" + bestChainHeight;
                Log.e(TAG, message);
            }

            Log.i(TAG, "starting peergroup");
            peerGroup = new PeerGroup(Constants.WALLET.NETWORK_PARAMETERS, blockChain);
            peerGroup.setDownloadTxDependencies(false);

            peerGroup.setUserAgent("TestWallet", "0.0.1");

            peerGroup.setMaxConnections(6);
            peerGroup.setConnectTimeoutMillis(15000);
            peerGroup.setPeerDiscoveryTimeoutMillis(10000);

            // start peergroup
            peerGroup.startAsync();
            peerGroup.startBlockChainDownload(blockchainDownloadListener);
        }
    };

    private final Handler delayHandler = new Handler();
    private PeerGroup peerGroup;
    private WakeLock wakeLock;
    private PeerConnectivityListener peerConnectivityListener;
    private NotificationManager nm;
    private Configuration config;
    private Wallet wallet;

    @Override
    protected void onHandleIntent(Intent intent) {
        wallet = new Wallet(Constants.WALLET.NETWORK_PARAMETERS);
        nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        final String lockName = getPackageName() + " blockchain sync";

        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, lockName);
        config = Configuration.getInstance();

        broadcastPeerState(0);

        blockChainFile = Constants.WALLET.BLOCKCHAIN_FILE;
        final boolean blockChainFileExists = blockChainFile.exists();

        if (!blockChainFileExists) {
            new File(Constants.WALLET.WALLET_PATH).mkdirs();
        }
        try {
            blockStore = new SPVBlockStore(Constants.WALLET.NETWORK_PARAMETERS, blockChainFile);
            blockStore.getChainHead(); // detect corruptions as early as possible

            final long earliestKeyCreationTime = wallet.getEarliestKeyCreationTime();

            if (!blockChainFileExists && earliestKeyCreationTime > 0){
                try {
                    config.setDeletingBlockchain(true);
                    final long start = System.currentTimeMillis();
                    final InputStream checkpointsInputStream = getAssets().open("bitcoin/" + Constants.WALLET.CHECKPOINTS_FILENAME);
                    CheckpointManager.checkpoint(Constants.WALLET.NETWORK_PARAMETERS, checkpointsInputStream, blockStore, earliestKeyCreationTime);
                    Log.i(TAG, String.format("checkpoints loaded from '%1$s', took %2$dms", Constants.WALLET.CHECKPOINTS_FILENAME, System.currentTimeMillis() - start));
                } catch (final IOException x) {
                    Log.e(TAG, "problem reading checkpoints, continuing without", x);
                }
            }
        } catch (final BlockStoreException x) {
            blockChainFile.delete();

            final String msg = "blockstore cannot be created";
            Log.e(TAG, msg, x);
        }

        try {
            blockChain = new BlockChain(Constants.WALLET.NETWORK_PARAMETERS, walletHelper.getMainWallet(), blockStore);
        } catch (final BlockStoreException x) {
            throw new Error("blockchain cannot be created", x);
        }

        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
        intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
        registerReceiver(connectivityReceiver, intentFilter); // implicitly start PeerGroup
    }

    private void notifyBlockchainProgress(int progress, int max) {
        boolean isCompleted = progress == max;
        if (config.isDeletingBlockchain()) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
            long percent = Math.round(Math.floor((progress * 100) / max));
            mBuilder.setContentTitle("Blockchain download")
                .setContentText(" Synchronization in progress  - " + percent + "%")
                .setSmallIcon(R.drawable.ic_logo_splash)
                .setProgress(max, progress, false)
                .setAutoCancel(false);

            if (isCompleted) {
                mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setProgress(0, 0, false)
                    .setAutoCancel(true)
                    .setContentText(getString(R.string.synchronization_completed));
                config.setDeletingBlockchain(false);
            }

            Notification notif = mBuilder.build();

            if (isCompleted) {
                notif.flags = Notification.FLAG_FOREGROUND_SERVICE;
            } else {
                notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE;
            }

            nm.notify(2, notif);
        }

    }
}

希望这将对您有所帮助。

检查此项:前台服务在UI线程上运行。任何I/O绑定的工作都应该使用AsyncTasks、新线程(将消息发送回处理程序)或(最佳选项)RxJava来完成downloading@IgorGanapolsky,我尝试使用AsyncTask执行此过程,得到了相同的结果。@HirenPatel我已使用IntentService对其进行了测试,但它不起作用。谢谢!但我已经测试过了,它不起作用。我尝试使用AsyncTask来完成这个过程,得到了相同的结果。