“MSDK的激活和绑定已经弃用,现在只需要完成MSDK注册就可以使用MSDK的功能”
一、概述
这边文章主要讲一下MSDK激活和绑定示例代码跟读,初步了解SDK的绑定登录等功能实现原理即状态监听。
代码链接:
https://www.github.com/DJI-Mobile-SDK-Tutorials/Android-ImportAndActivateSDKInAndroidStudio
文章链接:https://developer.dji.com/mobile-sdk/documentation/android-tutorials/ActivationAndBinding.html
二、代码
安装SDK代码:MApplication.java
注册App和连接飞机代码:ConnectionActivity.java,DemoApplication.java
安装SDK,注册,连接飞机等相关代码和上篇MSDK -- Android-ImportAndActivateSDKInAndroidStudio代码跟读文章过程基本类似,这边不再做重复解读。
主代码: MainActivity.java
实现了UI界面初始化,账号登录登出功能,登录状态检查,绑定状态检查。
UI界面初始化:
使用了R.layout.activity_main布局初始化activity界面:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initData();
}
对登录登出按钮组件进行绑定,激活和绑定信息状态的text的显示组件绑定:
private void initUI(){
bindingStateTV = (TextView) findViewById(R.id.tv_binding_state_info);
appActivationStateTV = (TextView) findViewById(R.id.tv_activation_state_info);
loginBtn = (Button) findViewById(R.id.btn_login);
logoutBtn = (Button) findViewById(R.id.btn_logout);
loginBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
}
登入登出按钮事件绑定:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:{
loginAccount();
break;
}
case R.id.btn_logout:{
logoutAccount();
break;
}
default:
break;
}
}
数据的状态监听,实现了app激活状态,绑定状态的MSDK代码层面的回调显示并实时进行回调刷新:
1.App激活状态监听AppActivationStateListener,并通过状态变更回调实时更新显示到text中
appActivationStateTV.setText("" + appActivationState);
2.飞机绑定状态监听回调:AircraftBindingStateListener,并通过状态变更回调实时更新显示到text中
bindingStateTV.setText("" + bindingState);
private void setUpListener() {
// Example of Listener
activationStateListener = new AppActivationState.AppActivationStateListener() {
@Override
public void onUpdate(final AppActivationState appActivationState) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
appActivationStateTV.setText("" + appActivationState);
}
});
}
};
bindingStateListener = new AircraftBindingState.AircraftBindingStateListener() {
@Override
public void onUpdate(final AircraftBindingState bindingState) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
bindingStateTV.setText("" + bindingState);
}
});
}
};
}
private void initData(){
setUpListener();
appActivationManager = DJISDKManager.getInstance().getAppActivationManager();
if (appActivationManager != null) {
appActivationManager.addAppActivationStateListener(activationStateListener);
appActivationManager.addAircraftBindingStateListener(bindingStateListener);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
appActivationStateTV.setText("" + appActivationManager.getAppActivationState());
bindingStateTV.setText("" + appActivationManager.getAircraftBindingState());
}
});
}
}
DJI账号的登入登出功能:
登入:loginAccount
private void loginAccount(){
UserAccountManager.getInstance().logIntoDJIUserAccount(this,
new CommonCallbacks.CompletionCallbackWith<UserAccountState>() {
@Override
public void onSuccess(final UserAccountState userAccountState) {
showToast("Login Success");
}
@Override
public void onFailure(DJIError error) {
showToast("Login Error:"
+ error.getDescription());
}
});
}
登出:logoutAccount
private void logoutAccount(){
UserAccountManager.getInstance().logoutOfDJIUserAccount(new CommonCallbacks.CompletionCallback() {
@Override
public void onResult(DJIError error) {
if (null == error) {
showToast("Logout Success");
} else {
showToast("Logout Error:"
+ error.getDescription());
}
}
});
}
}
评论
0 条评论
请登录写评论。