模拟器跑的时候没问题 手机运行就出现以下问题
2条回答 默认 最新
- 狂我之心 2017-03-21 03:21关注
package www.csy.com.adress;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;public class NetworkConnectChangedReceiver extends BroadcastReceiver {
public NetEvevt evevt = BaseActivity.evevt; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { int netWorkState = NetUtil.getNetWorkState(context); // 接口回调传过去状态的类型 evevt.onNetChange(netWorkState); } } // 自定义接口 public interface NetEvevt { public void onNetChange(int netMobile); }
}
package www.csy.com.adress;
import android.Manifest;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;import com.example.getlocation.R;
import java.io.IOException;
import java.util.List;public class Adress extends BaseActivity {
private TextView postionView; private LocationManager locationManager; public String locationProvider; public String str; public double jingdu; public double weidu; public String id = null; public SQLiteDatabase db = null; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.adress); Button bt1 = (Button) findViewById(R.id.bt1); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /** * 清空轻量级储存器 * */ SharedPreferences sharedPreferences = getSharedPreferences("userinfo", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.remove("uname"); editor.remove("upswd"); editor.commit(); Intent intent = new Intent(Adress.this, MainActivity.class); startActivity(intent); } }); //获取显示地理位置信息的TextView postionView = (TextView) findViewById(R.id.positionView); //获取地理位置管理器 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //获取所有可用的位置提供器 List<String> providers = locationManager.getProviders(true); if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS locationProvider = LocationManager.GPS_PROVIDER; } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是Network locationProvider = LocationManager.NETWORK_PROVIDER; } else { Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show(); return; } //获取Location if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } Location location = locationManager.getLastKnownLocation(locationProvider); if (location != null) { //不为空,显示地理位置经纬度 try { showLocation(location); /** *与服务器开始连接 */ id = getIntent().getStringExtra("id"); Double jingdu1 = location.getLongitude(); Double weidu1 = location.getLatitude(); mWebSocket MW = new mWebSocket(); MW.connect(id, jingdu1, weidu1); } catch (IOException e) { e.printStackTrace(); } } //监视地理位置变化 locationManager.requestLocationUpdates(locationProvider, 60 * 1000/*1s*/, 1/*1米*/, locationListener); } /** * 显示地理位置经度和纬度信息 */ private void showLocation(Location location) throws IOException { id = getIntent().getStringExtra("id"); weidu = location.getLatitude(); jingdu = location.getLongitude(); String locationStr = "维度:" + location.getLatitude() + "\n" + "经度:" + location.getLongitude(); postionView.setText(locationStr); /** * 如果网络可用。。。。。 * 如果网络不可用。。。。 */ if (isNetworkAvailable.panduanwangluo(this)) { L.e("网络可用"); str = new Gettime().getTime(); try { if (HttpUtils.httpurl(id, jingdu, weidu, str)) { Toast.makeText(Adress.this, "成功上传数据库", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(Adress.this, "失败!!!", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else { L.e("没有网络"); onNetChange(NetUtil.NETWORK_NONE); } } /** * LocationListern监听器 * 参数:地理位置提供器、监听位置变化的时间间隔、位置变化的距离间隔、LocationListener监听器 */ LocationListener locationListener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle arg2) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(Location location) { //如果位置发生变化,重新显示 try { showLocation(location); } catch (IOException e) { e.printStackTrace(); } } }; public void onNetChange(int netMobile) { // TODO Auto-generated method stub //在这个判断,根据需要做处理 if (netMobile == NetUtil.NETWORK_NONE) { Toast.makeText(this, "网络断开", Toast.LENGTH_SHORT).show(); L.e("网络断开"); str = new Gettime().getTime();//获得时间 db = openOrCreateDatabase("address", MODE_PRIVATE, null); db.execSQL("create table if not exists stutb(_id integer primary key autoincrement," + "id text not null,lon text not null,lat text not null,time text not null)"); ContentValues values = new ContentValues(); values.put("id", id); values.put("lon", jingdu); values.put("lat", weidu); values.put("time", str); db.insert("stutb", null, values); Toast.makeText(this, "数据已插入", Toast.LENGTH_SHORT).show(); values.clear(); } else { Toast.makeText(this, "网络已连接", Toast.LENGTH_SHORT).show(); L.e("网络已连接"); Cursor c = db.query("stutb", null, null, null, null, null, null); if (c != null) { while (c.moveToNext()) { String id = c.getString(c.getColumnIndex("id")); Double lon = c.getDouble(c.getColumnIndex("lon")); Double lat = c.getDouble(c.getColumnIndex("lat")); String time = c.getString(c.getColumnIndex("time")); if (HttpUtils.httpurl(id, lon, lat, time)) { Toast.makeText(this, "数据已上传", Toast.LENGTH_SHORT).show(); db.delete("stutb", "id=?", new String[]{id}); } Toast.makeText(this, id + lon + lat + time, Toast.LENGTH_SHORT).show(); }
// db.execSQL("DROP stutb");
Toast.makeText(this, "数据已清空", Toast.LENGTH_SHORT).show();
c.close();
}
db.close();
}
}@Override protected void onDestroy() { super.onDestroy(); if (locationManager != null) { //移除监听器 if (ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.removeUpdates(locationListener); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.mian, menu); return true; }
}
解决 无用评论 打赏 举报
悬赏问题
- ¥15 无线电能传输系统MATLAB仿真问题
- ¥50 如何用脚本实现输入法的热键设置
- ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
- ¥30 深度学习,前后端连接
- ¥15 孟德尔随机化结果不一致
- ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
- ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
- ¥15 谁有desed数据集呀
- ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
- ¥15 关于#hadoop#的问题