版本升級實現代碼 點擊下載安裝 並在通知欄顯示,下載安裝通知

來源:互聯網
上載者:User

版本升級實現代碼 點擊下載安裝 並在通知欄顯示,下載安裝通知

    private void loadNewApp() {        String loadurl = "http://.........";//檔案的網路路徑        final String store = "/sdcard"                + "/apkname" + new Random(127) + UserUtils.getRoundChar(6) + ".apk";//下載的檔案儲存體位置        HttpUtils http = new HttpUtils();//使用xutils下載檔案        http.download(loadurl, store                , true,                new RequestCallBack<File>() {                    @Override                    public void onSuccess(                            ResponseInfo<File> arg0) {                        //取消通知                        mNotificationManager.cancel(1);                        //開啟安裝介面                        Intent intent = new Intent(Intent.ACTION_VIEW);                        File apkfile = new File(store);                        intent.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");                        activity.startActivity(intent);                    }                    @Override                    public void onLoading(long total,                                          long current, boolean isUploading) {                        super.onLoading(total, current,                                isUploading);                        long process = current / total;                        //開啟通知                        setUpNotification((int) process, (int) total);                    }                    @Override                    public void onFailure(HttpException arg0,                                          String arg1) {                        rl_loading.setVisibility(View.INVISIBLE);                    }                });    }<pre name="code" class="java">    private NotificationManager mNotificationManager;    private void setUpNotification(int process, int max) {        mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);        int icon = R.drawable.ic_launcher;        CharSequence tickerText = "...開始下載";        long when = System.currentTimeMillis();        Notification mNotification = new Notification(icon, tickerText, when);        mNotification.flags = Notification.FLAG_ONGOING_EVENT;        RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.download_notification_layout);        contentView.setTextViewText(R.id.name, "...正在下載...");        contentView.setProgressBar(R.id.progressbar, max, process, true);        mNotification.contentView = contentView;//設定點擊通知欄條目 跳轉的activity//        Intent intent = new Intent(activity, NewDianPuActivity.class);//        PendingIntent contentIntent = PendingIntent.getActivity(activity, 0, intent,//                PendingIntent.FLAG_UPDATE_CURRENT);        // mNotification.contentIntent = contentIntent;        mNotificationManager.notify(1, mNotification);    }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.