5***@qq.com
5***@qq.com
  • 发布:2021-12-31 16:34
  • 更新:2022-01-04 15:07
  • 阅读:1022

用最新sdk里面的Demo,增加胶囊的×号监听和关闭小程序的事件,多次关闭和打开小程序后,胶囊×号的监听事件会失效。

分类:uni小程序sdk

public class MainActivity extends Activity {
Context mContext;
Handler mHandler;
/ unimp小程序实例缓存/
HashMap<String, IUniMP> mUniMPCaches = new HashMap<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
mHandler = new Handler();
setContentView(R.layout.main);
Button button1 = findViewById(R.id.button1);
//用来测试sdkDemo 胶囊×的点击事件,是否生效;
DCUniMPSDK.getInstance().setCapsuleCloseButtonClickCallBack(new IDCUniMPOnCapsuleCloseButtontCallBack() {
@Override
public void closeButtonClicked(String appid) {
Toast.makeText(mContext, "点击×号了", Toast.LENGTH_SHORT).show();
if(mUniMPCaches.containsKey(appid)) {
IUniMP mIUniMP = mUniMPCaches.get(appid);
if (mIUniMP != null && mIUniMP.isRuning()){
mIUniMP.closeUniMP();
mUniMPCaches.remove(appid) ;
}
}
}
});
DCUniMPSDK.getInstance().setUniMPOnCloseCallBack(new IUniMPOnCloseCallBack() {
@Override
public void onClose(String appid) {
Toast.makeText(mContext, appid+"被关闭了", Toast.LENGTH_SHORT).show();
//小程序被关闭需要对实例缓存删除操作
if(mUniMPCaches.containsKey(appid)) {
mUniMPCaches.remove(appid);
}
}
});

    button1.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            try {  
                IUniMP uniMP = DCUniMPSDK.getInstance().openUniMP(mContext,"__UNI__04E3A11", MySplashView.class);  
                mUniMPCaches.put(uniMP.getAppid(), uniMP);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    });  

    Button button2 = findViewById(R.id.button2);  

    button2.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            try {  
                IUniMP uniMP = DCUniMPSDK.getInstance().openUniMP(mContext,"__UNI__04E3A11");  
                mUniMPCaches.put(uniMP.getAppid(), uniMP);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    });  

    Button button3 = findViewById(R.id.button3);  

    button3.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            try {  
                //"pages/tabBar/extUI/extUI" "pages/component/scroll-view/scroll-view"  
                IUniMP uniMP = DCUniMPSDK.getInstance().openUniMP(mContext,"__UNI__04E3A11", "pages/tabBar/extUI/extUI?aaa=123&bbb=456");  
                mUniMPCaches.put(uniMP.getAppid(), uniMP);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    });  

    Button button4 = findViewById(R.id.button4);  

    button4.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            try {  
                final IUniMP uniMP = DCUniMPSDK.getInstance().openUniMP(mContext,"__UNI__04E3A11", "pages/component/view/view");  
                mUniMPCaches.put(uniMP.getAppid(), uniMP);  
                mHandler.postDelayed(new Runnable() {  
                    @Override  
                    public void run() {  
                        Log.e("unimp", "延迟5秒结束 开始关闭当前小程序");  
                        uniMP.closeUniMP();  
                    }  
                }, 5000);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    });  

    Button button5 = findViewById(R.id.button5);  

    button5.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            JSONObject info = DCUniMPSDK.getInstance().getAppVersionInfo("__UNI__04E3A11");  
            if(info != null) {  
                Log.e("unimp", "info==="+info.toString());  
            }  
        }  
    });  

    Button button6 = findViewById(R.id.button6);  
    button6.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            try {  
                IUniMP uniMP = DCUniMPSDK.getInstance().openUniMP(mContext,"__UNI__B61D13B");  
                mUniMPCaches.put(uniMP.getAppid(), uniMP);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    });  

    Button button7 = findViewById(R.id.button7);  
    button7.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            try {  
                IUniMP uniMP = DCUniMPSDK.getInstance().openUniMP(mContext,"__UNI__B61D13B", "pages/sample/send-event");  
                mUniMPCaches.put(uniMP.getAppid(), uniMP);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    });  

    Button button8 = findViewById(R.id.button8);  
    button8.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View v) {  
            try {  
                IUniMP uniMP = DCUniMPSDK.getInstance().openUniMP(mContext,"__UNI__B61D13B", "pages/sample/ext-module");  
                mUniMPCaches.put(uniMP.getAppid(), uniMP);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    });  

    DCUniMPSDK.getInstance().setDefMenuButtonClickCallBack(new IMenuButtonClickCallBack() {  
        @Override  
        public void onClick(String appid, String id) {  
            switch (id) {  
                case "gy":{  
                    Log.e("unimp", "点击了关于" + appid);  
                    //宿主主动触发事件  
                    JSONObject data = new JSONObject();  
                    try {  
                        IUniMP uniMP = mUniMPCaches.get(appid);  
                        if(uniMP != null) {  
                            data.put("sj", "点击了关于");  
                            uniMP.sendUniMPEvent("gy", data);  
                        }  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                    break;  
                }  
                case "hqdqym" :{  
                    IUniMP uniMP = mUniMPCaches.get(appid);  
                    if(uniMP != null) {  
                        Log.e("unimp", "当前页面url=" + uniMP.getCurrentPageUrl());  
                    } else {  
                        Log.e("unimp", "未找到相关小程序实例");  
                    }  
                    break;  
                }  
                case "gotoTestPage" :  
                    Intent intent = new Intent();  
                    intent.setClassName(mContext, "com.example.unimpdemo.TestPageActivity");  
                    DCUniMPSDK.getInstance().startActivityForUniMPTask(appid, intent);  
                    break;  
            }  
        }  
    });  

    DCUniMPSDK.getInstance().setOnUniMPEventCallBack(new IOnUniMPEventCallBack() {  
        @Override  
        public void onUniMPEventReceive(String appid, String event, Object data, DCUniMPJSCallback callback) {  
            Log.i("cs", "onUniMPEventReceive    event="+event);  
            //回传数据给小程序  
            callback.invoke( "收到消息");  
        }  
    });  

    checkPermission();  
}  

