Android 使用百度LBS SDK(五),2021Android者未来的出路在哪里

break;

case 4:

// textHint.setText(“正在检测SD卡是否有离线地图”);

break;

}

super.handleMessage(msg);

}

};

public class CopyThread implements Runnable {

@Override

public void run() {

for (int i = 0; i < pathFrom.length; i++) {

if (!copyFolder(pathFrom[i], Constant.Path.BAIDU_OFFLINE_SUB)) {

} else {

Message message = new Message();

message.what = 4;

copyHandler.sendMessage(message);

Log.v(Constant.TAG, “Copy Map form SD Success, From:”

  • pathFrom[i]);

break;

}

}

}

}

/**

  • 导入离线地图包数据

*/

private MKOfflineMap mOffline = null;

public void importOfflineMapFromSDCard() {

mOffline = new MKOfflineMap();

mOffline.init(new MyMKOfflineMapListener());

int num = mOffline.importOfflineData();

Log.v(Constant.TAG, “ImportDataThread:Import Baidu Offline Map number:”

  • num);

if (num == 0) {

// 没有导入离线包,可能是离线包放置位置不正确,或离线包已经导入过

} else {

// "成功导入 num 个离线包

}

// new Thread(new ImportDataThread()).start();

}

public class ImportDataThread implements Runnable {

@Override

public void run() {

}

}

class MyMKOfflineMapListener implements MKOfflineMapListener {

@Override

public void onGetOfflineMapState(int type, int state) {

switch (type) {

case MKOfflineMap.TYPE_DOWNLOAD_UPDATE:

MKOLUpdateElement update = mOffline.getUpdateInfo(state);

// 处理下载进度更新提示

if (update != null) {

// stateView.setText(String.format("%s : %d%%",

// update.cityName,

// update.ratio));

// updateView();

}

break;

case MKOfflineMap.TYPE_NEW_OFFLINE:

// 有新离线地图安装

break;

case MKOfflineMap.TYPE_VER_UPDATE:

// 版本更新提示

// MKOLUpdateElement e = mOffline.getUpdateInfo(state);

break;

}

}

}

@Override

protected void onStop() {

isActivityShow = false;

super.onStop();

}

}

5.在线更新离线地图:

Android 使用百度LBS SDK(五),2021Android者未来的出路在哪里_第1张图片

package com.tchip.baidunavi.ui.activity;

import java.util.ArrayList;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.content.Intent;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.KeyEvent;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.BaseAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;

import com.baidu.mapapi.map.offline.MKOLSearchRecord;

import com.baidu.mapapi.map.offline.MKOLUpdateElement;

import com.baidu.mapapi.map.offline.MKOfflineMap;

import com.baidu.mapapi.map.offline.MKOfflineMapListener;

import com.tchip.baidunavi.R;

import com.tchip.baidunavi.util.MyLog;

import com.tchip.baidunavi.util.NetworkUtil;

/**

  • 安装后搜索仍需联网,但会节约达90%的流量

  • 低分屏:L 高分屏:H (854x480 5’ 属于低分屏)

*/

public class OfflineBaiduMapActivity extends Activity implements

MKOfflineMapListener {

private MKOfflineMap mOffline = null;

private TextView cidView;

private TextView stateView;

private EditText cityNameView;

private Button btnSearch;

private RelativeLayout layoutBack, layoutDownload, layoutCity,

layoutImport;

private LinearLayout layoutSingleDownload;

private boolean isSingleDownShow = false;

/**

  • 已下载的离线地图信息列表

*/

private ArrayList localMapList = null;

private LocalMapAdapter lAdapter = null;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_offline_baidumap);

mOffline = new MKOfflineMap();

mOffline.init(this);

initialLayout();

}

