public class Model {
private Context mContext;
private ExecutorService globalThreadPool = Executors.newCachedThreadPool();
private static Model model = new Model();
private Model() {
}
// 获得单例对象
public static Model getInstance() {
return model;
}
// 初始化
public void init(Context context) {
mContext = context;
}
// 获取全局线程池
public ExecutorService getGlobalThreadPool() {
return globalThreadPool;
}
}
public class MApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化数据模型层类
Model.getInstance().init(this);
}
}
// 子线程注册环信
Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
// 注册
EMClient.getInstance().createAccount(registName, registpwd);
// 提示注册成功
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
}
});
} catch (final HyphenateException e) {
e.printStackTrace();
// 提示注册失败
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, "注册失败" + e.toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
});