code\LoginActivity.java
code\func_page.java code\locate_page.java code\AddressToLatitudeLongitude.java code\AlertDialog.java code\MainActivity.java code\Mainactivity1.java code\MyOrientationListener.java code\NetworkState.java code\SocketConnectListener.java code\UDPSocketClientManage.java
This commit is contained in:
parent
209538a9b3
commit
ab958525fb
|
@ -0,0 +1,84 @@
|
|||
package com.example.administrator.myapp;
|
||||
|
||||
/**
|
||||
* Created by 小老宰 on 2018/3/16.
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
/**
|
||||
* Created by wanglei on 2017/6/20.
|
||||
* 根据百度地图API,根据地址得到经纬度
|
||||
*/
|
||||
public class AddressToLatitudeLongitude {
|
||||
private String address = "哈尔滨";//地址
|
||||
private double Latitude = 45.7732246332393;//纬度
|
||||
private double Longitude = 126.65771685544611;//经度
|
||||
|
||||
public AddressToLatitudeLongitude(String addr_str) {
|
||||
this.address = addr_str;
|
||||
}
|
||||
/*
|
||||
*根据地址得到地理坐标
|
||||
*/
|
||||
public void getLatAndLngByAddress(){
|
||||
String addr = "";
|
||||
String lat = "";
|
||||
String lng = "";
|
||||
try {
|
||||
addr = java.net.URLEncoder.encode(address,"UTF-8");//编码
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String url = String.format("http://api.map.baidu.com/geocoder/v2/?"
|
||||
+"address=%s&ak=4rcKAZKG9OIl0wDkICSLx8BA&output=json",addr);
|
||||
URL myURL = null;
|
||||
URLConnection httpsConn = null;
|
||||
//进行转码
|
||||
try {
|
||||
myURL = new URL(url);
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
// httpsConn = (URLConnection) myURL.openConnection();//建立连接
|
||||
if (httpsConn != null) {
|
||||
InputStreamReader insr = new InputStreamReader(//传输数据
|
||||
httpsConn.getInputStream(), "UTF-8");
|
||||
BufferedReader br = new BufferedReader(insr);
|
||||
String data = null;
|
||||
if ((data = br.readLine()) != null) {
|
||||
System.out.println(data);
|
||||
//这里的data为以下的json格式字符串,因为简单,所以就不使用json解析了,直接字符串处理
|
||||
//{"status":0,"result":{"location":{"lng":118.77807440802562,"lat":32.05723550180587},"precise":0,"confidence":12,"level":"城市"}}
|
||||
lat = data.substring(data.indexOf("\"lat\":")+("\"lat\":").length(), data.indexOf("},\"precise\""));
|
||||
lng = data.substring(data.indexOf("\"lng\":")+("\"lng\":").length(), data.indexOf(",\"lat\""));
|
||||
}
|
||||
insr.close();
|
||||
br.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.Latitude = Double.parseDouble(lat);
|
||||
this.Longitude = Double.parseDouble(lng);
|
||||
}
|
||||
public Double getLatitude() {
|
||||
return this.Latitude;
|
||||
}
|
||||
public Double getLongitude() {
|
||||
return this.Longitude;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
AddressToLatitudeLongitude at = new AddressToLatitudeLongitude("安徽省亳州市亳州一中");
|
||||
at.getLatAndLngByAddress();
|
||||
System.out.println(at.getLatitude() + " " + at.getLongitude());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.example.administrator.myapp;
|
||||
|
||||
import android.content.DialogInterface;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
||||
public class AlertDialog extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_login);
|
||||
}
|
||||
public void showdialog(View view)
|
||||
{
|
||||
//Toast.makeText(this,"clickme",Toast.LENGTH_LONG).show();
|
||||
android.support.v7.app.AlertDialog.Builder alertdialogbuilder=new android.support.v7.app.AlertDialog.Builder(this);
|
||||
alertdialogbuilder.setMessage("您确认要退出程序");
|
||||
alertdialogbuilder.setPositiveButton("确定", click1);
|
||||
alertdialogbuilder.setNegativeButton("取消", click2);
|
||||
android.support.v7.app.AlertDialog alertdialog1=alertdialogbuilder.create();
|
||||
alertdialog1.show();
|
||||
}
|
||||
private DialogInterface.OnClickListener click1=new DialogInterface.OnClickListener()
|
||||
{
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0,int arg1)
|
||||
{
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
}
|
||||
};
|
||||
private DialogInterface.OnClickListener click2=new DialogInterface.OnClickListener()
|
||||
{
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0,int arg1)
|
||||
{
|
||||
arg0.cancel();
|
||||
}
|
||||
};
|
||||
}
|
|
@ -95,13 +95,12 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
|
|||
|
||||
mLoginFormView = findViewById(R.id.login_form);
|
||||
mProgressView = findViewById(R.id.login_progress);
|
||||
/* findViewById(R.id.login_button).setOnClickListener(new OnClickListener() {
|
||||
findViewById(R.id.login_button).setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startActivity(new Intent(LoginActivity.this,func_page.class));
|
||||
}
|
||||
});*/
|
||||
//注册按钮
|
||||
});
|
||||
findViewById(R.id.register_button).setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
@ -132,9 +131,7 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
|
|||
@TargetApi(Build.VERSION_CODES.M)
|
||||
public void onClick(View v) {
|
||||
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
} else {
|
||||
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
|
||||
|
@ -205,27 +202,16 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
|
|||
showProgress(true);
|
||||
mAuthTask = new UserLoginTask(email, password);
|
||||
mAuthTask.execute((Void) null);
|
||||
|
||||
//这里加了一行,按需删除
|
||||
startActivity(new Intent(LoginActivity.this,func_page.class));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
private boolean isEmailValid(String email) {
|
||||
//TODO: Replace this with your own logic
|
||||
//这里改成自己的邮箱看看
|
||||
// return email.contains("@");
|
||||
// return email.equals("admin@qq.com");
|
||||
return true;
|
||||
return email.contains("@");
|
||||
}
|
||||
|
||||
private boolean isPasswordValid(String password) {
|
||||
//TODO: Replace this with your own logic
|
||||
//这里也换成自己的密码试试
|
||||
// return password.equals("123456");
|
||||
//return password.length() > 4;
|
||||
return true;
|
||||
return password.length() > 4;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -372,10 +358,7 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
|
|||
protected void onCancelled() {
|
||||
mAuthTask = null;
|
||||
showProgress(false);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,297 @@
|
|||
package com.example.administrator.myapp;
|
||||
|
||||
/**
|
||||
* Created by 小老宰 on 2018/3/16.
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.ActionBarActivity;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.baidu.location.BDLocation;
|
||||
import com.baidu.location.BDLocationListener;
|
||||
import com.baidu.location.LocationClient;
|
||||
import com.baidu.location.LocationClientOption;
|
||||
import com.baidu.mapapi.SDKInitializer;
|
||||
import com.baidu.mapapi.map.BaiduMap;
|
||||
import com.baidu.mapapi.map.BitmapDescriptor;
|
||||
import com.baidu.mapapi.map.BitmapDescriptorFactory;
|
||||
import com.baidu.mapapi.map.MapStatusUpdate;
|
||||
import com.baidu.mapapi.map.MapStatusUpdateFactory;
|
||||
import com.baidu.mapapi.map.MapView;
|
||||
import com.baidu.mapapi.map.MyLocationConfiguration;
|
||||
import com.baidu.mapapi.map.MyLocationData;
|
||||
import com.baidu.mapapi.model.LatLng;
|
||||
|
||||
public class MainActivity extends ActionBarActivity {
|
||||
private MapView myMapView = null;//地图控件
|
||||
private BaiduMap myBaiduMap;//百度地图对象
|
||||
private LocationClient mylocationClient;//定位服务客户对象
|
||||
private MylocationListener mylistener;//重写的监听类
|
||||
private Context context;
|
||||
|
||||
private double myLatitude;//纬度,用于存储自己所在位置的纬度
|
||||
private double myLongitude;//经度,用于存储自己所在位置的经度
|
||||
private float myCurrentX;
|
||||
|
||||
private BitmapDescriptor myIconLocation1;//图标1,当前位置的箭头图标
|
||||
// private BitmapDescriptor myIconLocation2;//图表2,前往位置的中心图标
|
||||
|
||||
private MyOrientationListener myOrientationListener;//方向感应器类对象
|
||||
|
||||
private MyLocationConfiguration.LocationMode locationMode;//定位图层显示方式
|
||||
// private MyLocationConfiguration.LocationMode locationMode2;//定位图层显示方式
|
||||
|
||||
private LinearLayout myLinearLayout1; //经纬度搜索区域1
|
||||
private LinearLayout myLinearLayout2; //地址搜索区域2
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
// requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
SDKInitializer.initialize(getApplicationContext());
|
||||
setContentView(R.layout.activity_main);
|
||||
this.context = this;
|
||||
initView();
|
||||
initLocation();
|
||||
}
|
||||
private void initView() {
|
||||
myMapView = (MapView) findViewById(R.id.baiduMapView);
|
||||
|
||||
myBaiduMap = myMapView.getMap();
|
||||
//根据给定增量缩放地图级别
|
||||
MapStatusUpdate msu= MapStatusUpdateFactory.zoomTo(18.0f);
|
||||
myBaiduMap.setMapStatus(msu);
|
||||
}
|
||||
|
||||
private void initLocation() {
|
||||
locationMode = MyLocationConfiguration.LocationMode.NORMAL;
|
||||
|
||||
//定位服务的客户端。宿主程序在客户端声明此类,并调用,目前只支持在主线程中启动
|
||||
mylocationClient = new LocationClient(this);
|
||||
mylistener = new MylocationListener();
|
||||
|
||||
//注册监听器
|
||||
mylocationClient.registerLocationListener(mylistener);
|
||||
//配置定位SDK各配置参数,比如定位模式、定位时间间隔、坐标系类型等
|
||||
LocationClientOption mOption = new LocationClientOption();
|
||||
//设置坐标类型
|
||||
mOption.setCoorType("bd09ll");
|
||||
//设置是否需要地址信息,默认为无地址
|
||||
mOption.setIsNeedAddress(true);
|
||||
//设置是否打开gps进行定位
|
||||
mOption.setOpenGps(true);
|
||||
//设置扫描间隔,单位是毫秒 当<1000(1s)时,定时定位无效
|
||||
int span = 1000;
|
||||
mOption.setScanSpan(span);
|
||||
//设置 LocationClientOption
|
||||
mylocationClient.setLocOption(mOption);
|
||||
|
||||
//初始化图标,BitmapDescriptorFactory是bitmap 描述信息工厂类.
|
||||
//myIconLocation1 = BitmapDescriptorFactory.fromResource(R.drawable.location_marker);
|
||||
// myIconLocation2 = BitmapDescriptorFactory.fromResource(R.drawable.icon_target);
|
||||
|
||||
//配置定位图层显示方式,三个参数的构造器
|
||||
MyLocationConfiguration configuration
|
||||
= new MyLocationConfiguration(locationMode, true, myIconLocation1);
|
||||
//设置定位图层配置信息,只有先允许定位图层后设置定位图层配置信息才会生效,参见 setMyLocationEnabled(boolean)
|
||||
myBaiduMap.setMyLocationConfigeration(configuration);
|
||||
|
||||
myOrientationListener = new MyOrientationListener(context);
|
||||
//通过接口回调来实现实时方向的改变
|
||||
myOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() {
|
||||
@Override
|
||||
public void onOrientationChanged(float x) {
|
||||
myCurrentX = x;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
*创建菜单操作
|
||||
*/
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
|
||||
getMenuInflater().inflate(R.menu.menu_item, menu);
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId())
|
||||
{
|
||||
/*
|
||||
*第一个功能,返回自己所在的位置,箭头表示
|
||||
*/
|
||||
case R.id.menu_item_mylocation://返回当前位置
|
||||
getLocationByLL(myLatitude, myLongitude);
|
||||
break;
|
||||
|
||||
/*
|
||||
*第二个功能,根据经度和纬度前往位置
|
||||
*/
|
||||
case R.id.menu_item_llsearch://根据经纬度搜索地点
|
||||
myLinearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
|
||||
//经纬度输入区域1可见
|
||||
myLinearLayout1.setVisibility(View.VISIBLE);
|
||||
final EditText myEditText_lg = (EditText) findViewById(R.id.editText_lg);
|
||||
final EditText myEditText_la = (EditText) findViewById(R.id.editText_la);
|
||||
Button button_ll = (Button) findViewById(R.id.button_llsearch);
|
||||
|
||||
button_ll.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final double mylg = Double.parseDouble(myEditText_lg.getText().toString());
|
||||
final double myla = Double.parseDouble(myEditText_la.getText().toString());
|
||||
getLocationByLL(myla, mylg);
|
||||
//隐藏前面经纬度输入区域
|
||||
myLinearLayout1.setVisibility(View.GONE);
|
||||
// Toast.makeText(context, "", Toast.LENGTH_SHORT).show();
|
||||
//隐藏输入法键盘
|
||||
InputMethodManager imm =(InputMethodManager)getSystemService(
|
||||
Context.INPUT_METHOD_SERVICE);
|
||||
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
/*
|
||||
*第三个功能,根据地址名前往所在的位置
|
||||
*/
|
||||
case R.id.menu_item_sitesearch://根据地址搜索
|
||||
myLinearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);
|
||||
//显示地址搜索区域2
|
||||
myLinearLayout2.setVisibility(View.VISIBLE);
|
||||
final EditText myEditText_site = (EditText) findViewById(R.id.editText_site);
|
||||
Button button_site = (Button) findViewById(R.id.button_sitesearch);
|
||||
|
||||
button_site.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final String site_str = myEditText_site.getText().toString();
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
AddressToLatitudeLongitude at = new AddressToLatitudeLongitude(site_str);
|
||||
at.getLatAndLngByAddress();
|
||||
getLocationByLL(at.getLatitude(), at.getLongitude());
|
||||
}
|
||||
}).start();
|
||||
//隐藏前面地址输入区域
|
||||
myLinearLayout2.setVisibility(View.GONE);
|
||||
//隐藏输入法键盘
|
||||
InputMethodManager imm = (InputMethodManager) getSystemService(
|
||||
Context.INPUT_METHOD_SERVICE);
|
||||
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
/*
|
||||
*根据经纬度前往
|
||||
*/
|
||||
public void getLocationByLL(double la, double lg)
|
||||
{
|
||||
//地理坐标的数据结构
|
||||
LatLng latLng = new LatLng(la, lg);
|
||||
//描述地图状态将要发生的变化,通过当前经纬度来使地图显示到该位置
|
||||
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
|
||||
myBaiduMap.setMapStatus(msu);
|
||||
}
|
||||
|
||||
/*
|
||||
*定位请求回调接口
|
||||
*/
|
||||
public class MylocationListener implements BDLocationListener
|
||||
{
|
||||
//定位请求回调接口
|
||||
private boolean isFirstIn=true;
|
||||
//定位请求回调函数,这里面会得到定位信息
|
||||
@Override
|
||||
public void onReceiveLocation(BDLocation bdLocation) {
|
||||
//BDLocation 回调的百度坐标类,内部封装了如经纬度、半径等属性信息
|
||||
//MyLocationData 定位数据,定位数据建造器
|
||||
/*
|
||||
* 可以通过BDLocation配置如下参数
|
||||
* 1.accuracy 定位精度
|
||||
* 2.latitude 百度纬度坐标
|
||||
* 3.longitude 百度经度坐标
|
||||
* 4.satellitesNum GPS定位时卫星数目 getSatelliteNumber() gps定位结果时,获取gps锁定用的卫星数
|
||||
* 5.speed GPS定位时速度 getSpeed()获取速度,仅gps定位结果时有速度信息,单位公里/小时,默认值0.0f
|
||||
* 6.direction GPS定位时方向角度
|
||||
* */
|
||||
myLatitude = bdLocation.getLatitude();
|
||||
myLongitude = bdLocation.getLongitude();
|
||||
MyLocationData data = new MyLocationData.Builder()
|
||||
.direction(myCurrentX)//设定图标方向
|
||||
.accuracy(bdLocation.getRadius())//getRadius 获取定位精度,默认值0.0f
|
||||
.latitude(myLatitude)//百度纬度坐标
|
||||
.longitude(myLongitude)//百度经度坐标
|
||||
.build();
|
||||
//设置定位数据, 只有先允许定位图层后设置数据才会生效,参见 setMyLocationEnabled(boolean)
|
||||
myBaiduMap.setMyLocationData(data);
|
||||
|
||||
//判断是否为第一次定位,是的话需要定位到用户当前位置
|
||||
if (isFirstIn) {
|
||||
//根据当前所在位置经纬度前往
|
||||
getLocationByLL(myLatitude, myLongitude);
|
||||
isFirstIn = false;
|
||||
//提示当前所在地址信息
|
||||
// Toast.makeText(context, bdLocation.getAddrStr(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*定位服务的生命周期,达到节省
|
||||
*/
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
//开启定位,显示位置图标
|
||||
myBaiduMap.setMyLocationEnabled(true);
|
||||
if(!mylocationClient.isStarted())
|
||||
{
|
||||
mylocationClient.start();
|
||||
}
|
||||
|
||||
myOrientationListener.start();
|
||||
// getLocationByLL(117.11,40.2);
|
||||
}
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
//停止定位
|
||||
myBaiduMap.setMyLocationEnabled(false);
|
||||
mylocationClient.stop();
|
||||
myOrientationListener.stop();
|
||||
}
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
myMapView.onResume();
|
||||
}
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
myMapView.onPause();
|
||||
}
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
myMapView.onDestroy();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
package com.example.administrator.myapp;
|
||||
|
||||
/**
|
||||
* Created by 小老宰 on 2018/4/3.
|
||||
*/
|
||||
|
||||
import android.support.v7.app.ActionBarActivity;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
|
||||
public class Mainactivity1 extends ActionBarActivity implements OnClickListener{
|
||||
UDPSocketClientManage socketClientManage = null;
|
||||
private String mstrDataString="";
|
||||
private TextView textViewRecrive;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.udp_main);
|
||||
|
||||
|
||||
findViewById(R.id.button1).setOnClickListener(this);
|
||||
findViewById(R.id.button2).setOnClickListener(this);
|
||||
findViewById(R.id.button3).setOnClickListener(this);
|
||||
TextView loTextView = (TextView)findViewById(R.id.textViewLoca);
|
||||
|
||||
//手机端的连接路由之后IP地址,网络调试助手向目标主机发送的IP地址就是这里获取出来的
|
||||
String strLoString = UDPSocketClientManage.getLocalIpAddress();
|
||||
if (strLoString != null) {
|
||||
loTextView.setText(strLoString);
|
||||
}
|
||||
textViewRecrive = (TextView)findViewById(R.id.textViewRecrive);
|
||||
|
||||
socketClientManage = new UDPSocketClientManage();
|
||||
socketClientManage.RegSocketConnectListener(new SocketConnectListener() {
|
||||
|
||||
@Override
|
||||
public void OnReceiverCallBack(int nLength, byte[] data) {
|
||||
mstrDataString = new String(data);
|
||||
mHandler.sendEmptyMessage(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void OnConnectStatusCallBack(NetworkState networkState) {
|
||||
switch (networkState) {
|
||||
case NETWORK_STATE_CONNECT_SUCCEED:
|
||||
mHandler.sendEmptyMessage(0);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
Handler mHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
switch (msg.what) {
|
||||
case 0: // 接受到消息之后,对UI控件进行修改
|
||||
Toast.makeText(Mainactivity1.this, "连接成功", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case 1: // 接受到消息之后,对UI控件进行修改
|
||||
textViewRecrive.append(mstrDataString);
|
||||
Toast.makeText(Mainactivity1.this, mstrDataString, Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (v.getId()) {
|
||||
case R.id.button1:
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
socketClientManage.Connect();
|
||||
|
||||
}
|
||||
}).start();
|
||||
|
||||
break;
|
||||
case R.id.button2:
|
||||
EditText ipEditText = (EditText)findViewById(R.id.editText1);
|
||||
EditText porText = (EditText)findViewById(R.id.editText2);
|
||||
String ipString = ipEditText.getText().toString().trim();
|
||||
String portString = porText.getText().toString().trim();
|
||||
socketClientManage.setNetworkParameter(ipString, portString != null ? Integer.parseInt(portString) : 0);
|
||||
Toast.makeText(Mainactivity1.this, "设置成功", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case R.id.button3:
|
||||
EditText sendEditText = (EditText)findViewById(R.id.editText3);
|
||||
String sendDataString = sendEditText.getText().toString().trim();
|
||||
if(sendDataString != null)
|
||||
socketClientManage.send(sendDataString.getBytes(), sendDataString.getBytes().length);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}}
|
|
@ -0,0 +1,82 @@
|
|||
package com.example.administrator.myapp;
|
||||
|
||||
/**
|
||||
* Created by 小老宰 on 2018/3/16.
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
|
||||
public class MyOrientationListener implements SensorEventListener{
|
||||
|
||||
private SensorManager mSensorManager;
|
||||
private Sensor mSensor;
|
||||
private Context mContext;
|
||||
private float lastX;
|
||||
private OnOrientationListener mOnOrientationListener;
|
||||
|
||||
public MyOrientationListener(Context context)
|
||||
{
|
||||
this.mContext=context;
|
||||
}
|
||||
public void start()
|
||||
{
|
||||
mSensorManager= (SensorManager) mContext
|
||||
.getSystemService(Context.SENSOR_SERVICE);
|
||||
if(mSensorManager!= null)
|
||||
{
|
||||
//获得方向传感器
|
||||
mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
|
||||
}
|
||||
//判断是否有方向传感器
|
||||
// if(mSensor!=null)
|
||||
// {
|
||||
// //注册监听器
|
||||
// mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_UI);
|
||||
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
public void stop()
|
||||
{
|
||||
mSensorManager.unregisterListener(this);
|
||||
|
||||
}
|
||||
//方向改变
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
if(event.sensor.getType()==Sensor.TYPE_ORIENTATION)
|
||||
{
|
||||
float x=event.values[SensorManager.DATA_X];
|
||||
if(Math.abs(x-lastX)>1.0)
|
||||
{
|
||||
if(mOnOrientationListener!=null)
|
||||
{
|
||||
mOnOrientationListener.onOrientationChanged(x);
|
||||
}
|
||||
}
|
||||
lastX=x;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public void setOnOrientationListener(OnOrientationListener listener)
|
||||
{
|
||||
mOnOrientationListener=listener;
|
||||
}
|
||||
|
||||
public interface OnOrientationListener
|
||||
{
|
||||
void onOrientationChanged(float x);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.example.administrator.myapp;
|
||||
|
||||
/**
|
||||
* Created by 小老宰 on 2018/4/3.
|
||||
*/
|
||||
|
||||
public enum NetworkState {
|
||||
NETWORK_STATE_NULL, // 无状态
|
||||
NETWORK_STATE_CONNECT_SUCCEED, // 网络连接成功
|
||||
NETWORK_STATE_DISCONNECT_SUCCEED, // 网络断开成功(自身断开)
|
||||
NETWORK_STATE_SERVER_DISCONNECT, // 网络被服务器断开
|
||||
NETWORK_STATE_CONNECT_FAILLD, // 连接服务器失败,IP/端口不正常
|
||||
NETWORK_STATE_CONNECT_ING, // 正在连接过程中...
|
||||
NETWORK_STATE_RXD, // 接收数据
|
||||
NETWORK_STATE_TXD; // 发送数据
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.example.administrator.myapp;
|
||||
|
||||
/**
|
||||
* Created by 小老宰 on 2018/4/3.
|
||||
*/
|
||||
|
||||
public abstract class SocketConnectListener {
|
||||
// 网络状态回调
|
||||
public abstract void OnConnectStatusCallBack(NetworkState networkState);
|
||||
|
||||
// 接收数据回调
|
||||
public abstract void OnReceiverCallBack(int nLength, byte[] data);
|
||||
|
||||
}
|
|
@ -0,0 +1,209 @@
|
|||
package com.example.administrator.myapp;
|
||||
|
||||
/**
|
||||
* Created by 小老宰 on 2018/4/3.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Enumeration;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.util.Log;
|
||||
|
||||
public class UDPSocketClientManage {
|
||||
// 服务器IP
|
||||
private static String SERVER_IP = "192.168.43.156";
|
||||
// 服务器端口
|
||||
private static int LOCAL_PORT_AUDIO = 8080;
|
||||
// 接收数据包
|
||||
private DatagramPacket Packet_Receive;
|
||||
// 端口
|
||||
private DatagramSocket msocketClient;
|
||||
|
||||
NetworkState mLastNetworkState = NetworkState.NETWORK_STATE_NULL;
|
||||
SocketConnectListener mConnectListener = null;
|
||||
|
||||
// 设置网络连接参数
|
||||
public void setNetworkParameter(String strIP, int nPort) {
|
||||
SERVER_IP = strIP;
|
||||
LOCAL_PORT_AUDIO = nPort;
|
||||
}
|
||||
|
||||
// 注册接收连接状态和数据的回调函数
|
||||
public void RegSocketConnectListener(SocketConnectListener listener) {
|
||||
mConnectListener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动连接服务器
|
||||
*/
|
||||
public void Connect() {
|
||||
// 正在开始连接
|
||||
mLastNetworkState = NetworkState.NETWORK_STATE_CONNECT_ING;
|
||||
|
||||
try {
|
||||
// 端口
|
||||
msocketClient = new DatagramSocket(LOCAL_PORT_AUDIO);
|
||||
// 接收数据缓存
|
||||
byte[] Buffer_Receive = new byte[1024];
|
||||
// 接收包
|
||||
Packet_Receive = new DatagramPacket(Buffer_Receive,1024);
|
||||
|
||||
mLastNetworkState = NetworkState.NETWORK_STATE_CONNECT_SUCCEED;
|
||||
|
||||
} catch (IOException e) {
|
||||
mLastNetworkState = NetworkState.NETWORK_STATE_CONNECT_FAILLD;
|
||||
Log.e("Show", e.toString());
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
mLastNetworkState = NetworkState.NETWORK_STATE_CONNECT_FAILLD;
|
||||
Log.e("Show", e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 向回调发数据
|
||||
if (null != mConnectListener) {
|
||||
mConnectListener.OnConnectStatusCallBack(mLastNetworkState);
|
||||
}
|
||||
|
||||
if (msocketClient != null) {
|
||||
new Thread(reRunnable).start();
|
||||
}
|
||||
}
|
||||
|
||||
Runnable reRunnable = new Runnable() {
|
||||
@SuppressLint("NewApi") @Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
// 接收数据
|
||||
if (Packet_Receive != null) {
|
||||
msocketClient.receive(Packet_Receive);
|
||||
|
||||
// 判断数据是否合法
|
||||
InetSocketAddress address = (InetSocketAddress) Packet_Receive.getSocketAddress();
|
||||
// 判断是否是调度服务器的ip
|
||||
if (!address.getHostName().equals(SERVER_IP)) {
|
||||
continue;
|
||||
}
|
||||
// 判断是否是调度服务器的端口
|
||||
if (address.getPort() != LOCAL_PORT_AUDIO) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int length = Packet_Receive.getLength();
|
||||
if (length > 0)
|
||||
mConnectListener.OnReceiverCallBack(length, Packet_Receive.getData());
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.e("Show", e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 断开连接
|
||||
*/
|
||||
public void Close() {
|
||||
if (msocketClient != null) {
|
||||
msocketClient = null;
|
||||
mLastNetworkState = NetworkState.NETWORK_STATE_DISCONNECT_SUCCEED;
|
||||
mConnectListener.OnConnectStatusCallBack(mLastNetworkState);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 发送数据
|
||||
* @param data
|
||||
* :需要发送的数据
|
||||
* @param len
|
||||
* :数据字节数据
|
||||
*/
|
||||
public void send(byte[] data, int len) {
|
||||
Thread_Send thread_send = new Thread_Send(data, len);
|
||||
new Thread(thread_send).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 发送线程
|
||||
*/
|
||||
private class Thread_Send implements Runnable {
|
||||
// 发送数据缓存
|
||||
private byte[] Buffer_Send = new byte[1024];
|
||||
// 发送数据包
|
||||
private DatagramPacket Packet_Send;
|
||||
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param data
|
||||
* :需要发送的数据
|
||||
* @param len
|
||||
* :数据字节数据
|
||||
*/
|
||||
public Thread_Send(byte[] data, int len) {
|
||||
// 发送包
|
||||
Packet_Send = new DatagramPacket(Buffer_Send,1024);
|
||||
Packet_Send.setData(data);
|
||||
Packet_Send.setLength(len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Packet_Send.setPort(LOCAL_PORT_AUDIO);
|
||||
Packet_Send.setAddress(InetAddress.getByName(SERVER_IP));
|
||||
if (msocketClient != null) {
|
||||
msocketClient.send(Packet_Send);
|
||||
mLastNetworkState = NetworkState.NETWORK_STATE_TXD;
|
||||
mConnectListener.OnConnectStatusCallBack(mLastNetworkState);
|
||||
}else {
|
||||
mLastNetworkState = NetworkState.NETWORK_STATE_NULL;
|
||||
mConnectListener.OnConnectStatusCallBack(mLastNetworkState);
|
||||
}
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
mLastNetworkState = NetworkState.NETWORK_STATE_NULL;
|
||||
mConnectListener.OnConnectStatusCallBack(mLastNetworkState);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
mLastNetworkState = NetworkState.NETWORK_STATE_NULL;
|
||||
mConnectListener.OnConnectStatusCallBack(mLastNetworkState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取最后的网络状态
|
||||
public NetworkState getLastNetworkState() {
|
||||
return mLastNetworkState;
|
||||
}
|
||||
|
||||
public static String getLocalIpAddress() {
|
||||
try {
|
||||
for (Enumeration<NetworkInterface> en = NetworkInterface
|
||||
.getNetworkInterfaces(); en.hasMoreElements();) {
|
||||
NetworkInterface intf = en.nextElement();
|
||||
for (Enumeration<InetAddress> enumIpAddr = intf
|
||||
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
|
||||
InetAddress inetAddress = enumIpAddr.nextElement();
|
||||
if (!inetAddress.isLoopbackAddress()
|
||||
&& !inetAddress.isLinkLocalAddress()) {
|
||||
return inetAddress.getHostAddress().toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SocketException ex) {
|
||||
Log.e("WifiPreference IpAddress", ex.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ public class func_page extends AppCompatActivity {
|
|||
findViewById(R.id.locate_button).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startActivity(new Intent(func_page.this,locate_page.class));
|
||||
startActivity(new Intent(func_page.this,MainActivity.class));
|
||||
}
|
||||
});
|
||||
findViewById(R.id.address_button).setOnClickListener(new View.OnClickListener() {
|
||||
|
@ -30,6 +30,12 @@ public class func_page extends AppCompatActivity {
|
|||
startActivity(new Intent(func_page.this,my_address_page.class));
|
||||
}
|
||||
});
|
||||
findViewById(R.id.information_button).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startActivity(new Intent(func_page.this,Mainactivity1.class));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,22 +4,20 @@ import android.app.Activity;
|
|||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import com.baidu.mapapi.SDKInitializer;
|
||||
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
||||
public class locate_page extends Activity {
|
||||
private Spinner myspinner;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
SDKInitializer.initialize(getApplicationContext());
|
||||
setContentView(R.layout.activity_locate_page);
|
||||
|
||||
myspinner= (Spinner) findViewById(R.id.goods_list);
|
||||
|
@ -97,7 +95,7 @@ public class locate_page extends Activity {
|
|||
@Override
|
||||
public void onClick(DialogInterface arg0,int arg1)
|
||||
{
|
||||
// android.os.Process.killProcess(android.os.Process.myPid());
|
||||
// android.os.Process.killProcess(android.os.Process.myPid());
|
||||
startActivity(new Intent(locate_page.this,get_goods.class));
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue