Android 天气APP(三十一)每日提醒弹窗

}

@Override

public void onFailed() {

if (getView() != null) {

getView().getDataFailed();

}

}

});

}

/**

  • 获取必应每日一图返回

  • @param response BiYingImgResponse

*/

void getBiYingResult(Response response);

增加位置如下:

在这里插入图片描述

然后进入SplashActivity,重写getBiYingResult方法,代码如下:

/**

  • 必应壁纸数据返回

  • @param response BiYingImgResponse

*/

@Override

public void getBiYingResult(Response response) {

if (response.body().getImages() != null) {

//得到的图片地址是没有前缀的,所以加上前缀否则显示不出来

String biyingUrl = “http://cn.bing.com” + response.body().getImages().get(0).getUrl();

SPUtils.putString(Constant.EVERYDAY_TIP_IMG,biyingUrl,context);

} else {

ToastUtils.showShortToast(context, “未获取到必应的图片”);

}

}

这里你会发现Constant.EVERYDAY_TIP_IMG,没有这个属性值,那么就到Constant中去创建。

/**

  • 每日提示弹窗的背景图

*/

public static final String EVERYDAY_TIP_IMG = “everydayTipImg”;

/**

  • 每日提示弹窗是否弹出

*/

public static final String EVERYDAY_POP_BOOLEAN = “everydayPopBoolean”;

在Constant里面增加这两个系统变量,注释已经说明了这两个变量的用途了。

下面在layout中新建一个dialog_everyday_tip.xml。里面的代码如下:

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

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

xmlns:app=“http://schemas.android.com/apk/res-auto”

android:layout_width=“@dimen/dp_270”

android:layout_height=“wrap_content”

android:orientation=“vertical”>

<com.google.android.material.imageview.ShapeableImageView

android:id=“@+id/iv_dialog_bg”

android:layout_width=“match_parent”

android:layout_height=“420dp”

android:foreground=“@drawable/shape_dialog_foreground_bg_12”

android:scaleType=“fitXY”

android:src=“@drawable/img_5”

app:shapeAppearanceOverlay=“@style/roundedImageStyle” />

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“420dp”

android:padding=“@dimen/dp_12”>

<TextView

android:id=“@+id/tv_week”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“星期四”

android:textColor=“@color/white”

android:textSize=“@dimen/sp_20”

android:textStyle=“bold” />

<TextView

android:id=“@+id/tv_temperature”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/tv_week”

android:text=“温度”

android:textColor=“@color/white”

android:textSize=“@dimen/sp_48” />

<TextView

android:id=“@+id/tv_weather_state”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/tv_temperature”

android:text=“天气”

android:textColor=“@color/white”

android:textSize=“@dimen/sp_20”

android:typeface=“monospace” />

<ImageView

android:id=“@+id/iv_weather_state”

android:layout_width=“@dimen/dp_80”

android:layout_height=“@dimen/dp_80”

android:layout_alignParentRight=“true”

android:src=“@mipmap/icon_100” />

<TextView

android:id=“@+id/tv_precipitation”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/iv_weather_state”

android:layout_alignParentRight=“true”

android:layout_marginTop=“@dimen/dp_10”

android:drawableLeft=“@mipmap/icon_weather_prec”

android:drawablePadding=“4dp”

android:text=“降水预告”

android:textColor=“@color/white”

android:textSize=“@dimen/sp_12” />

<TextView

android:id=“@+id/tv_temp_difference”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/tv_weather_state”

android:layout_marginTop=“@dimen/dp_100”

android:text=“温差提示”

android:textColor=“@color/white”

android:textSize=“@dimen/sp_18”

android:typeface=“monospace” />

<com.google.android.material.checkbox.MaterialCheckBox

android:id=“@+id/cb_no_pop_up”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentBottom=“true”

android:layout_centerHorizontal=“true”

android:text=“不再弹出”

android:textSize=“@dimen/sp_16”

