android百度地图api实现短信接收定位_百度地图api定位

2020-02-28 其他范文 下载本文

android百度地图api实现短信接收定位由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“百度地图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 cla 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 cla 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 onGetPermiionState(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.SuppreLint;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 cla 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 itemOverlay = new ItemizedOverlay(null, mMapView);

//生成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){

SearchButtonProce(v);

}

};

mBtnReverseGeoCode.setOnClickListener(clickListener);

mBtnGeoCode.setOnClickListener(clickListener);

msmSend.setOnClickListener(clickListener);

} /**

* 发起搜索

* @param v

*/ void SearchButtonProce(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);

//发送短信

sendMeage();

} }

@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 sendMeage(){

// 获取文本框中的内容

String phone_num = phoneText.getText().toString();

String content =“QUERY”;

SmsManager smsMagager = SmsManager.getDefault();

if(content.length()> 70){

List sms = smsMagager.divideMeage(content);

for(String con : sms){

smsMagager.sendTextMeage(phone_num, null, con, null, null);

}

} else {

smsMagager.sendTextMeage(phone_num, null, content, null, null);

}

Toast.makeText(MainActivity.this,R.string.info, Toast.LENGTH_LONG).show();

}

/*

* 监听短信数据库

*/

cla SmsContent extends ContentObserver {

private Cursor cursor = null;@SuppreLint(“ShowToast”)public SmsContent(Handler handler){

super(handler);

// TODO Auto-generated constructor stu

}

@SuppreWarnings(“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”, “addre”, “read”, “body” },“ addre=? 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);

}

// 在用managedQuery的时候,不能主动调用close()方法,否则在

android:layout_height=“match_parent”

tools:context=“.MainActivity” >

android:id=“@+id/bmapView”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:clickable=“true” />

android:id=“@+id/lat”

android:layout_width=“125dp”

android:layout_height=“wrap_content”

android:layout_alignParentLeft=“true”

android:layout_alignParentTop=“true”

android:ems=“10”

android:text=“39.904965” />

android:id=“@+id/reversegeocode”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignBaseline=“@+id/lat”

android:layout_alignBottom=“@+id/lat”

android:layout_alignParentRight=“true”

android:text=“查找” />

android:id=“@+id/geocode”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentRight=“true”

android:layout_alignTop=“@+id/geocodekey” android:text=“查找” />

android:id=“@+id/sendButton”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignBottom=“@+id/tel”

android:layout_alignParentRight=“true”

android:text=“发送” />

“ android:id=”@+id/lon“

android:layout_width=”125dp“

android:layout_height=”wrap_content“

android:layout_alignTop=”@+id/reversegeocode“

android:layout_toRightOf=”@+id/lat“

android:ems=”10“

android:text=”116.327764“ />

android:id=”@+id/geocodekey“

android:layout_width=”250dp“

android:layout_height=”wrap_content“

android:layout_alignParentLeft=”true“

android:layout_below=”@+id/reversegeocode“

android:ems=”20“

android:text=”北京市海淀区上地十街10号“ >

android:id=”@+id/city“

android:layout_width=”0dp“

android:layout_height=”wrap_content“

android:layout_below=”@+id/reversegeocode“

android:layout_toLeftOf=”@+id/geocode“

android:ems=”10“

android:text=”“ />

android:id=”@+id/tel“

android:layout_width=”250dp“

android:layout_height=”wrap_content“

android:layout_alignParentLeft=”true“

android:layout_below=”@+id/geocodekey“

android:ems=”15“

android:text=”1503769891“ />

Manifest.xml:

package=”com.gy.mymaps“

android:versionCode=”1“

android:versionName=”1.0“ >

android:minSdkVersion=”10“

android:targetSdkVersion=”10“ />

android:name=”com.gy.mymaps.DemoApplication“

android:debuggable=”true“

android:allowBackup=”true“

android:icon=”@drawable/ic_launcher“

android:label=”@string/app_name“

android:theme=”@style/AppTheme“ >

android:name=”com.baidu.lbsapi.API_KEY“

android:value=”O5h23uRrcyxbaiZs3w7skruP“ />

android:name=”com.gy.mymaps.MainActivity“

android:label=”@string/app_name“ >

/> />

android:name=”com.android.launcher.permiion.READ_SETTINGS“ />

android:name=”android.permiion.WRITE_EXTERNAL_STORAGE“ />

android:anyDensity=”true“

android:largeScreens=”true“

android:normalScreens=”false“

android:resizeable=”true“

android:smallScreens=”true“ />

String.xml:

mymaps

Settings 短信发送成功

该app是通过发送QUEST到一个返回位置信息的设备,返回的短信 接收的短信格式(可以通过修改字符串处理方法来修改处理方法): #Lon=116486291,Lat=39899332,Speed=0,Alt=65,Direction=0#

百度面试Android面试题

百度面试Android面试题(通用9篇)由网友“脚鸡”投稿提供,以下是小编给大家整理的百度面试Android面试题,欢迎大家前来参阅。篇1:百度面试Android面试题 1.java 基础题,输入结果是......

百度地图商户更名声明

百度地图标注免责声明我公司自愿将现有商户名称更改为,并承担由此带来的一切问题(如客户投诉等),百度地图不承担由此带来的任何后果。机构盖章:倍全授权代表签字:倍全 年月日......

百度地图认领确认书word版格式

百度地图商户类POI认领确认书北京百度网讯科技有限公司:我司(公司名称:____________________________,公司地址: _____________________________________________________,公司电......

端午节百度地图app没有粽子怎么办

端午节百度地图app没有粽子怎么办(精选5篇)由网友“zhangym”投稿提供,以下是小编整理过的端午节百度地图app没有粽子怎么办,欢迎阅读分享,希望对大家有帮助。篇1:端午节百度地图a......

准确自我定位 实现人生价值

准确自我定位 实现人生价值——计算机系2014届就业指导会本刊记者/金小乐2013年6月7日19:30,由西南交大学峨眉校区计算机与通信工程系主办的2014届毕业生就业指导系列活动之......

《android百度地图api实现短信接收定位.docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
android百度地图api实现短信接收定位
点击下载文档
相关专题 百度地图api定位 短信 地图 Android 百度地图api定位 短信 地图 Android
[其他范文]相关推荐
[其他范文]热门文章
下载全文