admin管理员组文章数量:1597416
在国内总会有一些公司做出一些APP对国家限制的要求,被限制的国家只可以看到弹框提示,无法正常的使用APP,除非翻墙(VPN)使用才可以使用。比如:暂不为一下国家或地区的用户提供服务:中国大陆、古巴、伊朗、朝鲜、克里米亚、苏丹、叙利亚、孟加拉国、玻利维亚、厄瓜多尔、吉尔吉斯斯坦等等。上面的国家或地区使用的时候就会在首页出现弹框提示,无法正常使用,返回则退出APP,使用翻墙软件则可以正常使用APP。
1.判断代码:
@Override
protected void onResume() {
super.onResume();
// MobclickAgent.onPageStart(PageNameConstant.PageName_MainActivity);
MobclickAgent.onResume(this);
OkGo.<IPData>get("http://ip.taobao/service/getIpInfo.php")
.params("ip", "myip").headers("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
.execute(new CommonCallBack<IPData>(IPData.class) {
@Override
public void onSuccess(Response<IPData> response) {
try {
if(response.body().getData().getCountry_id().equalsIgnoreCase("cn")){
ban();
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onError(Response<IPData> response) {
super.onError(response);
}
});
}
2.弹框提示:
private void ban() {
if(null == banDidalog){
banDidalog = new SelfDidalog(this, getString(R.string.prompt), R.string.ip_ban, true);
}
if(!banDidalog.isShowing()&&
!URLConstant.getUrlTop().equalsIgnoreCase(URLConstant.URL_TOP_LOACL)){
banDidalog.show();
}
}
3.自定义弹框:
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.tokenloan.lightwallet.R;
import com.tokenloan.lightwallet.app.WalletApplication;
/**
* Created by sgf
* <p>
* 自定义弹话框
*/
public class SelfDidalog extends Dialog implements android.view.View.OnClickListener {
private int yesStrs; //设置确定按钮的文字
private int NoStrs; //设置取消按钮的文字
private onYesAndNoOnclickListener yesAndNoOnclickListener;
private int content; //内容
private String title; //内容
private TextView tv_dialgo_title;
private LinearLayout llayout_dialgo_no;
private TextView tv_no;
private LinearLayout llayout_dialgo_yes;
private TextView tv_yes;
private boolean isVisibleCancel;
private boolean isBan;
private boolean isConfirm;
public SelfDidalog(Context context, String title, int content, boolean isVisibleCancel, int noStrs, int yesStrs,
onYesAndNoOnclickListener yesAndNoOnclickListener) {
super(context, R.style.whDialog);
this.title = title;
this.content = content;
this.isVisibleCancel = isVisibleCancel;
this.NoStrs = noStrs;
this.yesStrs = yesStrs;
this.yesAndNoOnclickListener = yesAndNoOnclickListener;
}
public SelfDidalog(Context context, String title, int content, boolean isVisibleCancel, int noStrs, int yesStrs,
onYesAndNoOnclickListener yesAndNoOnclickListener,boolean isConfirm) {
super(context, R.style.whDialog);
this.title = title;
this.content = content;
this.isVisibleCancel = isVisibleCancel;
this.NoStrs = noStrs;
this.yesStrs = yesStrs;
this.yesAndNoOnclickListener = yesAndNoOnclickListener;
this.isConfirm = isConfirm;
}
public SelfDidalog(Context context, String title, int content,boolean isBan) {
super(context, R.style.whDialog);
this.title = title;
this.content = content;
this.isBan = isBan;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_ok_and_no);
initView();
}
/**
* 初始化界面控件
*/
private void initView() {
tv_dialgo_title = findViewById(R.id.tv_dialgo_title);
tv_dialgo_title.setText(title);
TextView ctv_dialog_msg = findViewById(R.id.ctv_dialog_msg);
ctv_dialog_msg.setText(content);
llayout_dialgo_no = findViewById(R.id.llayout_dialgo_no);
tv_no = findViewById(R.id.tv_no);
if(NoStrs!=0)
tv_no.setText(NoStrs);
llayout_dialgo_yes = findViewById(R.id.llayout_dialgo_yes);
tv_yes = findViewById(R.id.tv_yes);
if(yesStrs!=0)
tv_yes.setText(yesStrs);
if (isVisibleCancel) {
llayout_dialgo_no.setVisibility(View.VISIBLE);
llayout_dialgo_yes.setVisibility(View.VISIBLE);
} else {//显示一个白色背景带圆角的按钮
llayout_dialgo_yes.setBackgroundResource(R.drawable.bg_white_corner_3);
findViewById(R.id.lineVertical).setVisibility(View.GONE);
// ViewGroup.LayoutParams layoutParams = llayout_dialgo_yes.getLayoutParams();
// layoutParams.height = 48;//设置控件的宽高
// layoutParams.width = 100;
// llayout_dialgo_yes.setLayoutParams(layoutParams);
llayout_dialgo_no.setVisibility(View.GONE);
llayout_dialgo_yes.setVisibility(View.VISIBLE);
}
llayout_dialgo_no.setOnClickListener(this);
llayout_dialgo_yes.setOnClickListener(this);
if(isConfirm){
this.findViewById(R.id.icon).setVisibility(View.GONE);
int color = Color.parseColor("#0a0a0a");
tv_dialgo_title.setTextColor(color);
TextPaint tp = tv_dialgo_title.getPaint();
tp.setFakeBoldText(true);
ctv_dialog_msg.setTextColor(color);
ctv_dialog_msg.setGravity(Gravity.START);
tv_no.setTextColor(Color.parseColor("#888888"));
tv_yes.setTextColor(color);
TextPaint tp1 = tv_yes.getPaint();
tp1.setFakeBoldText(true);
int lineColor = Color.parseColor("#E5E5E5");
findViewById(R.id.lineHorizontal).setBackgroundColor(lineColor);
findViewById(R.id.lineVertical).setBackgroundColor(lineColor);
}else if(isBan){
int color = Color.parseColor("#0a0a0a");
tv_dialgo_title.setTextColor(color);
TextPaint tp = tv_dialgo_title.getPaint();
tp.setFakeBoldText(true);
ctv_dialog_msg.setTextColor(color);
ctv_dialog_msg.setGravity(Gravity.START);
int padding = (int)getContext().getResources().getDimension(R.dimen._10dip);
ctv_dialog_msg.setPadding(0,0,0,padding*2);
findViewById(R.id.buttonsLayout).setVisibility(View.GONE);
setCanceledOnTouchOutside(false);
setCancelable(true);
setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
WalletApplication.getInstance().getCurrentTopActivity().finish();
}
});
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.llayout_dialgo_no:
if (yesAndNoOnclickListener != null) {
yesAndNoOnclickListener.onNoClick();
}
dismiss();
break;
case R.id.llayout_dialgo_yes:
if (yesAndNoOnclickListener != null) {
yesAndNoOnclickListener.onYesClick();
}
dismiss();
break;
}
}
/**
* 设置监听事件
*/
public interface onYesAndNoOnclickListener {
/**
* 回调确定按钮的方法 处理事件
*/
void onYesClick();
/**
* 回调取消按钮的方法 处理事件
*/
void onNoClick();
}
}
4.布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_white_corner">
<LinearLayout
android:layout_width="270dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_white_corner"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:src="@mipmap/common_icon_hint_wrong"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_dialgo_title"
android:layout_width="match_parent"
android:layout_height="24dp"
android:gravity="center"
android:layout_marginTop="15dp"
android:textColor="#282828"
android:textSize="17sp" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:visibility="gone"
android:background="@color/gray_three" />
</LinearLayout>
<TextView
android:id="@+id/ctv_dialog_msg"
android:layout_width="220dp"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:lineSpacingExtra="5dp"
android:textColor="@color/black_one"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:visibility="gone"
android:background="@color/gray_three">
</View>
<LinearLayout
android:id="@+id/warnAgain"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
style="@style/Default_text_black_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:button="@drawable/checkbox_background_login"
android:gravity="center"
android:paddingLeft="5dp"
android:text="不再提示"
android:textColor="@color/gray_three" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/gray_three"/>
</LinearLayout>
<Button
android:id="@+id/btDialgoYes"
android:layout_width="200dp"
android:layout_height="36dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center"
android:visibility="gone"
android:text="@string/btn_coin_apply_success"
android:background="@drawable/bg_jianbianse_contacts"
android:textColor="@color/colorWhite"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/buttonsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<View
android:id="@+id/lineHorizontal"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/gray_three"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/llayout_dialgo_no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_white_corner_1"
android:orientation="horizontal"
android:visibility="visible">
<TextView
android:id="@+id/tv_no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/cancle"
android:textColor="@color/gray_five"
android:textSize="17sp" />
</LinearLayout>
<View
android:id="@+id/lineVertical"
android:layout_width="0.5dp"
android:layout_height="48dp"
android:layout_marginTop="5dp"
android:background="@color/gray_three"/>
<LinearLayout
android:id="@+id/llayout_dialgo_yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_white_corner_2"
android:orientation="horizontal"
android:visibility="visible">
<TextView
android:id="@+id/tv_yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/immediate_authentication"
android:textColor="@color/black_one"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
5.按钮背景:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android/apk/res/android">
<item android:bottom="10dp">
<shape android:shape="rectangle">
<!--设置渐变色 android:angle="270从上到下,android:angle="0"从左到右"-->
<gradient
android:angle="0"
android:type="linear"
android:startColor="#02D2B9"
android:endColor="#00B6E0"/>
<!--设置上面两个角的弧度-->
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
</shape>
</item>
<item android:top="10dp">
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:type="linear"
android:startColor="#02D2B9"
android:endColor="#00B6E0"/>
<!--设置下面两个角的弧度-->
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp" />
</shape>
</item>
</layer-list>
6.styles:
<!--自定义dialog背景全透明无边框theme -->
<style name="whDialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">true</item>
</style>
7.勾选按钮配置
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/common_shopping_redio_sel" />
<item android:state_checked="false" android:drawable="@drawable/common_redio_nor" />
<item android:state_selected="true" android:drawable="@drawable/common_shopping_redio_sel" />
<item android:state_selected="false" android:drawable="@drawable/common_redio_nor" />
</selector>
8.圆角白色背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
<stroke
android:width="0.5dp"
android:color="#DADADA" />
<corners android:radius="10dp" />
</shape>
版权声明:本文标题:Android 针对APP做国家地区使用限制,不提供服务弹框提示,无法使用APP 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728281329a1151839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论