安卓大作业-校园lbs系统
文章目录
1.设计要求
本课题主要设计一种基于Android平台的校园LBS应用系统,系统主要的设计目标和功能如下:
1、能定位当前位置并获取、显示、检索当前位置附近的场所信息;
2、实现校园范围的室外和室内导航;
3、支持活动推荐、空教室查询、校园新闻等功能;
具备良好的操作界面,操作简单方便,系统运行稳定。
2.集成的SDK
1.高德地图SDK。
2.小米推送SDK。
3.效果图
4.用到的开源框架
1.brvah适配器:http://www.recyclerview.org/
2.抽屉布局:https://github.com/HeinrichReimer/material-drawer
3.悬浮按钮:https://github.com/Clans/FloatingActionButton
4.afinal框架:https://github.com/yangfuhai/afinal
5.pickerView:https://codechina.csdn.net/mirrors/bigkoo/android-pickerview?utm_source=csdn_github_accelerator
5.核心代码
(1)地图路线规划
/**
* 开始搜索路径规划方案
*/
public void searchRouteResult(int routeType, int mode) {
//判断起点终点
if (mStartPoint == null) {
Toast.makeText(mContext, "起点未设置", Toast.LENGTH_LONG).show();
return;
}
if (mEndPoint == null) {
Toast.makeText(mContext, "终点未设置", Toast.LENGTH_LONG).show();
}
//展示等待进度对话框
showProgressDialog();
//构造路径规划的起点和终点坐标。
final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(mStartPoint, mEndPoint);
// 步行路径规划
if (routeType == ROUTE_TYPE_WALK) {
//此类定义了步行路径的起终点和计算路径的模式。
RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(fromAndTo);
//异步处理:根据指定的参数来计算步行路径。
mRouteSearch.calculateWalkRouteAsyn(query);
}
}
/**
* 步行路线搜索结果方法回调
*/
@Override
public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {
//隐藏进度条
dissmissProgressDialog();
// 清理地图上的所有覆盖物
aMap.clear();
/*
public class AMapExceptionextends java.lang.Exception
服务异常信息类。 具体信息请前往官网错误码对照表进行查看
*/
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
//判断返回结果是否是空的
if (result != null && result.getPaths() != null) {
/*
getPaths()方法:返回步行路径规划方案(List类型)。
*/
if (result.getPaths().size() > 0) {
mWalkRouteResult = result;
/*
获取多种规划路径的第一种方案即可
public class WalkPath extends Path
定义了步行路径规划的一个方案。
*/
final WalkPath walkPath = mWalkRouteResult.getPaths().get(0);
if (walkPath == null) {
return;
}
//新建步行规划绘制图层(自定义):通过传入起点和终点,绘制路线
WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(
this, aMap, walkPath,
mWalkRouteResult.getStartPos(),
mWalkRouteResult.getTargetPos());
//清除绘制的杂物
walkRouteOverlay.removeFromMap();
//添加绘制的步行路线到地图中。
walkRouteOverlay.addToMap();
//将视角调到绘制的矩形
walkRouteOverlay.zoomToSpan();
//底部详细布局可视化
mBottomLayout.setVisibility(View.VISIBLE);
//返回此规划方案的距离,单位米。
int dis = (int) walkPath.getDistance();
int dur = (int) walkPath.getDuration();
//封装好的地图工具 将距离和时间转化为人能看懂的数据
String des = AMapUtil.getFriendlyTime(dur) + "(" + AMapUtil.getFriendlyLength(dis) + ")";
//距离和时间文本框装上数据
mRotueTimeDes.setText(des);
//设置不可见
mRouteDetailDes.setVisibility(View.GONE);
//点击详情,进入详细页面
mBottomLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, WalkRouteDetailActivity.class);
intent.putExtra("walk_path", walkPath);
// intent.putExtra("walk_result", mWalkRouteResult);
startActivity(intent);
}
});
} else if (result != null && result.getPaths() == null) {
//封装好的Toast
ToastUtil.show(mContext, R.string.no_result);
}
} else {
//封装好的Toast
ToastUtil.show(mContext, R.string.no_result);
}
} else {
//用封装好的工具显示错误码信息
ToastUtil.showerror(this.getApplicationContext(), errorCode);
}
}
(2)消息推送—自定义接收广播
/*
两个作用:
1.获取服务器推送的消息
2.获取调用MIPUSHClient方法的返回结果
*/
public class MIPUSH_MessageReceiver extends PushMessageReceiver {
private String mRegId;
private String mTopic;
private String mAlias;
private String mAccount;
private String mStartTime;
private String mEndTime;
private FinalDb db= MainActivity.db;
@Override
//接受服务器推送的透传消息,消息封装在MiPUSHMessage类中
public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
//添加日志
Log.v(TAG,
"onReceivePassThroughMessage is called. " + message.toString());
String log = context.getString(R.string.recv_passthrough_message, message.getContent());
InformationActivity.logList.add(0, getSimpleDate() + " " + log);
//依次判断别名,订阅主题:有别名就是消息推送,有订阅主题则是新闻推送
if (!TextUtils.isEmpty(message.getTopic())) {
mTopic = message.getTopic();
if(mTopic.equals("新闻")){
News news=new News();
news.setNewsName(message.getExtra().get("标题"));
news.setNewsText(message.getExtra().get("副标题"));
news.setContent(message.getContent());
news.setDate(getSimpleDate());
db.save(news);
}
} else if (!TextUtils.isEmpty(message.getAlias())) {
mAlias = message.getAlias();
if(mAlias.equals("韦欣")){
Data data=new Data();
data.setTweetName(message.getExtra().get("标题"));
data.setTweetText(message.getExtra().get("副标题"));
data.setContent(message.getContent());
data.setDate(getSimpleDate());
db.save(data);
}
}
// //记录到日志中
// Message msg = Message.obtain();
// msg.obj = log;
// MyApplication.getHandler().sendMessage(msg);
}
@Override
//接收服务器推送的通知消息,用户点击后触发,消息封装在 MiPushMessage类中。
public void onNotificationMessageClicked(Context context, MiPushMessage message) {
Log.v(TAG,
"onNotificationMessageClicked is called. " + message.toString());
String log = context.getString(R.string.click_notification_message, message.getContent());
InformationActivity.logList.add(0, getSimpleDate() + " " + log);
//依次判断主题,订阅别名
if (!TextUtils.isEmpty(message.getTopic())) {
mTopic = message.getTopic();
if(mTopic.equals("新闻")){
Intent intent=new Intent();
intent.setAction("android.intent.action.news");
//广播里跳转需加上这句,否则报异常 android.util.AndroidRuntimeException
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
} else if (!TextUtils.isEmpty(message.getAlias())) {
mAlias = message.getAlias();
if(mAlias.equals("韦欣")){
Intent intent=new Intent();
intent.setAction("android.intent.action.notification");
//广播里跳转需加上这句,否则报异常 android.util.AndroidRuntimeException
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
// Message msg = Message.obtain();
// //isNotified表示消息是否通过通知栏传给app的。
// // 如果为true,表示消息在通知栏出过通知;
// // 如果为false,表示消息是直接传给app的,没有弹出过通知。
// if (message.isNotified()) {
// msg.obj = log;
// }
// MyApplication.getHandler().sendMessage(msg);
}
@Override
/*接收服务器推送的通知消息,消息到达客户端时触发,还可以接受应用在前台时不弹出通知的通知消息,消息封装在 MiPushMessage类中。
在MIUI上,只有应用处于启动状态,或者自启动白名单中,才可以通过此方法接受到该消息。
*/
public void onNotificationMessageArrived(Context context, MiPushMessage message) {
Log.v(TAG,
"onNotificationMessageArrived is called. " + message.toString());
String log = context.getString(R.string.arrive_notification_message, message.getContent());
InformationActivity.logList.add(0, getSimpleDate() + " " + log);
//依次判断别名,订阅主题:有别名就是消息推送,有订阅主题则是新闻推送
if (!TextUtils.isEmpty(message.getTopic())) {
mTopic = message.getTopic();
if(mTopic.equals("新闻")){
News news=new News();
news.setNewsName(message.getExtra().get("标题"));
news.setNewsText(message.getExtra().get("副标题"));
news.setContent(message.getContent());
news.setDate(getSimpleDate());
db.save(news);
}
} else if (!TextUtils.isEmpty(message.getAlias())) {
mAlias = message.getAlias();
if(mAlias.equals("韦欣")){
Data data=new Data();
data.setTweetName(message.getExtra().get("标题"));
data.setTweetText(message.getExtra().get("副标题"));
data.setContent(message.getContent());
data.setDate(getSimpleDate());
db.save(data);
}
}
// Message msg = Message.obtain();
// msg.obj = log;
// MyApplication.getHandler().sendMessage(msg);
}
@Override
//获取给服务器发送命令的结果,结果封装在MiPushCommandMessage类中。
public void onCommandResult(Context context, MiPushCommandMessage message) {
Log.v(TAG,
"onCommandResult is called. " + message.toString());
String command = message.getCommand();
List<String> arguments = message.getCommandArguments();
String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
String log;
//注册回调
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mRegId = cmdArg1;
log = context.getString(R.string.register_success);
} else {
log = context.getString(R.string.register_fail);
}
//设置别名回调
} else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mAlias = cmdArg1;
log = context.getString(R.string.set_alias_success, mAlias);
} else {
log = context.getString(R.string.set_alias_fail, message.getReason());
}
//设置取消别名
} else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mAlias = cmdArg1;
log = context.getString(R.string.unset_alias_success, mAlias);
} else {
log = context.getString(R.string.unset_alias_fail, message.getReason());
}
//设置账户
} else if (MiPushClient.COMMAND_SET_ACCOUNT.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mAccount = cmdArg1;
log = context.getString(R.string.set_account_success, mAccount);
} else {
log = context.getString(R.string.set_account_fail, message.getReason());
}
//撤销账户
} else if (MiPushClient.COMMAND_UNSET_ACCOUNT.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mAccount = cmdArg1;
log = context.getString(R.string.unset_account_success, mAccount);
} else {
log = context.getString(R.string.unset_account_fail, message.getReason());
}
//设置订阅主题
} else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mTopic = cmdArg1;
log = context.getString(R.string.subscribe_topic_success, mTopic);
} else {
log = context.getString(R.string.subscribe_topic_fail, message.getReason());
}
//取消订阅
} else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mTopic = cmdArg1;
log = context.getString(R.string.unsubscribe_topic_success, mTopic);
} else {
log = context.getString(R.string.unsubscribe_topic_fail, message.getReason());
}
//设置接收时间
} else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mStartTime = cmdArg1;
mEndTime = cmdArg2;
log = context.getString(R.string.set_accept_time_success, mStartTime, mEndTime);
} else {
log = context.getString(R.string.set_accept_time_fail, message.getReason());
}
} else {
log = message.getReason();
}
InformationActivity.logList.add(0, getSimpleDate() + " " + log);
// Message msg = Message.obtain();
// msg.obj = log;
// MyApplication.getHandler().sendMessage(msg);
}
@Override
//获取给服务器发送注册命令的结果,结果封装在MiPushCommandMessage类中。
public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
Log.v(TAG,
"onReceiveRegisterResult is called. " + message.toString());
String command = message.getCommand();
List<String> arguments = message.getCommandArguments();
String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
String log;
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mRegId = cmdArg1;
Log.d(TAG,"注册成功");
log = context.getString(R.string.register_success);
} else {
log = context.getString(R.string.register_fail);
}
} else {
log = message.getReason();
}
// Message msg = Message.obtain();
// msg.obj = log;
// MyApplication.getHandler().sendMessage(msg);
}
@SuppressLint("SimpleDateFormat")
//获取当前时间
private static String getSimpleDate() {
return new SimpleDateFormat("MM-dd hh:mm:ss").format(new Date());
}
}
6.心得
一次珍贵的经验吧,也就是稍微会用别人的sdk和框架而已,并没有核心技术的学习。