應用更新之_UpdateNotifiActivity

apk更新觸發界面

package com.xxx.update;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.xxx.base.SczwApplication;
import com.xxx.sczwdemo.R;
import com.xxx.update.DownloadService.DownloadBinder;
import com.xxx.utils.DimensUtils;

public class UpdateNotifiActivity extends Activity {
    private Button btn_cancel;
    private TextView tv_progress;
    private DownloadBinder binder;
    private boolean isBinded;
    private ProgressBar mProgressBar;
    // 獲取到下載url后,直接復制給MapApp,里面的全局變量
    private String downloadUrl;
    //
    private boolean isDestroy = true;
    private SczwApplication sczwApp;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_activity);
        sczwApp = SczwApplication.getInstance();
        btn_cancel = (Button) findViewById(R.id.cancel);
        tv_progress = (TextView) findViewById(R.id.currentPro);
        mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
        btn_cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                binder.cancel();
                binder.cancelNotification();
                finish();
            }
        });
    }

    ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            isBinded = false;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            binder = (DownloadBinder) service;
            Log.i("yzh","服務啟動!!!");
            // 開始下載
            isBinded = true;
            binder.addCallback(callback);
            binder.start();

        }
    };

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (isDestroy && sczwApp.isDownload()) {
            Intent it = new Intent(UpdateNotifiActivity.this, DownloadService.class);
            startService(it);
            bindService(it, conn, Context.BIND_AUTO_CREATE);
        }
        Log.i("yzh"," notification  onresume");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        super.onNewIntent(intent);
        if (isDestroy && sczwApp.isDownload()) {
            Intent it = new Intent(UpdateNotifiActivity.this, DownloadService.class);
            startService(it);
            bindService(it, conn, Context.BIND_AUTO_CREATE);
        }
        Log.i("yzh"," notification  onNewIntent");
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.i("yzh"," notification  onPause");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        isDestroy = false;
        Log.i("yzh"," notification  onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isBinded) {
            Log.i("yzh"," onDestroy   unbindservice");
            unbindService(conn);
        }
        if (binder != null && binder.isCanceled()) {
            Log.i("yzh"," onDestroy  stopservice");
            Intent it = new Intent(this, DownloadService.class);
            stopService(it);
        }
    }

    private ICallbackResult callback = new ICallbackResult() {

        @Override
        public void OnBackResult(Object result) {
            // TODO Auto-generated method stub
            if ("finish".equals(result)) {
                finish();
                return;
            }
            int i = (Integer) result;
            mProgressBar.setProgress(i);
            mHandler.sendEmptyMessage(i);
        }

    };

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            tv_progress.setText("下載進度 : " + msg.what + "%");
            if(msg.what==1){
                ObjectAnimator.ofFloat(tv_progress, "translationY", DimensUtils.dip2px(20f) ,0.0f).setDuration(500).start();
                ObjectAnimator.ofFloat(tv_progress, "alpha", 0.0f , 1.0f).setDuration(500).start();
                tv_progress.setVisibility(View.VISIBLE);
                
                ObjectAnimator.ofFloat(btn_cancel, "alpha", 0.0f , 1.0f).setDuration(500).start();
                btn_cancel.setVisibility(View.VISIBLE);
            }
        };
    };

    public interface ICallbackResult {
        public void OnBackResult(Object result);
    }
    
}

應用更新相關文章:
應用更新之_DownloadService
應用更新之_update_activity.xml
應用更新之_update_notif_ly.xml
應用更新之_permission

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,841評論 25 708
  • android.permission.EXPAND_STATUS_BAR 允許一個程序擴展收縮在狀態欄,andro...
    pyyy閱讀 3,654評論 1 17
  • 第50篇,用考試和學習的話題來紀念!也是不錯的!記錄畢業后的第一場考試! 自從大學畢業后沒好好地學習過,上研究生大...
    Brena閱讀 229評論 0 0
  • 風 輕輕的吹著 雨 慢慢的下著 你朝我緩緩的走來 我站在原地默默地等你 突然 我們之間出現了裂痕 你不再前行 而我...
    花界簡閱讀 254評論 0 0
  • “我以為Alex那份策劃案他應該過幾天才能交到我手上,沒想到今天就收到了,他的這份勤奮倒是不錯。” “怎么,剛在會...
    Shirley覃閱讀 225評論 0 0