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.imageView1,

// 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>

时间: 2024-08-05 17:33:19

cc_美团之自定义升级通知的相关文章

美团_ccListView选中Item的颜色修改及解决登陆后升级通知再次弹出的bug

实现,在listView Xml中设置 android:listSelector="@color/green" 2. 解决登陆后升级通知再次弹出的bug ①: MineFragment跳转到Login界面使用StartActivityForResult,而不是StartActivity: @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (data != n

屏蔽Win10升级通知方法

对于有系统洁癖的我来说,不喜欢还原和自动升级,我更乐意使用全新安装的方式来装系统! 据说微软也知道这种方式有时候的确很讨人嫌,因此就低调的在美国微软社区中给出了屏蔽这项通知的官方"大法".这个方法适用于Win7/Win8.1用户. 具体方法如下: 1.在运行中输入regedit后,按回车键进入注册表编辑器 2.定位到 HKEY_LOCAL_MACHINE \SOFTWARE\Policies\Microsoft\Windows\Gwx,如果找不到,就手动建立Gwx子健 3.新建一个DW

android 实现自定义状态栏通知(Status Notification)

在android项目的开发中,有时为了实现和用户更好的交互,在通知栏这一小小的旮旯里,我们通常需要将内容丰富起来,这个时候我们就需要去实现自定义的通知栏,例如下面360或者网易的样式: 首先我们要了解的是 自定义布局文件支持的控件类型:Notification的自定义布局是RemoteViews,因此,它仅支持FrameLayout.LinearLayout.RelativeLayout三种布局控件,同时支持AnalogClock.Chronometer.Button.ImageButton.I

【Android】自定义状态栏通知

在项目开发中,我们有时候需要自定义状态栏通知的样式,以下就是自定义状态栏通知的一个案例代码,以此作为一个记录,有需要的童鞋也可以参考一下 状态栏通知布局custom_notification.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" an

自定义状态栏通知

状态栏通知布局 custom_notification.xml <?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=&q

【Win10应用开发】自定义磁贴通知的排版

前面老周用了两篇烂文,向大家介绍了Adaptive磁贴的模板使用.那些XML模板已经很强大了,不过,如果你觉得那些排版还不足以满足需求,不妨试试自己来定义磁贴的内容. 其实,Runtime App支持在后台任务中生成XAML呈现,只要你编写的后台任务类从XamlRenderingBackgroundTask(位于Windows.UI.Xaml.Media.Imaging命名空间)类派生即可.利用这一特性,我们可以在后台生成XAML布局,然后通过RenderTargetBitmap类来呈现XAML

011_自定义mac通知的时长

打开终端(找不到的点击 Mac 屏幕右上角放大镜按钮,Spotlight 搜索 "终端"),粘入下面这行命令,回车就行了.注意最后的 # 替换成你希望通知中心横幅停留的秒数,比如 15.defaults write com.apple.notificationcenterui bannerTime #如果想恢复系统默认参数,同样的步骤,换成下面这行命令就行了:defaults delete com.apple.notificationcenterui bannerTime

【起航计划 026】2015 起航计划 Android APIDemo的魔鬼步伐 25 App-&gt;Notification-&gt;Status Bar 状态栏显示自定义的通知布局,省却声音、震动

这个例子的Icons Only 和 Icons and marquee 没有什么特别好说明的. 而Use Remote views in balloon 介绍了可以自定义在Extended Status bar显示Notification的Layout,Extended Status Bar缺省显示Notification 是一个图标后接文字,对应大多数情况是够用了.但如果有需要也可以使用自定义的Layout在Extented Status bar显示Notification,方法是通过Remo

cc_美团 评论使用GitHut水平条开源项目事项

package com.chencheng.meituan.view; import java.util.ArrayList; import java.util.List; import com.android.volley.VolleyError; import com.chencheng.meituan.util.MeituanConstants; import com.chencheng.model.detail.XComment; import com.chencheng.model.d