app:useMaterialThemeColors=“true”

app:buttonTint=“@color/gray”

android:textColor=“@color/gray” />

<View

android:layout_centerHorizontal=“true”

android:layout_below=“@+id/iv_dialog_bg”

android:background=“@color/white”

android:layout_width=“@dimen/dp_1”

android:layout_height=“@dimen/dp_12”/>

<ImageView

android:id=“@+id/iv_close”

android:layout_width=“@dimen/dp_24”

android:layout_height=“@dimen/dp_24”

android:layout_below=“@+id/iv_dialog_bg”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“@dimen/dp_12”

android:src=“@mipmap/icon_close_dialog” />

预览图如下所示(里面的图标没有的话可以去我的源码里面下载,或者自行下载一个,因为是白色的所示我贴了也看不见,CSDN中,不开会员的人无法修改文章的主题颜色,免费的主题,改不了博文的颜色,这一点我觉得很坑,非要你开个会员,吃相太难看了)

在这里插入图片描述

这里面用到了一个style,在app的styles.xml中增加如下代码:

在drawable下新建一个shape_dialog_foreground_bg_12.xml文件,代码如下:

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

在app的colors.xml中新增一个颜色

#20000000

布局有了,那么就是先改变背景,再增加数据。

二、每天第一次弹窗


下面进入到MainActivity中,将检查版本更新的方法移动一个位置

在这里插入图片描述

因为自动更新的弹窗也是在每日第一次才弹出,所以公用,不过也要修改一下checkAppVersion里面的逻辑才行。修改后代码如下:

/**

  • 检查APP版本

*/

private void checkAppVersion() {

AppVersion appVersion = LitePal.find(AppVersion.class, 1);//读取第一条数据

Log.d(“appVersion”, new Gson().toJson(appVersion.getVersionShort()));

if (AppStartUpUtils.isTodayFirstStartApp(context)) {//今天第一次打开APP

if (!appVersion.getVersionShort().equals(APKVersionInfoUtils.getVerName(context))) {//提示更新

//更新提示弹窗

showUpdateAppDialog(appVersion.getInstall_url(), appVersion.getChangelog());

}

//设置每日提示弹窗

setTipDialog();

}

}

之前是判断可不可以更新,再判断是否为第一次,现在判断是否为第一次打开。

然后下面的重点就是这个setTipDialog方法了

/**

  • 设置每日弹窗

*/

private void setTipDialog() {

boolean isShow = SPUtils.getBoolean(Constant.EVERYDAY_POP_BOOLEAN, true, context);

if (isShow) {

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

//当所有数据加载完成之后显示弹窗

if (everyDayTipDialog != null) {

return;

}

//弹出每日提醒弹窗

showEveryDayTipDialog();

}

},1000);

}

}

这里用到那么那个系统变量,判断是否可以弹窗这个弹窗,然后延时弹出。

三、弹出每日提示弹窗


/**

  • 每日提示弹窗

*/

