本来想着用handlerpostdelay就可以实现,没想到演示后关闭应用居然报错。

后来想到是没有了activity。

((Activity)context).isFinishing()

可以传入context,再强转成activity。判断此acitvity是否存在,之后就可有去放心的弹出了。

通过sharedpreference判断是否购买。

public class MyDialog {
    static SharedPreferences myShare;
    public static boolean isShowing = false;

public static void showAlert(final Context context) {

myShare = context.getSharedPreferences("billingShared", 0x0002);
        if (!myShare.contains("isBilling")) {

Log.d("sharedpre", "sharedpreferce is created!!");
            SharedPreferences isBilling = context.getSharedPreferences(
                    "billingShared", 0x0002);
            Editor editor = isBilling.edit();
            editor.putBoolean("isEnable", true);
            editor.putBoolean("isBilling", false);
            editor.commit();
        }
        boolean isBilling = myShare.getBoolean("isBilling", false);
        boolean isEnable = myShare.getBoolean("isEnable", false);
        if (!isBilling || !isEnable) {
            Log.d("Mytest", "------>>>>>" + ((Activity) context).isFinishing());
            Log.d("Mytest", "------>>>>>" + isShowing);
            Log.d("Mytest", "------>>>>>" + ((Activity) context).toString());

if (!isShowing) {

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

@Override
                    public void run() {
                        if (!((Activity) context).isFinishing()) {
                            Log.d("Mytest", "------>>>>>running");
                            Log.d("Mytest", "------>>>>>"
                                    + ((Activity) context).isFinishing());
                            new AlertDialog.Builder(context)
                                    .setMessage(
                                            "     请支持开发者,乐捐即能正常使用!!                            ")
                                    .setCancelable(false)
                                    .setPositiveButton("乐捐",
                                            new OnClickListener() {

@Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    boolean isBilling = true;
                                                    if (isBilling) {
                                                        Editor editor = myShare
                                                                .edit();
                                                        editor.putBoolean(
                                                                "isBilling",
                                                                isBilling);
                                                        editor.commit();
                                                        Toast.makeText(
                                                                context,
                                                                "乐捐成功。",
                                                                Toast.LENGTH_LONG)
                                                                .show();
                                                        dialog.dismiss();
                                                        isShowing = false;
                                                    } else {
                                                        Toast.makeText(
                                                                context,
                                                                "乐捐未成功",
                                                                Toast.LENGTH_SHORT)
                                                                .show();
                                                    }
                                                }
                                            })
                                    .setNegativeButton("取消",
                                            new OnClickListener() {

@Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    dialog.dismiss();
                                                    dialog.cancel();
                                                    isShowing = false;
                                                    MyDialog.showAlert(context);
                                                }
                                            }).show();
                            isShowing = true;
                        }
                    }
                }, 8000L);

}

}

}

}

Android 定时重复启动弹出窗口。的更多相关文章

  1. Android Demo---实现从底部弹出窗口

    在前面的博文中,小编简单的介绍了如何制作圆角的按钮以及圆角的图片,伴着键盘和手指之间的舞步,迎来新的问题,不知道小伙伴有没有这样的经历,以App为例,点击头像的时候,会从底部弹出一个窗口,有从相册中选 ...

  2. 弹出窗口Session丢失、防止表单重复提交问题

    一.弹出窗口Session丢失问题 弹出窗口Session丢失使用window.showModalDialog进行信息的提示,相当方便,也容易控制外观和布局.但是存在一个严重的问题,就是Session ...

  3. 【Android】百度地图自定义弹出窗口

    我们使用百度地图的时候,点击地图上的Marker,会弹出一个该地点详细信息的窗口,如下左图所示,有时候,我们希望自己定义这个弹出窗口的内容,或者,干脆用自己的数据来构造这样的弹出窗口,但是,在百度地图 ...

  4. (转)弹出窗口lhgDialog API文档

    应用到你的项目 如果您使用独立版本的lhgDialog窗口组件,您只需在页面head中引入lhgcore.lhgdialog.min.js文件,4.1.1+版本做了修改可以和jQuerya库同时引用, ...

  5. JSP弹出窗口和模式对话框

    本文转载于其它blog,在此向本文原创者,致意!    JSP 弹出窗口  一.window.open() 基础知识      1.window.open()支持环境:  JavaScript1.0+ ...

  6. [转]js来弹出窗口的详细说明

    1.警告对话框 <script> alert("警告文字") </script> 2.确认对话框 <script> confirm(" ...

  7. 在HTML网页中设置弹出窗口的办法

    [1.最基本的弹出窗口代码] 其实代码非常简单: <SCRIPT LANGUAGE="javascript"> <!-- window.open ('page.h ...

  8. js弹出窗口总结6种弹窗方法

    注: //关闭,父窗口弹出对话框,子窗口直接关闭 this.Response.Write("<script language=javascript>window.close(); ...

  9. JS弹出窗口代码大全(详细整理)

    1.弹启一个全屏窗口 复制代码代码如下: <html> <body http://www.jb51.net','脚本之家','fullscreen');">; < ...

随机推荐

  1. You're Given a String...

    You're given a string of lower-case Latin letters. Your task is to find the length of its longest su ...

  2. MySQL安装与操作总结

    安装MySQL 添加mysql源 # rpm -Uvh http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm 安装mysq ...

  3. jenkins api 使用

    1.  java <dependency> <groupId>com.offbytwo.jenkins</groupId> <artifactId>je ...

  4. 使用CXF开发简单的Web Service-HelloWorld(二)

    上篇博文我们介绍了Web Service的基本概念,了解它的基本概念之后,我们这篇博文介绍一个开源的WebService框架-Apache CXF,并实现一个HelloWorld实例. 一.开始之前 ...

  5. Oracle修改主键约束

    项目需求,有张表,原有三个联合主键,现在需要再加一个字段进去,而恰恰这个字段可以为空的.去数据库捞了一把,还好数据都不为空: SQL> select count(*) from t_wlf_re ...

  6. json用法常见错误

    Json用法三个常见错误   net.sf.json.JSONException: java.lang.NoSuchMethodException

  7. 小程序scroll-view组件使用时,子元素虽设置样式display:inline-flex;whit-space:nowrap

    小程序scroll-view组件使用时,子元素虽设置样式display:inline-flex;whit-space:nowrap

  8. [转]第一次使用Android Studio时你应该知道的一切配置(三):gradle项目构建

    目录: 1.gradle的概念 2.gradle配置jar包,和libs文件夹导入jar包的区别 3.签名打包: (1)Studio (2)命令行 (3)gradle wrapper的原理 4.Bui ...

  9. Shell教程快速入门

    Shell即是一种命令语言,又是一种程序设计语言,使用者可以通过Shell访问操作系统的内核服务. Shell编程和java.python.C一样,只要一个能编写代码的文本编辑器和一个能解释执行的脚本 ...

  10. CC1,IceBreak,Hello world ,hello toastmaster

    this is my first speech:icebreak in toastmaster,it is precious memory for me,this is first time that ...