您好,登錄后才能下訂單哦!
一.片段代碼:
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
二。一 自定義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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。