4.在地图上添加一个标记:这种写法会导致addoverlay是红色的

改法:
把mbaidumap要改为自己地图资源的名字:mMapview.
特别需要注意的是,这段代码应该添加到oncreat函数中,不然也会导致addoverlay为红色的,addoverlay是创建覆盖物的一个方法
在构建maker图标前添加一句话:

在地图上创建可以移动的覆盖物:

改法:

5.如何实现地图的定位:
(1)参考慕课网的方法
①调用百度地图的定位,实现出来和安卓的系统定位不一样的地方就是使用百度地图进行定位能够在屏幕的中央显示一个小蓝点。
在mainactivity中使用initLocation方法,然后创建这个方法。

然后再书写onstart和onstop方法的代码,在进入程序的时候开始进行定位,离开的时候销毁定位。这样可以每次打开应用的时候都是更新了的位置。

还需要写一个位置监听器。用位置监听器获得当前经纬度

效果:

(2)用安卓自带的系统定位
安卓系统可以调用自己的定位,可以通过文本形式在界面中进行显示。
通过addtext方法添加到界面中进行经纬度的显示。显示为location=参数值。调用广播接收,查找经纬度值。
①添加一个textview控件

②需要声明的变量

③需要添加在mainactivity的oncreate函数中的


④需要添加到mainactivity中其他位置的代码
位置监听器:

列表函数方法:


Onresume

Onpause

⑤效果图

6.出现的问题
定位到其他位置:①可能是手机GPS没有处于打开状态
②源代码中权限没添加
③手机不信任此应用,需要手动打开权限,权限没有打开很有可能导致程序崩溃。

7.发短信:
1.第一种方法
在界面上添加to,what,getfrom,getcontent,send五个关于发短信的控件。
实现的功能是,在to的编辑框内输入一个电话号码,在what里编辑短信,点击send实现发送。
当好友收到短信时,在getfrom的文本框内会显示他的电话号码;app自动读取短信,获取经纬度。在getcontent的文本框内显示短信内容。


①在界面上添加to,what,getfrom,getcontent,send五个关于发短信的控件。

②mainactivity中

Onresume中

短信发送后的发送状态广播接收器:

//短信发送到对方后,对对方返回的接受状态的处理逻辑public classSmsDeliveryStatusReceiverextendsBroadcastReceiver {
private static finalStringTAG="SmsDeliveryStatusReceiver";
@Override
public voidonReceive(Context context, Intent intent) {// Log.d(TAG,"SmsDeliveryStatusReceiver onReceive.");switch(getResultCode()) {
caseActivity.RESULT_OK:
Toast.makeText(context,"Send Succeeded",Toast.LENGTH_SHORT).show();// Log.i(TAG, "RESULT_OK");break;
caseActivity.RESULT_CANCELED:
Toast.makeText(context,"Send Failed",Toast.LENGTH_SHORT).show();// Log.i(TAG, "RESULT_CANCELED");break;
}
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[])bundle.get("pdus");
SmsMessage[] messages =newSmsMessage[pdus.length];
for(inti =0; ilength; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
String address = messages[0].getOriginatingAddress();
String fullMessage =" ";
for(SmsMessage message : messages)
{
fullMessage += message.getMessageBody();
}//收取处理短信字符串 -> address fullmessageif(fullMessage.trim().equals("where are you?"))
{
//Toast.makeText(getApplicationContext(),address.substring(3, address.length()), Toast.LENGTH_SHORT).show();SmsManager smsManager = SmsManager.getDefault();//address.substring(3)smsManager.sendTextMessage(address.substring(3, address.length()),null,//String.valueOf(mLatitude)+"/"+String.valueOf(mLongtitude),null,null);
}
}
}
MessageReceivermessageReceiver=newMessageReceiver();
//registerReceiver(messageReceiver, receiveFilter);classMessageReceiverextendsBroadcastReceiver{
@Override
public voidonReceive(Context context, Intent intent) {
String sms ="";
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
for(Object pdu : pdus) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
String address = smsMessage.getDisplayOriginatingAddress();
String fullMessage = smsMessage.getMessageBody();
sender.setText(address);
//content.setText(fullMessage);sms += fullMessage;
/* if ("10086".equals(address)) {//测试截断短信abortBroadcast();}*/}
content.setText(sms);
}
}
2.第二种方法
①首先在main样式表中添加“特别关心”Button控件,ID:button1;

②声明button

在mainactivity的oncreat函数中添加

点击按钮后执行的方法:

这样短信就可以通过点击“特别关心”按钮进行发送啦
8.如何自动回复好友的短信为经纬度,如何自动读取短信并且显示point坐标在地图资源控件上:
①在mainactivity最后一个大括号之前添加:
这个方法介绍的内容是:在收到了一个短信之后,把短信的内容转化为massage,把发送短信的号码传值给sender变量;用if语句判断短信的内容是否为“where are you?”,如果是where are you?的话就把获取的短信号码变为将要回复的短信号码。回复的内容为:经度+纬度。然后进行发送

如果收到短信的内容中包含内容“/”(包含经纬度坐标的回复短信)的话,则开始执行短信内容转化为经纬度坐标并显示在地图上的代码。
先把短信进行了拆分,传值进字符串mLatitude和mlongtitude,再进行数据类型的强制转化(因为坐标点必须是double类型的数据),最后创建坐标点。

②在onstart方法中添加在程序一开始就运行监测收到短信的代码:如下图所示

③需要声明的变量名:

达到的效果:

网友评论