private void initialLayout() {

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

btnSearch.setOnClickListener(new MyOnClickListener());

layoutBack = (RelativeLayout) findViewById(R.id.layoutBack);

layoutBack.setOnClickListener(new MyOnClickListener());

layoutDownload = (RelativeLayout) findViewById(R.id.layoutDownload);

layoutDownload.setOnClickListener(new MyOnClickListener());

layoutCity = (RelativeLayout) findViewById(R.id.layoutCity);

layoutCity.setOnClickListener(new MyOnClickListener());

layoutImport = (RelativeLayout) findViewById(R.id.layoutImport);

layoutImport.setOnClickListener(new MyOnClickListener());

layoutSingleDownload = (LinearLayout) findViewById(R.id.layoutSingleDownload);

layoutSingleDownload.setVisibility(View.GONE);

cidView = (TextView) findViewById(R.id.cityid);

cityNameView = (EditText) findViewById(R.id.textCity);

stateView = (TextView) findViewById(R.id.state);

// ListView hotCityList = (ListView) findViewById(R.id.hotcitylist);

// ArrayList hotCities = new ArrayList();

// // 获取热闹城市列表

ArrayList records1 = mOffline.getHotCityList();

// if (records1 != null) {

// for (MKOLSearchRecord r : records1) {

// hotCities.add(r.cityName + “(” + r.cityID + “)” + " --"

// + this.formatDataSize(r.size));

// }

// }

// ListAdapter hAdapter = (ListAdapter) new ArrayAdapter(this,

// android.R.layout.simple_list_item_1, hotCities);

// hotCityList.setAdapter(hAdapter);

ListView allCityList = (ListView) findViewById(R.id.allcitylist);

// 获取所有支持离线地图的城市

final ArrayList allCities = new ArrayList();

ArrayList records2 = mOffline.getOfflineCityList();

if (records1 != null) {

for (MKOLSearchRecord r : records2) {

allCities.add(r.cityName + “(” + r.cityID + “)” + " --"

  • this.formatDataSize(r.size));

}

}

ListAdapter aAdapter = (ListAdapter) new ArrayAdapter(this,

R.layout.offline_map_all_city_list_item, R.id.textAllCityList,

allCities);

allCityList.setAdapter(aAdapter);

allCityList.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView parent, View view,

int position, long id) {

if (NetworkUtil.isNetworkConnected(getApplicationContext())) {

int startIndex = allCities.get(position).indexOf("(") + 1;

int endIndex = allCities.get(position).indexOf(")");

String clickId = allCities.get(position).substring(

startIndex, endIndex);

MyLog.v(“Offline Map id:” + clickId);

mOffline.start(Integer.parseInt(clickId));

Toast.makeText(

getApplicationContext(),

getResources().getString(R.string.start_download)

  • “:”

  • allCities.get(position).substring(0,

startIndex - 1), Toast.LENGTH_SHORT)

.show();

} else {

NetworkUtil.noNetworkHint(getApplicationContext());

}

}

});

LinearLayout cl = (LinearLayout) findViewById(R.id.citylist_layout);

LinearLayout lm = (LinearLayout) findViewById(R.id.localmap_layout);

lm.setVisibility(View.GONE);

cl.setVisibility(View.VISIBLE);

// 获取已下过的离线地图信息

localMapList = mOffline.getAllUpdateInfo();

if (localMapList == null) {

localMapList = new ArrayList();

}

ListView localMapListView = (ListView) findViewById(R.id.localmaplist);

lAdapter = new LocalMapAdapter();

localMapListView.setAdapter(lAdapter);

}

/**

  • 设置下载单个城市离线地图是否可见

  • @param isShow

*/

private void setSingleDownShow(boolean isShow) {

if (!isShow) {

layoutSingleDownload.setVisibility(View.GONE);

isSingleDownShow = false;

} else {

layoutSingleDownload.setVisibility(View.VISIBLE);

isSingleDownShow = true;

}

}

class MyOnClickListener implements View.OnClickListener {

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btnSearch:

// 搜索离线城市

if (NetworkUtil.isNetworkConnected(getApplicationContext())) {

ArrayList records = mOffline

.searchCity(cityNameView.getText().toString());

if (records == null || records.size() != 1) {

Toast.makeText(

getApplicationContext(),

getResources()

.getString(R.string.no_match_city),

Toast.LENGTH_SHORT).show();

return;

}

setSingleDownShow(true);

cidView.setText(String.valueOf(records.get(0).cityID));

} else {

NetworkUtil.noNetworkHint(getApplicationContext());

}

break;

case R.id.layoutBack:

onBackKeyPressed();

break;

case R.id.layoutDownload:

// 下载管理列表

LinearLayout cl = (LinearLayout) findViewById(R.id.citylist_layout);

LinearLayout lm = (LinearLayout) findViewById(R.id.localmap_layout);

lm.setVisibility(View.VISIBLE);

cl.setVisibility(View.GONE);

break;

case R.id.layoutCity:

// 城市列表

LinearLayout cls = (LinearLayout) findViewById(R.id.citylist_layout);

LinearLayout lms = (LinearLayout) findViewById(R.id.localmap_layout);

lms.setVisibility(View.GONE);

cls.setVisibility(View.VISIBLE);

break;

case R.id.layoutImport:

importFromSDCard();

break;

default:

break;

}

}

}

/**

  • 开始下载

  • @param view

*/

public void start(View view) {

if (NetworkUtil.isNetworkConnected(getApplicationContext())) {

Toast.makeText(this,

getResources().getString(R.string.offline_download_start),

Toast.LENGTH_SHORT).show();

startDownloadCityId = Integer

.parseInt(cidView.getText().toString());

new Thread(new StartDownloadThread()).start();

} else {

NetworkUtil.noNetworkHint(getApplicationContext());

}

}

private int startDownloadCityId = 1; // 要下载的城市ID

/**

  • 下载地图线程

*/

private class StartDownloadThread implements Runnable {

@Override

public void run() {

// 启动下载指定城市ID的离线地图,离线地图下载服务仅当wifi连接正常时可用

mOffline.start(startDownloadCityId);

startDownloadCityId = 1; // 重置

Message messageStartDownload = new Message();

messageStartDownload.what = 1;

startDownloadHandler.sendMessage(messageStartDownload);

}

}