private void showEveryDayTipDialog() {

AlertDialog.Builder builder = new AlertDialog.Builder(context)

.addDefaultAnimation()//默认弹窗动画

.setCancelable(false)

.setText(R.id.tv_week, DateUtils.getWeekOfDate(new Date()))//星期

.setText(R.id.tv_weather_state, dialogWeatherState)//天气状态

.setText(R.id.tv_precipitation, dialogPrecipitation)//降水预告

.setText(R.id.tv_temp_difference,

WeatherUtil.differenceTempTip(dialogTempHeight, dialogTempLow))//温差提示信息

.setContentView(R.layout.dialog_everyday_tip)//载入布局文件

.setWidthAndHeight(SizeUtils.dp2px(context, 270), ViewGroup.LayoutParams.WRAP_CONTENT)//设置弹窗宽高

.setOnClickListener(R.id.iv_close, v -> {//关闭

everyDayTipDialog.dismiss();

});

everyDayTipDialog = builder.create();

String imgUrl = SPUtils.getString(Constant.EVERYDAY_TIP_IMG, “”, context);

ShapeableImageView bg = everyDayTipDialog.getView(R.id.iv_dialog_bg);

Glide.with(context).load(imgUrl).into(bg);

//温度

Typeface typeface = Typeface.createFromAsset(getAssets(), “fonts/Roboto-Light.ttf”);

TextView temp = everyDayTipDialog.getView(R.id.tv_temperature);

temp.setTypeface(typeface);

temp.setText(dialogTemp + “℃”);

//设置天气状态图标

ImageView weatherStateIcon = everyDayTipDialog.getView(R.id.iv_weather_state);

WeatherUtil.changeIcon(weatherStateIcon, dialogWeatherStateCode);

//不再弹出

MaterialCheckBox cb = everyDayTipDialog.getView(R.id.cb_no_pop_up);

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {

SPUtils.putBoolean(Constant.EVERYDAY_POP_BOOLEAN, false, context);

} else {

SPUtils.putBoolean(Constant.EVERYDAY_POP_BOOLEAN, true, context);

}

}

});

everyDayTipDialog.show();

}

这里面的代码就是先显示一些要的数据,通过缓存拿到必应的url设置背景,然后在弹窗的底部有一个选中框,选中后再关闭这个弹窗,那么这个弹窗以后都不会再弹出了,除非你再应用设置中进行打开。

到这一步,弹窗就出现了。

四、弹窗的开关


既然是增加用户的体验,那么自然要让用户可以自行控制,于是,我在新增了一个应用设置页面。在ui包下新建一个Empty Activity。命名为SettingActivity。它的xml布局如下:

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

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:orientation=“vertical”

android:fitsSystemWindows=“true”

android:background=“@color/gray_white”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“.ui.SettingActivity”>

<androidx.appcompat.widget.Toolbar

android:id=“@+id/toolbar”

android:layout_width=“match_parent”

android:layout_height=“?attr/actionBarSize”

android:background=“@color/white”

app:contentInsetLeft=“@dimen/dp_16”

android:elevation=“@dimen/dp_10”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_scrollFlags=“scroll|enterAlways”

app:navigationIcon=“@mipmap/icon_return”

app:popupTheme=“@style/ThemeOverlay.AppCompat.Light”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:text=“应用设置”

android:textColor=“@color/black”

android:textSize=“@dimen/sp_18” />

</androidx.appcompat.widget.Toolbar>

<LinearLayout

android:layout_marginTop=“@dimen/dp_12”

android:background=“@color/white”

android:paddingLeft=“@dimen/dp_16”

android:paddingRight=“@dimen/dp_16”

android:paddingTop=“@dimen/dp_8”

android:paddingBottom=“@dimen/dp_8”

android:gravity=“center_vertical”

android:orientation=“horizontal”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

<TextView
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

《960全网最全Android开发笔记》

《379页Android开发面试宝典》

《507页Android开发相关源码解析》

因为文件太多,全部展示会影响篇幅,暂时就先列举这些部分截图

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

13750958892)]

[外链图片转存中…(img-B6HTpBet-1713750958893)]

[外链图片转存中…(img-PRnzThXV-1713750958894)]

[外链图片转存中…(img-4hjaWsr0-1713750958895)]

[外链图片转存中…(img-ZBQk4epa-1713750958895)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

[外链图片转存中…(img-34qMzP5D-1713750958896)]

《960全网最全Android开发笔记》

[外链图片转存中…(img-53Cq1N93-1713750958897)]

《379页Android开发面试宝典》

[外链图片转存中…(img-3u681jdN-1713750958898)]

《507页Android开发相关源码解析》

[外链图片转存中…(img-yuNJvWji-1713750958899)]

因为文件太多,全部展示会影响篇幅,暂时就先列举这些部分截图

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值