/**  
 * 检查并申请权限  
 */  
public void checkPermission() {  
    int targetSdkVersion = 0;  
    String[] PermissionString={Manifest.permission.WRITE_EXTERNAL_STORAGE};  
    try {  
        final PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);  
        targetSdkVersion = info.applicationInfo.targetSdkVersion;//获取应用的Target版本  
    } catch (PackageManager.NameNotFoundException e) {  
        e.printStackTrace();  
    }  

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
        //Build.VERSION.SDK_INT是获取当前手机版本 Build.VERSION_CODES.M为6.0系统  
        //如果系统>=6.0  
        if (targetSdkVersion >= Build.VERSION_CODES.M) {  
            //第 1 步: 检查是否有相应的权限  
            boolean isAllGranted = checkPermissionAllGranted(PermissionString);  
            if (isAllGranted) {  
                Log.e("err","所有权限已经授权!");  
                return;  
            }  
            // 一次请求多个权限, 如果其他有权限是已经授予的将会自动忽略掉  
            ActivityCompat.requestPermissions(this, PermissionString, 1);  
        }  
    }  
}  

/**  
 * 检查是否拥有指定的所有权限  
 */  
private boolean checkPermissionAllGranted(String[] permissions) {  
    for (String permission : permissions) {  
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {  
            // 只要有一个权限没有被授予, 则直接返回 false  
            //Log.e("err","权限"+permission+"没有授权");  
            return false;  
        }  
    }  
    return true;  
}  

@Override  
protected void onDestroy() {  
    super.onDestroy();  
    mUniMPCaches.clear();  
}  

}

2021-12-31 16:34 负责人:DCloud_Android_DQQ 分享
已邀请:
DCloud_Android_DQQ

DCloud_Android_DQQ

你试试最新3.3.5 有没有这个问题

  • 5***@qq.com (作者)

    3.3.5还有这个问题的,需要点击点击胶囊X号多次,就会出现

    2022-01-04 16:19

  • DCloud_Android_DQQ

    回复 5***@qq.com: 我这边没法复现。 能提供更多的复现条件吗。

    2022-01-04 16:26

DCloud_Android_DQQ

DCloud_Android_DQQ

我这边测试3.3.5 没有复现。 还有其他的前置条件吗。 比如机型

该问题目前已经被锁定, 无法添加新回复