BuglyDevTeam / Bugly-Android-Demo

Bugly Android SDK 使用例子
Apache License 2.0
812 stars 307 forks source link

替换不了没有最新版本的Toast提示 #274

Closed halolol closed 3 years ago

halolol commented 4 years ago
Beta.upgradeStateListener=new UpgradeStateListener() {
            @Override
            public void onUpgradeFailed(boolean b) {
                new UtilsToast(getApplicationContext(),"更新失败").show(Toast.LENGTH_SHORT);
            }
            @Override
            public void onUpgradeSuccess(boolean b) {
                new UtilsToast(getApplicationContext(),"更新成功").show(Toast.LENGTH_SHORT);
            }
            //没有新版本
            @Override
            public void onUpgradeNoVersion(boolean b) {
                new UtilsToast(getApplicationContext(),"没有最新版本").show(Toast.LENGTH_SHORT);
            }

            @Override
            public void onUpgrading(boolean b) {
                new UtilsToast(getApplicationContext(),"正在检测最新版本...").show(Toast.LENGTH_SHORT);
            }

            @Override
            public void onDownloadCompleted(boolean b) {
            }
        };

监听更新状态,想替换Toast提示,onUpgradeNoVersion这里设置了不起作用

用的版本是

implementation 'com.tencent.bugly:crashreport_upgrade:1.4.5'
 implementation 'com.tencent.bugly:nativecrashreport:3.7.500'
roybill commented 3 years ago

我也遇到了一样的问题,兄弟你是怎么解决的???腾讯的这个文档我也真是服了

halolol commented 3 years ago

我也遇到了一樣的問題,兄弟你是怎麼解決的???騰訊的這個文檔我也真是服了

你试试初始化语句写在监听方法的后面

roybill commented 3 years ago

没有效果啊,我是这样写的: public class BaseApplication extends Application {

private static final String TAG = "BaseApplication";

public static BaseApplication sApp;
public HashMap<String, Object> map = new HashMap<>();
private static BaseApplication application;

public static Context getContext() {
    return sApp.getApplicationContext();
}

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(base);
}

@Override
public void onCreate() {
    super.onCreate();
    sApp = this;
    application = this;
    UserManager.init(this);
    UIUtils.init(this);
    LoadMoreModuleConfig.setDefLoadMoreView(new CustomLoadmoreView());   // 在 Application 中配置全局自定义的 LoadMoreView

// CrashReport.initCrashReport(getApplicationContext(), Constants.BUGLY_APP_ID, false);

// Bugly.init(getApplicationContext(), Constants.BUGLY_APP_ID, false);//bugly初始化,包含异常捕捉和版本更新 // Beta.upgradeDialogLayoutId = R.layout.dialog_version_update; // UpgradeInfo upgradeInfo=Beta.getUpgradeInfo(); // LogUtils.d("1412 "+upgradeInfo.title);

    versionUpdate();//版本更新  (初始化必须在后面,否则不走回调!)

    FileDownloader.setupOnApplicationOnCreate(this)
            .connectionCreator(new FileDownloadUrlConnection
                    .Creator(new FileDownloadUrlConnection.Configuration()
                    .connectTimeout(30000) // 超时时间30秒
                    .readTimeout(30000) // 超时时间30秒
            )).
            commit();//初始化下载器

    initUmeng();//初始化友盟数据

// initAliyun();//阿里云文件存储

    //捕获界面奔溃
    activityOnCrash();

    //初始化腾讯webView
    initX5Web();

    //初始化ErrorPage
    initLoadSir();
}