final Handler startDownloadHandler = new Handler() {

public void handleMessage(Message msg) {

switch (msg.what) {

case 1:

updateView();

break;

default:

break;

}

}

};

/**

  • 暂停下载

  • @param view

*/

public void stop(View view) {

int cityid = Integer.parseInt(cidView.getText().toString());

mOffline.pause(cityid);

Toast.makeText(this,

getResources().getString(R.string.offline_download_pause),

Toast.LENGTH_SHORT).show();

updateView();

}

/**

  • 删除离线地图

  • @param view

*/

public void deleteMapByCityId(int cityId) {

deleteMapId = cityId;

new Thread(new DeleteMapThread()).start();

Toast.makeText(this,

getResources().getString(R.string.offline_download_delete),

Toast.LENGTH_SHORT).show();

updateView();

}

private int deleteMapId = 1; // 要删除的地图ID

/**

  • 删除地图线程

*/

private class DeleteMapThread implements Runnable {

@Override

public void run() {

mOffline.remove(deleteMapId);

deleteMapId = 1;

}

}

/**

  • 从SD卡导入离线地图安装包

  • 存放位置:USB存储器/BaiduMapSDK/vmp/l/zhongshan_187.dat

*/

public void importFromSDCard() {

new Thread(new ImportOfflineMapThread()).start();

}

private class ImportOfflineMapThread implements Runnable {

@Override

public void run() {

int mapNum = mOffline.importOfflineData();

String msg = “”;

if (mapNum == 0) {

Message messageNoMap = new Message();

messageNoMap.what = 1;

importOfflineMapHandler.sendMessage(messageNoMap);

} else {

Message messageImport = new Message();

messageImport.what = 2;

importOfflineMapHandler.sendMessage(messageImport);

}

}

}

final Handler importOfflineMapHandler = new Handler() {

public void handleMessage(android.os.Message msg) {

switch (msg.what) {

case 1: // 未导入离线地图包

AlertDialog.Builder builder = new Builder(

OfflineBaiduMapActivity.this);

builder.setMessage(getResources().getString(

R.string.not_import_offline_map));

builder.setTitle(getResources().getString(R.string.hint));

builder.setPositiveButton(

getResources().getString(R.string.confirm),

new android.content.DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

dialog.dismiss();

}

});

builder.create().show();

break;

case 2:

AlertDialog.Builder builderImport = new Builder(

OfflineBaiduMapActivity.this);

builderImport.setMessage(getResources().getString(

R.string.import_success_without_num));

builderImport.setTitle(getResources().getString(R.string.hint));

builderImport.setPositiveButton(

getResources().getString(R.string.confirm),

new android.content.DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

dialog.dismiss();

}

});

builderImport.create().show();

updateView();

break;

default:

break;

}

}

};

/**

  • 更新状态显示

*/

public void updateView() {

new Thread(new UpdateViewThread()).start();

}

private class UpdateViewThread implements Runnable {

@Override

public void run() {

localMapList = mOffline.getAllUpdateInfo();

if (localMapList == null) {

localMapList = new ArrayList();

}

Message messageUpdate = new Message();

messageUpdate.what = 1;

updateViewHandler.sendMessage(messageUpdate);

}

}

final Handler updateViewHandler = new Handler() {

public void handleMessage(Message msg) {

switch (msg.what) {

case 1:

lAdapter.notifyDataSetChanged();

break;

default:

break;

}

}

};

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

onBackKeyPressed();

return true;

} else

return super.onKeyDown(keyCode, event);

}

private void onBackKeyPressed() {

if (isSingleDownShow) {

setSingleDownShow(false);

} else {

finish();

}

}

@Override

protected void onPause() {

int cityid = Integer.parseInt(cidView.getText().toString());

MKOLUpdateElement temp = mOffline.getUpdateInfo(cityid);

if (temp != null && temp.status == MKOLUpdateElement.DOWNLOADING) {

mOffline.pause(cityid);

}

super.onPause();

}

@Override

结尾

我还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料,免费分享给大家。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

领取方式:关注+点赞+点击我的GitHub 免费获取!

lAdapter.notifyDataSetChanged();

break;

default:

break;

}

}

};

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

onBackKeyPressed();

return true;

} else

return super.onKeyDown(keyCode, event);

}

private void onBackKeyPressed() {

if (isSingleDownShow) {

setSingleDownShow(false);

} else {

finish();

}

}

@Override

protected void onPause() {

int cityid = Integer.parseInt(cidView.getText().toString());

MKOLUpdateElement temp = mOffline.getUpdateInfo(cityid);

if (temp != null && temp.status == MKOLUpdateElement.DOWNLOADING) {

mOffline.pause(cityid);

}

super.onPause();

}

@Override

结尾

我还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料,免费分享给大家。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

领取方式:关注+点赞+点击我的GitHub 免费获取!

Android 使用百度LBS SDK(五),2021Android者未来的出路在哪里_第2张图片

你可能感兴趣的:(程序员,架构,面试,android)