代码改变世界

Android 定时重复启动弹出窗口。

本来想着用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);

            }

        }

    }

}

 

posted on 2014-02-19 20:36  Captain林  阅读(826)  评论(0编辑  收藏  举报

导航