当前位置:   article > 正文

Android 针对APP做国家地区使用限制,不提供服务弹框提示,无法使用APP_android ip限制印度地区

android ip限制印度地区

在国内总会有一些公司做出一些APP对国家限制的要求,被限制的国家只可以看到弹框提示,无法正常的使用APP,除非翻墙(VPN)使用才可以使用。比如:暂不为一下国家或地区的用户提供服务:中国大陆、古巴、伊朗、朝鲜、克里米亚、苏丹、叙利亚、孟加拉国、玻利维亚、厄瓜多尔、吉尔吉斯斯坦等等。上面的国家或地区使用的时候就会在首页出现弹框提示,无法正常使用,返回则退出APP,使用翻墙软件则可以正常使用APP。

1.判断代码:

  1. @Override
  2. protected void onResume() {
  3. super.onResume();
  4. // MobclickAgent.onPageStart(PageNameConstant.PageName_MainActivity);
  5. MobclickAgent.onResume(this);
  6. OkGo.<IPData>get("http://ip.taobao.com/service/getIpInfo.php")
  7. .params("ip", "myip").headers("User-Agent",
  8. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
  9. .execute(new CommonCallBack<IPData>(IPData.class) {
  10. @Override
  11. public void onSuccess(Response<IPData> response) {
  12. try {
  13. if(response.body().getData().getCountry_id().equalsIgnoreCase("cn")){
  14. ban();
  15. }
  16. }catch (Exception e){
  17. e.printStackTrace();
  18. }
  19. }
  20. @Override
  21. public void onError(Response<IPData> response) {
  22. super.onError(response);
  23. }
  24. });
  25. }

 2.弹框提示:

  1. private void ban() {
  2. if(null == banDidalog){
  3. banDidalog = new SelfDidalog(this, getString(R.string.prompt), R.string.ip_ban, true);
  4. }
  5. if(!banDidalog.isShowing()&&
  6. !URLConstant.getUrlTop().equalsIgnoreCase(URLConstant.URL_TOP_LOACL)){
  7. banDidalog.show();
  8. }
  9. }

3.自定义弹框:

  1. import android.app.Activity;
  2. import android.app.Dialog;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.graphics.Color;
  6. import android.os.Bundle;
  7. import android.text.TextPaint;
  8. import android.view.Gravity;
  9. import android.view.KeyEvent;
  10. import android.view.View;
  11. import android.widget.LinearLayout;
  12. import android.widget.TextView;
  13. import com.tokenloan.lightwallet.R;
  14. import com.tokenloan.lightwallet.app.WalletApplication;
  15. /**
  16. * Created by sgf
  17. * <p>
  18. * 自定义弹话框
  19. */
  20. public class SelfDidalog extends Dialog implements android.view.View.OnClickListener {
  21. private int yesStrs; //设置确定按钮的文字
  22. private int NoStrs; //设置取消按钮的文字
  23. private onYesAndNoOnclickListener yesAndNoOnclickListener;
  24. private int content; //内容
  25. private String title; //内容
  26. private TextView tv_dialgo_title;
  27. private LinearLayout llayout_dialgo_no;
  28. private TextView tv_no;
  29. private LinearLayout llayout_dialgo_yes;
  30. private TextView tv_yes;
  31. private boolean isVisibleCancel;
  32. private boolean isBan;
  33. private boolean isConfirm;
  34. public SelfDidalog(Context context, String title, int content, boolean isVisibleCancel, int noStrs, int yesStrs,
  35. onYesAndNoOnclickListener yesAndNoOnclickListener) {
  36. super(context, R.style.whDialog);
  37. this.title = title;
  38. this.content = content;
  39. this.isVisibleCancel = isVisibleCancel;
  40. this.NoStrs = noStrs;
  41. this.yesStrs = yesStrs;
  42. this.yesAndNoOnclickListener = yesAndNoOnclickListener;
  43. }
  44. public SelfDidalog(Context context, String title, int content, boolean isVisibleCancel, int noStrs, int yesStrs,
  45. onYesAndNoOnclickListener yesAndNoOnclickListener,boolean isConfirm) {
  46. super(context, R.style.whDialog);
  47. this.title = title;
  48. this.content = content;
  49. this.isVisibleCancel = isVisibleCancel;
  50. this.NoStrs = noStrs;
  51. this.yesStrs = yesStrs;
  52. this.yesAndNoOnclickListener = yesAndNoOnclickListener;
  53. this.isConfirm = isConfirm;
  54. }
  55. public SelfDidalog(Context context, String title, int content,boolean isBan) {
  56. super(context, R.style.whDialog);
  57. this.title = title;
  58. this.content = content;
  59. this.isBan = isBan;
  60. }
  61. @Override
  62. protected void onCreate(Bundle savedInstanceState) {
  63. super.onCreate(savedInstanceState);
  64. setContentView(R.layout.dialog_ok_and_no);
  65. initView();
  66. }
  67. /**
  68. * 初始化界面控件
  69. */
  70. private void initView() {
  71. tv_dialgo_title = findViewById(R.id.tv_dialgo_title);
  72. tv_dialgo_title.setText(title);
  73. TextView ctv_dialog_msg = findViewById(R.id.ctv_dialog_msg);
  74. ctv_dialog_msg.setText(content);
  75. llayout_dialgo_no = findViewById(R.id.llayout_dialgo_no);
  76. tv_no = findViewById(R.id.tv_no);
  77. if(NoStrs!=0)
  78. tv_no.setText(NoStrs);
  79. llayout_dialgo_yes = findViewById(R.id.llayout_dialgo_yes);
  80. tv_yes = findViewById(R.id.tv_yes);
  81. if(yesStrs!=0)
  82. tv_yes.setText(yesStrs);
  83. if (isVisibleCancel) {
  84. llayout_dialgo_no.setVisibility(View.VISIBLE);
  85. llayout_dialgo_yes.setVisibility(View.VISIBLE);
  86. } else {//显示一个白色背景带圆角的按钮
  87. llayout_dialgo_yes.setBackgroundResource(R.drawable.bg_white_corner_3);
  88. findViewById(R.id.lineVertical).setVisibility(View.GONE);
  89. // ViewGroup.LayoutParams layoutParams = llayout_dialgo_yes.getLayoutParams();
  90. // layoutParams.height = 48;//设置控件的宽高
  91. // layoutParams.width = 100;
  92. // llayout_dialgo_yes.setLayoutParams(layoutParams);
  93. llayout_dialgo_no.setVisibility(View.GONE);
  94. llayout_dialgo_yes.setVisibility(View.VISIBLE);
  95. }
  96. llayout_dialgo_no.setOnClickListener(this);
  97. llayout_dialgo_yes.setOnClickListener(this);
  98. if(isConfirm){
  99. this.findViewById(R.id.icon).setVisibility(View.GONE);
  100. int color = Color.parseColor("#0a0a0a");
  101. tv_dialgo_title.setTextColor(color);
  102. TextPaint tp = tv_dialgo_title.getPaint();
  103. tp.setFakeBoldText(true);
  104. ctv_dialog_msg.setTextColor(color);
  105. ctv_dialog_msg.setGravity(Gravity.START);
  106. tv_no.setTextColor(Color.parseColor("#888888"));
  107. tv_yes.setTextColor(color);
  108. TextPaint tp1 = tv_yes.getPaint();
  109. tp1.setFakeBoldText(true);
  110. int lineColor = Color.parseColor("#E5E5E5");
  111. findViewById(R.id.lineHorizontal).setBackgroundColor(lineColor);
  112. findViewById(R.id.lineVertical).setBackgroundColor(lineColor);
  113. }else if(isBan){
  114. int color = Color.parseColor("#0a0a0a");
  115. tv_dialgo_title.setTextColor(color);
  116. TextPaint tp = tv_dialgo_title.getPaint();
  117. tp.setFakeBoldText(true);
  118. ctv_dialog_msg.setTextColor(color);
  119. ctv_dialog_msg.setGravity(Gravity.START);
  120. int padding = (int)getContext().getResources().getDimension(R.dimen._10dip);
  121. ctv_dialog_msg.setPadding(0,0,0,padding*2);
  122. findViewById(R.id.buttonsLayout).setVisibility(View.GONE);
  123. setCanceledOnTouchOutside(false);
  124. setCancelable(true);
  125. setOnDismissListener(new OnDismissListener() {
  126. @Override
  127. public void onDismiss(DialogInterface dialog) {
  128. WalletApplication.getInstance().getCurrentTopActivity().finish();
  129. }
  130. });
  131. }
  132. }
  133. @Override
  134. public void onClick(View view) {
  135. switch (view.getId()) {
  136. case R.id.llayout_dialgo_no:
  137. if (yesAndNoOnclickListener != null) {
  138. yesAndNoOnclickListener.onNoClick();
  139. }
  140. dismiss();
  141. break;
  142. case R.id.llayout_dialgo_yes:
  143. if (yesAndNoOnclickListener != null) {
  144. yesAndNoOnclickListener.onYesClick();
  145. }
  146. dismiss();
  147. break;
  148. }
  149. }
  150. /**
  151. * 设置监听事件
  152. */
  153. public interface onYesAndNoOnclickListener {
  154. /**
  155. * 回调确定按钮的方法 处理事件
  156. */
  157. void onYesClick();
  158. /**
  159. * 回调取消按钮的方法 处理事件
  160. */
  161. void onNoClick();
  162. }
  163. }

4.布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:background="@drawable/bg_white_corner">
  6. <LinearLayout
  7. android:layout_width="270dp"
  8. android:layout_height="wrap_content"
  9. android:background="@drawable/bg_white_corner"
  10. android:orientation="vertical"
  11. android:layout_alignParentTop="true"
  12. android:layout_centerHorizontal="true">
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:orientation="vertical"
  16. android:gravity="center"
  17. android:layout_height="wrap_content">
  18. <ImageView
  19. android:id="@+id/icon"
  20. android:layout_width="wrap_content"
  21. android:layout_gravity="center"
  22. android:layout_marginTop="50dp"
  23. android:src="@mipmap/common_icon_hint_wrong"
  24. android:layout_height="wrap_content" />
  25. <TextView
  26. android:id="@+id/tv_dialgo_title"
  27. android:layout_width="match_parent"
  28. android:layout_height="24dp"
  29. android:gravity="center"
  30. android:layout_marginTop="15dp"
  31. android:textColor="#282828"
  32. android:textSize="17sp" />
  33. <View
  34. android:layout_width="match_parent"
  35. android:layout_height="1px"
  36. android:visibility="gone"
  37. android:background="@color/gray_three" />
  38. </LinearLayout>
  39. <TextView
  40. android:id="@+id/ctv_dialog_msg"
  41. android:layout_width="220dp"
  42. android:layout_marginTop="15dp"
  43. android:layout_height="wrap_content"
  44. android:layout_gravity="center"
  45. android:gravity="center"
  46. android:lineSpacingExtra="5dp"
  47. android:textColor="@color/black_one"
  48. android:textSize="15sp" />
  49. <LinearLayout
  50. android:layout_width="match_parent"
  51. android:layout_height="wrap_content"
  52. android:orientation="vertical">
  53. <View
  54. android:layout_width="match_parent"
  55. android:layout_height="1px"
  56. android:visibility="gone"
  57. android:background="@color/gray_three">
  58. </View>
  59. <LinearLayout
  60. android:id="@+id/warnAgain"
  61. android:layout_width="match_parent"
  62. android:visibility="gone"
  63. android:layout_height="wrap_content"
  64. android:orientation="vertical">
  65. <CheckBox
  66. style="@style/Default_text_black_l"
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content"
  69. android:layout_marginBottom="10dp"
  70. android:layout_marginLeft="10dp"
  71. android:layout_marginTop="10dp"
  72. android:button="@drawable/checkbox_background_login"
  73. android:gravity="center"
  74. android:paddingLeft="5dp"
  75. android:text="不再提示"
  76. android:textColor="@color/gray_three" />
  77. <View
  78. android:layout_width="match_parent"
  79. android:layout_height="1px"
  80. android:background="@color/gray_three"/>
  81. </LinearLayout>
  82. <Button
  83. android:id="@+id/btDialgoYes"
  84. android:layout_width="200dp"
  85. android:layout_height="36dp"
  86. android:layout_marginTop="20dp"
  87. android:layout_marginBottom="20dp"
  88. android:layout_gravity="center"
  89. android:visibility="gone"
  90. android:text="@string/btn_coin_apply_success"
  91. android:background="@drawable/bg_jianbianse_contacts"
  92. android:textColor="@color/colorWhite"
  93. android:textSize="18sp" />
  94. <LinearLayout
  95. android:id="@+id/buttonsLayout"
  96. android:layout_width="match_parent"
  97. android:layout_height="wrap_content"
  98. android:layout_marginTop="20dp"
  99. android:orientation="vertical">
  100. <View
  101. android:id="@+id/lineHorizontal"
  102. android:layout_width="match_parent"
  103. android:layout_height="0.5dp"
  104. android:background="@color/gray_three"/>
  105. <LinearLayout
  106. android:layout_width="match_parent"
  107. android:layout_height="40dp"
  108. android:orientation="horizontal">
  109. <LinearLayout
  110. android:id="@+id/llayout_dialgo_no"
  111. android:layout_width="0dp"
  112. android:layout_height="match_parent"
  113. android:layout_weight="1"
  114. android:background="@drawable/bg_white_corner_1"
  115. android:orientation="horizontal"
  116. android:visibility="visible">
  117. <TextView
  118. android:id="@+id/tv_no"
  119. android:layout_width="0dp"
  120. android:layout_height="match_parent"
  121. android:layout_weight="1"
  122. android:gravity="center"
  123. android:text="@string/cancle"
  124. android:textColor="@color/gray_five"
  125. android:textSize="17sp" />
  126. </LinearLayout>
  127. <View
  128. android:id="@+id/lineVertical"
  129. android:layout_width="0.5dp"
  130. android:layout_height="48dp"
  131. android:layout_marginTop="5dp"
  132. android:background="@color/gray_three"/>
  133. <LinearLayout
  134. android:id="@+id/llayout_dialgo_yes"
  135. android:layout_width="0dp"
  136. android:layout_height="match_parent"
  137. android:layout_weight="1"
  138. android:background="@drawable/bg_white_corner_2"
  139. android:orientation="horizontal"
  140. android:visibility="visible">
  141. <TextView
  142. android:id="@+id/tv_yes"
  143. android:layout_width="0dp"
  144. android:layout_height="match_parent"
  145. android:layout_weight="1"
  146. android:gravity="center"
  147. android:text="@string/immediate_authentication"
  148. android:textColor="@color/black_one"
  149. android:textSize="17sp" />
  150. </LinearLayout>
  151. </LinearLayout>
  152. </LinearLayout>
  153. </LinearLayout>
  154. </LinearLayout>
  155. </RelativeLayout>

5.按钮背景:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:bottom="10dp">
  4. <shape android:shape="rectangle">
  5. <!--设置渐变色 android:angle="270从上到下,android:angle="0"从左到右"-->
  6. <gradient
  7. android:angle="0"
  8. android:type="linear"
  9. android:startColor="#02D2B9"
  10. android:endColor="#00B6E0"/>
  11. <!--设置上面两个角的弧度-->
  12. <corners
  13. android:topLeftRadius="8dp"
  14. android:topRightRadius="8dp" />
  15. </shape>
  16. </item>
  17. <item android:top="10dp">
  18. <shape android:shape="rectangle">
  19. <gradient
  20. android:angle="0"
  21. android:type="linear"
  22. android:startColor="#02D2B9"
  23. android:endColor="#00B6E0"/>
  24. <!--设置下面两个角的弧度-->
  25. <corners
  26. android:bottomLeftRadius="8dp"
  27. android:bottomRightRadius="8dp" />
  28. </shape>
  29. </item>
  30. </layer-list>

6.styles:

  1. <!--自定义dialog背景全透明无边框theme -->
  2. <style name="whDialog" parent="android:style/Theme.Dialog">
  3. <!--背景颜色及和透明程度-->
  4. <item name="android:windowBackground">@android:color/transparent</item>
  5. <!--是否去除标题 -->
  6. <item name="android:windowNoTitle">true</item>
  7. <!--是否去除边框-->
  8. <item name="android:windowFrame">@null</item>
  9. <!--是否浮现在activity之上-->
  10. <item name="android:windowIsFloating">true</item>
  11. <!--是否模糊-->
  12. <item name="android:backgroundDimEnabled">true</item>
  13. </style>

7.勾选按钮配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_checked="true" android:drawable="@drawable/common_shopping_redio_sel" />
  4. <item android:state_checked="false" android:drawable="@drawable/common_redio_nor" />
  5. <item android:state_selected="true" android:drawable="@drawable/common_shopping_redio_sel" />
  6. <item android:state_selected="false" android:drawable="@drawable/common_redio_nor" />
  7. </selector>

8.圆角白色背景

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <solid android:color="#ffffff" />
  5. <stroke
  6. android:width="0.5dp"
  7. android:color="#DADADA" />
  8. <corners android:radius="10dp" />
  9. </shape>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/715057
推荐阅读
相关标签
  

闽ICP备14008679号