中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

cc_美團之自定義升級通知

發布時間:2020-07-17 08:59:13 來源:網絡 閱讀:402 作者:愚人cc 欄目:移動開發

cc_美團之自定義升級通知


一.片段代碼:

package com.chencheng.meituan.fragment;


import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.SocketTimeoutException;

import java.net.URL;


import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

import android.net.Uri;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Environment;

import android.support.v4.app.DialogFragment;

import android.support.v4.app.NotificationCompat;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.view.Window;

import android.widget.RemoteViews;

import android.widget.TextView;


import com.chencheng.utils.ToastUtil;

import com.chencheng.utils.upgrade.VersionInfo;

import com.yuchen.meituan.MainActivity;

import com.yuchen.meituan.R;

/**

 * A simple {@link android.support.v4.app.Fragment} subclass.

 * 

 */

public class UpgradeDialogFragment extends DialogFragment implements

OnClickListener {


private static final String APK_UPGRADE = Environment

.getExternalStorageDirectory() + "/lashou/upgrade/upgrade.apk";


private static final int NOTIFY_ID = 12345;

private NotificationManager mNotifyMgr;

private RemoteViews views;

private Notification nofity;

// private FragmentActivity mActivity;

private VersionInfo verInfo;

private TextView mTvContent;


private MainActivity mActivity;


@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

mActivity = (MainActivity) getActivity();

View inflate = inflater.inflate(R.layout.fragment_dialog_upgrade, null);

mTvContent = (TextView) inflate.findViewById(R.id.tv_content);

inflate.findViewById(R.id.tv_cancle).setOnClickListener(this);

inflate.findViewById(R.id.tv_ok).setOnClickListener(this);

getDialog().getWindow().setBackgroundDrawable(

new ColorDrawable(Color.TRANSPARENT));

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

mTvContent.setText(verInfo.featrue);

return inflate;

}


@Override

public void onClick(View v) {

dismiss();

switch (v.getId()) {

case R.id.tv_ok:

ToastUtil.showToast(mActivity, "確定");

// NotifiUtils.showToast(mActivity, "確定");

break;

default:

break;

}

}


class UpgradeTask extends AsyncTask<String, Integer, String> {

private static final int TIME_OUT = 25000;


@Override

protected void onPreExecute() {

showNotify();

}


protected String doInBackground(String... params) {

InputStream is = null;

FileOutputStream fos = null;

try {

URL url = new URL(params[0]);

HttpURLConnection conn = (HttpURLConnection) url

.openConnection();

// conn.getContentLength()// 注意,虛擬地址不能使用這個方法獲得文件大小

// 為http請求設置超時時間

conn.setConnectTimeout(TIME_OUT);

conn.setReadTimeout(TIME_OUT);

if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {

ToastUtil.showToast(mActivity, "服務端正在維護");

// NotifiUtils.showToast(mActivity, "服務端正在維護");

return null;

}

is = conn.getInputStream();

// conn.getContentLength()

File upgradeApk = new File(APK_UPGRADE);

if (!upgradeApk.exists()) {

upgradeApk.getParentFile().mkdirs();

}

fos = new FileOutputStream(upgradeApk);

byte[] buffer = new byte[1024];

int loaded = 0;

int len = 0;

int times = 0;

while (-1 != (len = is.read(buffer))) {

fos.write(buffer, 0, len);

// 每7%更新一次

loaded += len;

int percent = loaded * 100 / verInfo.file_len;

if (percent >= 7 * times) {

times++;

publishProgress(loaded);

}

}

fos.flush();

} catch (SocketTimeoutException e) {

// 處理超時事件

} catch (MalformedURLException e) {

} catch (IOException e) {

} finally {

if (is != null) {

try {

is.close();

} catch (IOException e) {

}

}

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

}

}

}

return null;

}


@Override

protected void onProgressUpdate(Integer... values) {

updateNotify(values[0]);

}


protected void onPostExecute(String result) {

// 提示安裝apk

ToastUtil.showToast(mActivity, "下載完成,請點擊通知安裝");

// NotifiUtils.showToast(mActivity, "下載完成,請點擊通知安裝");

finishNotify();

}

}


protected void upgrade() {

UpgradeTask task = new UpgradeTask();

task.execute(verInfo.apk_url);

}


public void setVersionInfo(VersionInfo verInfo) {

this.verInfo = verInfo;

}


private void showNotify() {

mNotifyMgr = (NotificationManager) getActivity().getSystemService(

Context.NOTIFICATION_SERVICE);


Intent intent = new Intent();

PendingIntent contentIntent = PendingIntent.getActivity(getActivity(),

0, intent, 0);

views = new RemoteViews(getActivity().getPackageName(),

R.layout.custom_notify);

views.setTextViewText(R.id.textView1, "準備下載");

views.setProgressBar(R.id.progressBar1, 10, 0, false);

nofity = new NotificationCompat.Builder(getActivity())

.setSmallIcon(R.drawable.ic_action_home)

.setTicker("美一次")

.setWhen(System.currentTimeMillis())

// .setContentTitle("contentTitle")

// .setContentText("contentText")

.setAutoCancel(true).setContent(views)

.setContentIntent(contentIntent).build();

mNotifyMgr.notify(NOTIFY_ID, nofity);


}


