第一篇:android百度地图api实现短信接收定位
工程文件结构:
demoApplication.java package com.gy.mymaps;
import android.app.Application;import android.content.Context;import android.widget.Toast;
import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.MKGeneralListener;import com.baidu.mapapi.map.MKEvent;
public class DemoApplication extends Application {
private static DemoApplication mInstance = null;
public boolean m_bKeyRight = true;
BMapManager mBMapManager = null;
@Override
public void onCreate(){
super.onCreate();
mInstance = this;
initEngineManager(this);}
public void initEngineManager(Context context){
if(mBMapManager == null){
mBMapManager = new BMapManager(context);
}
if(!mBMapManager.init(new MyGeneralListener())){
Toast.makeText(DemoApplication.getInstance().getApplicationContext(),“BMapManager初始化错误!”, Toast.LENGTH_LONG).show();
} }
public static DemoApplication getInstance(){
return mInstance;}
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
static class MyGeneralListener implements MKGeneralListener {
public void onGetNetworkState(int iError){
if(iError == MKEvent.ERROR_NETWORK_CONNECT){
Toast.makeText(DemoApplication.getInstance().getApplicationContext(), “您的网络出错啦!”,Toast.LENGTH_LONG).show();
}
else if(iError == MKEvent.ERROR_NETWORK_DATA){
Toast.makeText(DemoApplication.getInstance().getApplicationContext(), “输入正确的检索条件!”,Toast.LENGTH_LONG).show();
}
//...}
public void onGetPermissionState(int iError){
//非零值表示key验证未通过
if(iError!= 0){
//鎺堟潈Key閿欒锛�
Toast.makeText(DemoApplication.getInstance().getApplicationContext(),“请输入正确的授权Key,并检查您的网络连接是否正常!error: ”+iError, Toast.LENGTH_LONG).show();
DemoApplication.getInstance().m_bKeyRight = false;
}
else{
DemoApplication.getInstance().m_bKeyRight = true;
Toast.makeText(DemoApplication.getInstance().getApplicationContext(),“key认证成功”, Toast.LENGTH_LONG).show();
}
}
} } Mainactivity.java: package com.gy.mymaps;
import java.util.List;
import android.annotation.SuppressLint;import android.app.Activity;import android.content.ContentValues;import android.database.ContentObserver;import android.database.Cursor;import android.graphics.drawable.Drawable;import android.net.Uri;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.telephony.SmsManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;
import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.map.ItemizedOverlay;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.map.OverlayItem;import com.baidu.mapapi.search.MKAddrInfo;import com.baidu.mapapi.search.MKBusLineResult;import com.baidu.mapapi.search.MKDrivingRouteResult;import com.baidu.mapapi.search.MKPoiResult;import com.baidu.mapapi.search.MKSearch;import com.baidu.mapapi.search.MKSearchListener;import com.baidu.mapapi.search.MKShareUrlResult;import com.baidu.mapapi.search.MKSuggestionResult;import com.baidu.mapapi.search.MKTransitRouteResult;import com.baidu.mapapi.search.MKWalkingRouteResult;import com.baidu.platform.comapi.basestruct.GeoPoint;//import com.gy.msmsend.R;public class MainActivity extends Activity { //UI相关
Button mBtnReverseGeoCode = null;// 将坐标反编码为地址
Button mBtnGeoCode = null;// 将地址编码为坐标
Button msmSend =null;//短信发送按钮
//地图相关
MapView mMapView = null;// 地图View
//搜索相关
MKSearch mSearch = null;// 搜索模块,也可去掉地图模块独立使用
EditText phoneText;
String[] as;
String city=null;
String pos=null;
Drawable marker;
int png=0;
//
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/*
* 注册短信变化事件监听
*/
SmsContent content = new SmsContent(new Handler());
//注册短信变化监听
this.getContentResolver().registerContentObserver(Uri.parse(“content://sms/”), true, content);
//注册短信变化监听
/**
* 使用地图sdk前需先初始化BMapManager.* BMapManager是全局的,可为多个MapView共用,它需要地图模块创建前创建,* 并在地图地图模块销毁后销毁,只要还有地图模块在使用,BMapManager就不应该销毁
*/
DemoApplication app =(DemoApplication)this.getApplication();
if(app.mBMapManager == null){
app.mBMapManager = new BMapManager(getApplicationContext());
/**
* 如果BMapManager没有初始化则初始化BMapManager
*/
app.mBMapManager.init(new DemoApplication.MyGeneralListener());
}
setContentView(R.layout.activity_main);
CharSequence titleLable=“我的位置”;
setTitle(titleLable);
//地图初始化
mMapView =(MapView)findViewById(R.id.bmapView);
mMapView.getController().enableClick(true);
mMapView.getController().setZoom(12);
// 初始化搜索模块,注册事件监听
mSearch = new MKSearch();
mSearch.init(app.mBMapManager, new MKSearchListener(){
public void onGetPoiDetailSearchResult(int type, int error){
}
public void onGetAddrResult(MKAddrInfo res, int error){
if(error!= 0){
String str = String.format(“错误号:%d”, error);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
return;
}
//地图移动到该点
mMapView.getController().animateTo(res.geoPt);
if(res.type == MKAddrInfo.MK_GEOCODE){
//地理编码:通过地址检索坐标点
String strInfo = String.format(“纬度:%f 经度:%f”, res.geoPt.getLatitudeE6()/1e6, res.geoPt.getLongitudeE6()/1e6);
Toast.makeText(MainActivity.this, strInfo, Toast.LENGTH_LONG).show();
}
if(res.type == MKAddrInfo.MK_REVERSEGEOCODE){
//反地理编码:通过坐标点检索详细地址及周边poi
String strInfo = res.strAddr;
Toast.makeText(MainActivity.this, strInfo, Toast.LENGTH_LONG).show();
EditText editGeoCodeKey =(EditText)findViewById(R.id.geocodekey);
//设置城市
editGeoCodeKey.setText(strInfo);
}
//生成ItemizedOverlay图层用来标注结果点
ItemizedOverlay
//生成Item
OverlayItem item = new OverlayItem(res.geoPt, “", null);
//为maker定义位置和边界
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
//给item设置marker
item.setMarker(marker);
//在图层上添加item
itemOverlay.addItem(item);
//清除地图其他图层
mMapView.getOverlays().clear();
//添加一个标注ItemizedOverlay图层
mMapView.getOverlays().add(itemOverlay);
//执行刷新使生效
mMapView.refresh();
}
public void onGetPoiResult(MKPoiResult res, int type, int error){
}
public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error){
}
public void onGetTransitRouteResult(MKTransitRouteResult res, int error){
}
public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error){
}
public void onGetBusDetailResult(MKBusLineResult result, int iError){
}
public void onGetSuggestionResult(MKSuggestionResult res, int arg1){
}
public void onGetShareUrlResult(MKShareUrlResult result, int type,int error){
// TODO Auto-generated method stub
}
});
// 设定地理编码及反地理编码按钮的响应
mBtnReverseGeoCode =(Button)findViewById(R.id.reversegeocode);
mBtnGeoCode =(Button)findViewById(R.id.geocode);
msmSend =(Button)findViewById(R.id.sendButton);
OnClickListener clickListener = new OnClickListener(){
public void onClick(View v){
SearchButtonProcess(v);
}
};
mBtnReverseGeoCode.setOnClickListener(clickListener);
mBtnGeoCode.setOnClickListener(clickListener);
msmSend.setOnClickListener(clickListener);
} /**
* 发起搜索
* @param v
*/ void SearchButtonProcess(View v){
if(mBtnReverseGeoCode.equals(v)){
EditText lat =(EditText)findViewById(R.id.lat);
EditText lon =(EditText)findViewById(R.id.lon);
//得到需要标在地图上的资源
marker = getResources().getDrawable(R.drawable.ic_mark0);
GeoPoint ptCenter = GeoPoint((int)(Float.valueOf(lat.getText().toString())*1e6),(int)(Float.valueOf(lon.getText().toString())*1e6));
//反Geo搜索
mSearch.reverseGeocode(ptCenter);
} else if(mBtnGeoCode.equals(v)){
new
EditText editCity =(EditText)findViewById(R.id.city);
EditText editGeoCodeKey =(EditText)findViewById(R.id.geocodekey);
//得到需要标在地图上的资源
marker = getResources().getDrawable(R.drawable.ic_mark0);
//Geo搜索
mSearch.geocode(editGeoCodeKey.getText().toString(),editCity.getText().toString());
}else if(msmSend.equals(v)){
phoneText =(EditText)findViewById(R.id.tel);
//发送短信
sendMessage();
} }
@Override
protected void onPause(){
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume(){
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy(){
mMapView.destroy();
mSearch.destory();
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
mMapView.onRestoreInstanceState(savedInstanceState);
}
/**
*
*/
private void sendMessage(){
// 获取文本框中的内容
String phone_num = phoneText.getText().toString();
String content =”QUERY“;
SmsManager smsMagager = SmsManager.getDefault();
if(content.length()> 70){
List
for(String con : sms){
smsMagager.sendTextMessage(phone_num, null, con, null, null);
}
} else {
smsMagager.sendTextMessage(phone_num, null, content, null, null);
}
Toast.makeText(MainActivity.this,R.string.info, Toast.LENGTH_LONG).show();
}
/*
* 监听短信数据库
*/
class SmsContent extends ContentObserver {
private Cursor cursor = null;@SuppressLint(”ShowToast“)public SmsContent(Handler handler){
super(handler);
// TODO Auto-generated constructor stu
}
@SuppressWarnings(”deprecation“)
@Override
public void onChange(boolean selfChange){
// TODO Auto-generated method stub
super.onChange(selfChange);
EditText phonenum=(EditText)findViewById(R.id.tel);
String telnum=”+86“+phonenum.getText().toString();
// 读取收件箱中指定号码的短信
cursor = managedQuery(Uri.parse(”content://sms/inbox“),new String[] { ”_id“, ”address“, ”read“, ”body“ },” address=? and read=?“,new String[] {telnum,”0“ }, ”_id desc“);
// 按id排序,如果按date排序的话,修改手机时间后,读取的短信就不准了
if(cursor!= null && cursor.getCount()> 0){
ContentValues values = new ContentValues();
values.put(”read“, ”1“);// 修改短信为已读模式
cursor.moveToNext();
int smsbodyColumn = cursor.getColumnIndex(”body“);String smsBody = cursor.getString(smsbodyColumn);
getmypos(smsBody);
//开启搜索
//经纬度
EditText lat =(EditText)findViewById(R.id.lat);EditText lon =(EditText)findViewById(R.id.lon);//设置输入框的内容
lat.setText(as[2]);lon.setText(as[1]);//得到需要标在地图上的资源
getpng(png);GeoPoint ptCenter = new GeoPoint((int)(Float.valueOf(lat.getText().toString())*1e6),(int)(Float.valueOf(lon.getText().toString())*1e6));//反Geo搜索
mSearch.reverseGeocode(ptCenter);
}