安卓基础service

bindService

第一步

Intent intent = new Intent(this, BgDownLoadService.class);
bindService(intent,serviceConnection,BIND_AUTO_CREATE);

第二步

BgDownLoadService loadService;
ServiceConnection serviceConnection = new ServiceConnection(){
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        BgDownLoadService.MyBinder myBinder = (BgDownLoadService.MyBinder) service;
        loadService = myBinder.getService();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
    }
};

service的写法

public class BgDownLoadService extends Service {

    private RemoteViews remoteViews;

    public class MyBinder extends Binder {
        public BgDownLoadService getService() {
            return BgDownLoadService.this;
        }
    }

    private Callback.Cancelable downLoadEvent;
    private Notification downLoadNotification;
    private NotificationManager mNotificationManager;

    int downApk = 151;

    final String ACTION_CANCEL = "ACTIONCANCEL";
    final String ACTION_INSTALL = "ACTIONINSTALL";

    @Override
    public void onCreate() {
        super.onCreate();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_CANCEL);
        intentFilter.addAction(ACTION_INSTALL);
        //动态注册接收者
        registerReceiver(receiver, intentFilter);
        
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notifation_down);
        downLoadNotification = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContent(remoteViews)
                .build();
        mNotificationManager.notify(downApk, downLoadNotification);
    }

    final int handlerProgressChange = 1001;
    int count = 1;

    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case handlerProgressChange:
//                    int progress = Integer.valueOf((int)msg.obj) ;
                    count += 3;
                    if (count < 100) {
                        remoteViews.setProgressBar(R.id.progress, 100, count, false);
                        downLoadNotification.contentView = remoteViews;
                        LogUtil.e("进度--->" + count);
                        Message message = new Message();
                        message.what = handlerProgressChange;
                        mHandler.sendMessageDelayed(message, 100);
                        Intent intent = new Intent();
                        intent.setAction(ACTION_CANCEL);
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 152,
                                intent, PendingIntent.FLAG_UPDATE_CURRENT);
                        remoteViews.setOnClickPendingIntent(R.id.cancel_down, pendingIntent);
                    } else {
                        LogUtil.e("进度--->" + count);
                        remoteViews.setProgressBar(R.id.progress, 100, 100, false);
                        remoteViews.setTextViewText(R.id.cancel_down, "下载完成");
                        downLoadNotification.contentView = remoteViews;
                        Intent intent = new Intent();
                        intent.setAction(ACTION_INSTALL);
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 152,
                                intent, PendingIntent.FLAG_UPDATE_CURRENT);
                        remoteViews.setOnClickPendingIntent(R.id.cancel_down, pendingIntent);
                        ToastUtils.show("下载完成,进入安装");
                        MyUtils.installApk(BgDownLoadService.this,apkPathUrl);
                    }
                    mNotificationManager.notify(downApk, downLoadNotification);
                    return true;
            }
            return false;
        }
    });
	//用这玩意接收点击事件
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            LogUtil.e("接到的ACTION"+action);
            switch (action) {
                case ACTION_CANCEL:
                    mHandler.removeMessages(handlerProgressChange);
                    downLoadEvent.cancel();
                    mNotificationManager.cancel(downApk);
                    ToastUtils.show("下载已取消");
                    break;
                case ACTION_INSTALL:
                    MyUtils.installApk(BgDownLoadService.this,apkPathUrl);
                    break;
            }
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        downLoadAPk();
        Message message = new Message();
        message.what = handlerProgressChange;
        mHandler.sendMessage(message);
        return new MyBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        downLoadEvent.cancel();
        return super.onUnbind(intent);
    }
    
    String apkPathUrl = Environment.getExternalStorageDirectory().getPath() + "/XXX.apk";
    private void downLoadAPk() {}
}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页

打赏作者

且白

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值