private void updateNotify(int currLen) {

views.setTextViewText(R.id.textView1, currLen * 100 / verInfo.file_len

+ "%");

views.setProgressBar(R.id.progressBar1, verInfo.file_len, currLen,

false);

mNotifyMgr.notify(NOTIFY_ID, nofity);

}


private void finishNotify() {

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(new File(APK_UPGRADE)),

"application/vnd.android.package-archive");

PendingIntent contentIntent = PendingIntent.getActivity(mActivity, 0,

intent, 0);

nofity.contentIntent = contentIntent;

views.setTextViewText(R.id.textView1, "下載完成,點擊更新");

// views.setImageViewResource(R.id.p_w_picpathView1,

// android.R.drawable.ic_media_next);

views.setViewVisibility(R.id.progressBar1, View.INVISIBLE);

mNotifyMgr.notify(NOTIFY_ID, nofity);

}

}



一。二布局代碼:


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >


    <RelativeLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginBottom="200dp"

        android:layout_marginLeft="20dp"

        android:layout_marginRight="20dp"

        android:layout_marginTop="50dp"

        android:background="@drawable/bg_upgrade_dialog" >


        <RelativeLayout

            android:id="@+id/layout_top"

            android:layout_width="match_parent"

            android:layout_height="50dp" >


            <TextView

                android:id="@+id/tv_title"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_centerHorizontal="true"

                android:layout_centerVertical="true"

                android:text="檢測到新版本"

                android:textSize="16sp" />

        </RelativeLayout>


        <TextView

            android:id="@+id/textView1"

            android:layout_width="match_parent"

            android:layout_height="1dp"

            android:layout_alignParentLeft="true"

            android:layout_below="@+id/layout_top"

            android:background="@color/bg_common_gray" />


        <TextView

            android:id="@+id/tv_content"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_above="@+id/TextView01"

            android:layout_alignParentLeft="true"

            android:layout_below="@+id/textView1"

            android:layout_marginBottom="10dp"

            android:layout_marginLeft="20dp"

            android:layout_marginRight="20dp"

            android:layout_marginTop="10dp"

            android:text="Medium Text"

            android:textSize="16sp" />


        <LinearLayout

            android:id="@+id/layout_bottom"

            android:layout_width="match_parent"

            android:layout_height="50dp"

            android:layout_alignParentBottom="true"

            android:layout_centerHorizontal="true"

            android:orientation="horizontal" >


            <TextView

                android:id="@+id/tv_cancle"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:layout_weight="1"

                android:gravity="center"

                android:text="忽略"

                android:textColor="@color/textColor_5b"

                android:textSize="18sp" />


            <TextView

                android:id="@+id/TextView02"

                android:layout_width="1dp"

                android:layout_height="match_parent"

                android:background="@color/bg_common_gray" />


            <TextView

                android:id="@+id/tv_ok"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:layout_weight="1"

                android:gravity="center"

                android:text="立即升級"

                android:textColor="@color/green"

                android:textSize="18sp" />

        </LinearLayout>


        <TextView

            android:id="@+id/TextView01"

            android:layout_width="match_parent"

            android:layout_height="1dp"

            android:layout_above="@+id/layout_bottom"

            android:layout_alignParentLeft="true"

            android:background="@color/bg_common_gray" />

    </RelativeLayout>


二。自定義Toast 


cc_美團之自定義升級通知


二。一 自定義Toast類代碼:


package com.chencheng.utils;


import android.content.Context;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;


import com.yuchen.meituan.R;



public class ToastUtil {

private static Toast toast;


public static void showToast(Context context, String text) {

// Toast toast = Toast.makeText(this, "toast", Toast.LENGTH_LONG);

// 如果之前有任務,就取消

if (toast != null) {

toast.cancel();

}

toast = new Toast(context);

View view = LayoutInflater.from(context).inflate(R.layout.toast_custom,

null);

TextView tvText = (TextView) view.findViewById(R.id.tv_content);

tvText.setText(text);

// toast.setBackground();

toast.setView(view);

toast.setGravity(Gravity.BOTTOM, 0, 100);

toast.setDuration(Toast.LENGTH_SHORT);

toast.show();

}


private ToastUtil() {

}

}

二.二


布局文件


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >


    <RelativeLayout

        android:id="@+id/layout_bg"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:padding="5dp"

        android:background="@color/green">


        <TextView

            android:id="@+id/tv_content"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerHorizontal="true"

            android:layout_centerVertical="true"

            android:text="Medium Text"

            android:textColor="#ffffff"

            android:textSize="15sp" />

    </RelativeLayout>


</RelativeLayout>


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

喜德县| 南丹县| 闽侯县| 西峡县| 兴山县| 东宁县| 宽城| 昭觉县| 高青县| 任丘市| 汨罗市| 桦甸市| 洛扎县| 遵化市| 浦城县| 陆良县| 宜兰市| 克什克腾旗| 郎溪县| 恩平市| 万年县| 渝北区| 老河口市| 渝中区| 望都县| 枞阳县| 安庆市| 正镶白旗| 安龙县| 东平县| 清流县| 凉山| 水城县| 池州市| 精河县| 康马县| 八宿县| 友谊县| 额敏县| 遂川县| 兴业县|