private void versionUpdate() {
    /*在application中初始化时设置监听,监听策略的收取*/
    Beta.autoCheckUpgrade = true;//true为自动检查升级,false表示不会自动检查升级,需要手动调用Beta.checkUpgrade()方法

// Beta.upgradeCheckPeriod = 1000 60 60 24 1; //弹框提示更新周期(设置为一天弹一次) Beta.upgradeListener = new UpgradeListener() { @Override public void onUpgrade(int ret, UpgradeInfo strategy, boolean isManual, boolean isSilence) { if (strategy != null) { String title = strategy.title; //标题 String apkUrl = strategy.apkUrl; //apk地址 long fileSize = strategy.fileSize; //文件大小 String newFeature = strategy.newFeature; //显示的更新内容 int upgradeType = strategy.upgradeType; //更新类型 (1.非强制 2.强制) int versionCode = strategy.versionCode; //不显示的版本号 String versionName = strategy.versionName; //显示给用户的版本号

// LogUtils.d("1504 title="+title+" apkUrl="+apkUrl+" fileSize="+fileSize+" newFeature="+newFeature+ // " upgradeType="+upgradeType+" versionCode="+versionCode+" versionName="+versionName);

                Intent i = new Intent();
                i.setClass(getApplicationContext(), UpdateActivity.class);//版本更新的activity
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra("versionName", versionName);
                i.putExtra("content", newFeature);
                i.putExtra("apkUrl", apkUrl);
                i.putExtra("upgradeType", upgradeType);
                startActivity(i);
            } else {

// Toast.makeText(BaseApplication.this, "没有更新", Toast.LENGTH_LONG).show(); LogUtils.d("1444 没有更新"); } } };

    /* 设置更新状态回调接口 */
    Beta.upgradeStateListener = new UpgradeStateListener() {
        @Override
        public void onUpgradeSuccess(boolean isManual) {

// Toast.makeText(getApplicationContext(),"UPGRADE_SUCCESS",Toast.LENGTH_SHORT).show(); }

        @Override
        public void onUpgradeFailed(boolean isManual) {

// Toast.makeText(getApplicationContext(),"UPGRADE_FAILED",Toast.LENGTH_SHORT).show(); }

        @Override
        public void onUpgrading(boolean isManual) {

// Toast.makeText(getApplicationContext(),"UPGRADE_CHECKING",Toast.LENGTH_SHORT).show(); }

        @Override
        public void onDownloadCompleted(boolean b) {

        }

        @Override
        public void onUpgradeNoVersion(boolean isManual) {
            LogUtils.d("1606  111111111111");

// Toast.makeText(getApplicationContext(),"UPGRADE_NO_VERSION",Toast.LENGTH_SHORT).show(); } };

    Bugly.init(getApplicationContext(), Constants.BUGLY_APP_ID, false);//bugly初始化,包含异常捕捉和版本更新
}

private void initLoadSir() {
    GLoading.debug(BuildConfig.DEBUG);
    GLoading.initDefault(new SpecialAdapter());
}

private void initX5Web() {
    LogUtils.dTag(TAG, "QbSdk.initX5Environment");
    HashMap<String, Object> map = new HashMap<>();
    map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
    map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
    QbSdk.initTbsSettings(map);
    QbSdk.setDownloadWithoutWifi(true);
    QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
        @Override
        public void onCoreInitFinished() {
            Log.d(TAG, "onCoreInitFinished");
        }

        @Override
        public void onViewInitFinished(boolean initResult) {
            Log.d(TAG, "onViewInitFinished:" + initResult);
        }
    });
}

private void activityOnCrash() {
    //防止项目崩溃,崩溃后打开错误界面
    CaocConfig.Builder.create()
            .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //default: CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM
            .enabled(true)//是否启用CustomActivityOnCrash崩溃拦截机制 必须启用!不然集成这个库干啥???
            .showErrorDetails(false) //是否必须显示包含错误详细信息的按钮 default: true
            .showRestartButton(false) //是否必须显示“重新启动应用程序”按钮或“关闭应用程序”按钮default: true
            .logErrorOnRestart(false) //是否必须重新堆栈堆栈跟踪 default: true
            .trackActivities(true) //是否必须跟踪用户访问的活动及其生命周期调用 default: false
            .minTimeBetweenCrashesMs(2000) //应用程序崩溃之间必须经过的时间 default: 3000
            .restartActivity(MainActivity.class) // 重启的activity
            //.errorActivity(ErrorActivity::class.java) //发生错误跳转的activity
            //.eventListener(null) //允许你指定事件侦听器,以便在库显示错误活动 default: null
            .apply();
}

private void initUmeng() {
    //todo:上线需注释这里,关闭sdk的相关提示

// UMConfigure.setLogEnabled(false); //配置友盟数据 UMConfigure.init(this, Constants.UMENG_APPKEY, Constants.UMENG_CHANNEL, UMConfigure.DEVICE_TYPE_PHONE, Constants.UMENG_SECRET);

    //必须先在项目内的build.gradle内添加依赖    implementation 'com.umeng.analytics:analytics:latest.integration'
    PlatformConfig.setWeixin(Constants.WECHAT_APPID, Constants.WECHAT_SECRET);

    //推送相关
    PushAgent.getInstance(this).onAppStart();        //获取消息推送代理示例
    PushAgent mPushagent = PushAgent.getInstance(this); ///获取消息推送代理示例

    //设置别名流程:  1.在application注册推送,获取并保存deviceToken到本地 →2.在登录成功后判断deviceToken是否存在,如果存在直接设置别名

    // 注册推送服务,每次调用register方法都会回调该接口
    mPushagent.register(new IUmengRegisterCallback() {
        @Override
        public void onSuccess(String deviceToken) {
            //注册成功会返回deviceToken deviceToken是推送消息的唯一标志
            LogUtils.d("1429 1111111 注册成功:deviceToken:=" + deviceToken);
            //alias的绑定是需要获取到deviceToken为前提的,最好是在注册即enable的回调接口中进行alias的绑定,此时可以保证获取到deviceToken;
            SPUtil.putString(getApplicationContext(), "deviceToken", deviceToken);
        }

        @Override
        public void onFailure(String s, String s1) {
            LogUtils.d("1429111111111111 注册失败:-------->  " + "s:" + s + ",s1:" + s1);
        }
    });

    //测试模式

// InAppMessageManager.getInstance(this).setInAppMsgDebugMode(true);

    notificationMessage(mPushagent);//通知栏消息,用于点击后到消息界面

    passthroughMessage(mPushagent);//透传消息,用于静默更新企业状态

}

private void passthroughMessage(PushAgent mPushagent) {
    UmengMessageHandler messageHandler = new UmengMessageHandler() {

        @Override
        public void dealWithCustomMessage(final Context context, final UMessage msg) {
            new Handler(getMainLooper()).post(new Runnable() {

                @Override
                public void run() {
                    LogUtils.d("1429   333333333333  收到透传消息=" + msg.custom + "   alias=  " + msg.alias);
                    boolean isClickOrDismissed = true; // 对于自定义消息,PushSDK默认只统计送达。若开发者需要统计点击和忽略,则需手动调用统计方法。
                    if (isClickOrDismissed) {
                        UTrack.getInstance(getApplicationContext()).trackMsgClick(msg);   //自定义消息的点击统计
                    } else {
                        UTrack.getInstance(getApplicationContext()).trackMsgDismissed(msg);   //自定义消息的忽略统计
                    }
                    boolean login = UserManager.getInstance().isLogin();
                    if (login) {
                        String json = msg.custom;
                        if (json != null && !TextUtils.isEmpty(json)) {
                            LogUtils.d("1458    " + json);
                            UserBean bean = new Gson().fromJson(json, UserBean.class);
                            UserManager.getInstance().updateMultiField(bean);//更新用户数据

// PushBean bean = new Gson().fromJson(json, PushBean.class); // int is_company = bean.getIs_company(); // String company_name = bean.getCompany_name(); // UserManager.getInstance().updateIsCompany(is_company);//更新企业状态 // UserManager.getInstance().updateCompanyName(company_name);//更新企业名称 // Intent intent = new Intent(getApplicationContext(), NoticeActivity.class);//弹框的Activity // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // startActivity(intent); } } else { LogUtils.d("0958 BaseApplication 未登录"); } } }); } }; mPushagent.setMessageHandler(messageHandler); }

private void notificationMessage(PushAgent mPushagent) {
    //通知栏消息,点击状态栏消息的回调,只有自定义行为才会进入dealWithCustomAction()方法。
    UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {

        @Override
        public void dealWithCustomAction(Context context, UMessage msg) {
            boolean login = UserManager.getInstance().isLogin();
            if (login) {
                //获取推送过来的的数据 (后台可以返回json数据)
                LogUtils.d("1108  dealWithCustomAction: " + msg.custom);
                Intent intent = new Intent(getApplicationContext(), MessageActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//此处开启Activity,必须添加flags
                startActivity(intent);
            }
        }
    };
    mPushagent.setNotificationClickHandler(notificationClickHandler);
}

public HashMap<String, Object> getMap() {
    return map;
}

public static BaseApplication getApplication() {
    return application;
}

public void onLowMemory() {
    super.onLowMemory();
    System.gc();
}

}

roybill commented 3 years ago

我主要是想始终不弹出"没有最新版本"这个提示

zhudi commented 3 years ago

现在我已经把bugly的升级功能暂停了 引入了自己的 也是因为这个提示的问题

roybill commented 3 years ago

@zhudi 改成自定义弹窗的形式就正常,我也是醉了 private void versionUpdate() { Beta.autoCheckUpgrade = true;//true为自动检查升级,false表示不会自动检查升级,需要手动调用Beta.checkUpgrade()方法 Beta.upgradeDialogLayoutId = R.layout.dialog_bugly_version_update; //自定义布局 Beta.canShowUpgradeActs.add(MainActivity.class); //Beta.upgradeCheckPeriod = 1000 60 60 24 7; //弹框提示更新周期(设置为7天弹一次) //Beta.upgradeCheckPeriod = 1000 60; //设置升级检查周期为60s(默认检查周期为0s),60s内SDK不重复向后台请求策略); Beta.initDelay = 1000 3;//设置启动延时为1s(默认延时3s),APP启动1s后初始化SDK,避免影响APP启动速度;

    Beta.upgradeDialogLifecycleListener = new UILifecycleListener<UpgradeInfo>() {
        @Override
        public void onCreate(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onCreate");
            // 注:可通过这个回调方式获取布局的控件,如果设置了id,可通过findViewById方式获取,如果设置了tag,可以通过findViewWithTag,具体参考下面例子:
        }

        @Override
        public void onStart(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onStart"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onResume(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onResume"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onPause(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onPause"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onStop(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onStop"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onDestroy(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onDestory"+ GsonUtils.toJson(upgradeInfo));
        }
    };
}
halolol commented 3 years ago

@zhudi 改成自定义弹窗的形式就正常,我也是醉了 private void versionUpdate() { Beta.autoCheckUpgrade = true;//true为自动检查升级,false表示不会自动检查升级,需要手动调用Beta.checkUpgrade()方法 Beta.upgradeDialogLayoutId = R.layout.dialog_bugly_version_update; //自定义布局 Beta.canShowUpgradeActs.add(MainActivity.class); //Beta.upgradeCheckPeriod = 1000 60 60 24 7; //弹框提示更新周期(设置为7天弹一次) //Beta.upgradeCheckPeriod = 1000 60; //设置升级检查周期为60s(默认检查周期为0s),60s内SDK不重复向后台请求策略); Beta.initDelay = 1000 3;//设置启动延时为1s(默认延时3s),APP启动1s后初始化SDK,避免影响APP启动速度;


    Beta.upgradeDialogLifecycleListener = new UILifecycleListener<UpgradeInfo>() {
        @Override
        public void onCreate(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onCreate");
            // 注:可通过这个回调方式获取布局的控件,如果设置了id,可通过findViewById方式获取,如果设置了tag,可以通过findViewWithTag,具体参考下面例子:
        }

        @Override
        public void onStart(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onStart"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onResume(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onResume"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onPause(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onPause"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onStop(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onStop"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onDestroy(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onDestory"+ GsonUtils.toJson(upgradeInfo));
        }
    };
}

好无语我是想应用刚启动自己检查的时候不弹,手动调用 Beta.checkUpgrade();才弹,我手动调用的时候才去实现那个接口,结果就是不走onUpgradeNoVersion(在里面换成我自定义的toast),要是在基activity写就可以回调到。。。要是在基activity写每次启动又都会弹出来。。。。

halolol commented 3 years ago

@zhudi 改成自定义弹窗的形式就正常,我也是醉了 private void versionUpdate() { Beta.autoCheckUpgrade = true;//true为自动检查升级,false表示不会自动检查升级,需要手动调用Beta.checkUpgrade()方法 Beta.upgradeDialogLayoutId = R.layout.dialog_bugly_version_update; //自定义布局 Beta.canShowUpgradeActs.add(MainActivity.class); //Beta.upgradeCheckPeriod = 1000 60 60 24 7; //弹框提示更新周期(设置为7天弹一次) //Beta.upgradeCheckPeriod = 1000 60; //设置升级检查周期为60s(默认检查周期为0s),60s内SDK不重复向后台请求策略); Beta.initDelay = 1000 3;//设置启动延时为1s(默认延时3s),APP启动1s后初始化SDK,避免影响APP启动速度;

    Beta.upgradeDialogLifecycleListener = new UILifecycleListener<UpgradeInfo>() {
        @Override
        public void onCreate(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onCreate");
            // 注:可通过这个回调方式获取布局的控件,如果设置了id,可通过findViewById方式获取,如果设置了tag,可以通过findViewWithTag,具体参考下面例子:
        }

        @Override
        public void onStart(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onStart"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onResume(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onResume"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onPause(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onPause"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onStop(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onStop"+ GsonUtils.toJson(upgradeInfo));
        }

        @Override
        public void onDestroy(Context context, View view, UpgradeInfo upgradeInfo) {
            Log.d(TAG, "onDestory"+ GsonUtils.toJson(upgradeInfo));
        }
    };
}

好无语我是想应用刚启动自己检查的时候不弹,手动调用 Beta.checkUpgrade();才弹,我手动调用的时候才去实现那个接口,结果就是不走onUpgradeNoVersion(在里面换成我自定义的toast),要是在基activity写就可以回调到。。。要是在基activity写每次启动又都会弹出来。。。。

好吧,后面我用一个变量去控制就可以了