热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android实现Service获取当前位置(GPS+基站)的方法

这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android实现Service获取当前位置(GPS+基站)的方法。分享给大家供大家参考。具体如下:

需求详情:

1)、Service中每隔1秒执行一次定位操作(GPS+基站)
2)、定位的结果实时显示在界面上(要求得到经度、纬度)

技术支持:

1)、获取经纬度
通过GPS+基站获取经纬度,先通过GPS来获取,如果为空改用基站进行获取–>GPS+基站(基站获取支持联通、电信、移动)。
2)、实时获取经纬度
为了达到实时获取经纬度,需在后台启动获取经纬度的Service,然后把经纬度数据通过广播发送出去,在需要的地方进行广播注册(比如在Activity中注册广播,显示在界面中)–>涉及到Service+BroadcastReceiver+Activity+Thread等知识点。

备注:本文注重实践,如有看不懂的,先去巩固下知识点,可以去看看前面相关的几篇文章。

1、CellInfo实体类–>基站信息

package com.ljq.activity;
/**
 * 基站信息
 * 
 * @author jiqinlin
 * 
 */
public class CellInfo {
 /** 基站id,用来找到基站的位置 */
 private int cellId;
 /** 移动国家码,共3位,中国为460,即imsi前3位 */
 private String mobileCountryCode="460";
 /** 移动网络码,共2位,在中国,移动的代码为00和02,联通的代码为01,电信的代码为03,即imsi第4~5位 */
 private String mobileNetworkCode="0";
 /** 地区区域码 */
 private int locationAreaCode;
 /** 信号类型[选 gsm|cdma|wcdma] */
 private String radioType="";
 public CellInfo() {
 }
 public int getCellId() {
 return cellId;
 }
 public void setCellId(int cellId) {
 this.cellId = cellId;
 }
 public String getMobileCountryCode() {
 return mobileCountryCode;
 }
 public void setMobileCountryCode(String mobileCountryCode) {
 this.mobileCountryCode = mobileCountryCode;
 }
 public String getMobileNetworkCode() {
 return mobileNetworkCode;
 }
 public void setMobileNetworkCode(String mobileNetworkCode) {
 this.mobileNetworkCode = mobileNetworkCode;
 }
 public int getLocationAreaCode() {
 return locationAreaCode;
 }
 public void setLocationAreaCode(int locationAreaCode) {
 this.locatiOnAreaCode= locationAreaCode;
 }
 public String getRadioType() {
 return radioType;
 }
 public void setRadioType(String radioType) {
 this.radioType = radioType;
 }
}

2、Gps类–>Gps封装类,用来获取经纬度

package com.ljq.activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class Gps{
 private Location location = null;
 private LocationManager locatiOnManager= null;
 private Context cOntext= null;
 /**
 * 初始化 
 * 
 * @param ctx
 */
 public Gps(Context ctx) {
 cOntext=ctx;
 locatiOnManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
 location = locationManager.getLastKnownLocation(getProvider());
 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
 }
 // 获取Location Provider
 private String getProvider() {
 // 构建位置查询条件
 Criteria criteria = new Criteria();
 // 查询精度:高
 criteria.setAccuracy(Criteria.ACCURACY_FINE);
 // 是否查询海拨:否
 criteria.setAltitudeRequired(false);
 // 是否查询方位角 : 否
 criteria.setBearingRequired(false);
 // 是否允许付费:是
 criteria.setCostAllowed(true);
 // 电量要求:低
 criteria.setPowerRequirement(Criteria.POWER_LOW);
 // 返回最合适的符合条件的provider,第2个参数为true说明 , 如果只有一个provider是有效的,则返回当前provider
 return locationManager.getBestProvider(criteria, true);
 }
 private LocationListener locatiOnListener= new LocationListener() {
 // 位置发生改变后调用
 public void onLocationChanged(Location l) {
  if(l!=null){
  location=l;
  }
 }
 // provider 被用户关闭后调用
 public void onProviderDisabled(String provider) {
  location=null;
 }
 // provider 被用户开启后调用
 public void onProviderEnabled(String provider) {
  Location l = locationManager.getLastKnownLocation(provider);
  if(l!=null){
  location=l;
  }
 }
 // provider 状态变化时调用
 public void onStatusChanged(String provider, int status, Bundle extras) {
 }
 };
 public Location getLocation(){
 return location;
 }
 public void closeLocation(){
 if(locationManager!=null){
  if(locationListener!=null){
  locationManager.removeUpdates(locationListener);
  locatiOnListener=null;
  }
  locatiOnManager=null;
 }
 }
}

3、GpsService服务类

package com.ljq.activity;
import java.util.ArrayList;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.IBinder;
import android.util.Log;
public class GpsService extends Service {
 ArrayList cellIds = null;
 private Gps gps=null;
 private boolean threadDisable=false; 
 private final static String TAG=GpsService.class.getSimpleName();
 @Override
 public void onCreate() {
 super.onCreate();
 gps=new Gps(GpsService.this);
 cellIds=UtilTool.init(GpsService.this);
 new Thread(new Runnable(){
  @Override
  public void run() {
  while (!threadDisable) { 
   try {
   Thread.sleep(1000);
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
   if(gps!=null){ //当结束服务时gps为空
   //获取经纬度
   Location location=gps.getLocation();
   //如果gps无法获取经纬度,改用基站定位获取
   if(location==null){
    Log.v(TAG, "gps location null"); 
    //2.根据基站信息获取经纬度
    try {
    location = UtilTool.callGear(GpsService.this, cellIds);
    } catch (Exception e) {
    location=null;
    e.printStackTrace();
    }
    if(location==null){
    Log.v(TAG, "cell location null"); 
    }
   }
   //发送广播
   Intent intent=new Intent();
   intent.putExtra("lat", location==null?"":location.getLatitude()+""); 
   intent.putExtra("lon", location==null?"":location.getLongitude()+""); 
   intent.setAction("com.ljq.activity.GpsService"); 
   sendBroadcast(intent);
   }
  }
  }
 }).start();
 }
 @Override
 public void onDestroy() {
 threadDisable=true;
 if(cellIds!=null&&cellIds.size()>0){
  cellIds=null;
 }
 if(gps!=null){
  gps.closeLocation();
  gps=null;
 }
 super.onDestroy();
 }
 @Override
 public IBinder onBind(Intent arg0) {
 return null;
 }
}

4、GpsActivity–>在界面上实时显示经纬度数据

package com.ljq.activity;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
public class GpsActivity extends Activity {
 private Double homeLat=26.0673834d; //宿舍纬度
 private Double homeLon=119.3119936d; //宿舍经度
 private EditText editText = null;
 private MyReceiver receiver=null; 
 private final static String TAG=GpsActivity.class.getSimpleName();
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 editText=(EditText)findViewById(R.id.editText);
 //判断GPS是否可用
 Log.i(TAG, UtilTool.isGpsEnabled((LocationManager)getSystemService(Context.LOCATION_SERVICE))+"");
 if(!UtilTool.isGpsEnabled((LocationManager)getSystemService(Context.LOCATION_SERVICE))){
  Toast.makeText(this, "GSP当前已禁用,请在您的系统设置屏幕启动。", Toast.LENGTH_LONG).show();
  Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
  startActivity(callGPSSettingIntent);
      return;
 }  
 //启动服务
 startService(new Intent(this, GpsService.class));
 //注册广播
 receiver=new MyReceiver();
 IntentFilter filter=new IntentFilter();
 filter.addAction("com.ljq.activity.GpsService");
 registerReceiver(receiver, filter);
 }
 //获取广播数据
 private class MyReceiver extends BroadcastReceiver{
 @Override
 public void onReceive(Context context, Intent intent) {
  Bundle bundle=intent.getExtras();   
  String lon=bundle.getString("lon");  
  String lat=bundle.getString("lat"); 
  if(lon!=null&&!"".equals(lon)&&lat!=null&&!"".equals(lat)){
  double distance=getDistance(Double.parseDouble(lat), 
   Double.parseDouble(lon), homeLat, homeLon);
  editText.setText("目前经纬度\n经度:"+lon+"\n纬度:"+lat+"\n离宿舍距离:"+java.lang.Math.abs(distance));
  }else{
  editText.setText("目前经纬度\n经度:"+lon+"\n纬度:"+lat);
  }
 }
 }
 @Override
 protected void onDestroy() {
 //注销服务
 unregisterReceiver(receiver);
 //结束服务,如果想让服务一直运行就注销此句
 stopService(new Intent(this, GpsService.class));
 super.onDestroy();
 }
 /**
 * 把经纬度换算成距离
 * 
 * @param lat1 开始纬度
 * @param lon1 开始经度
 * @param lat2 结束纬度
 * @param lon2 结束经度
 * @return
 */
 private double getDistance(double lat1, double lon1, double lat2, double lon2) {
 float[] results = new float[1];
 Location.distanceBetween(lat1, lon1, lat2, lon2, results);
 return results[0];
 } 
}

5、UtilTool–>工具类

package com.ljq.activity;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.widget.Toast;
public class UtilTool {
 public static boolean isGpsEnabled(LocationManager locationManager) {
 boolean isOpenGPS = locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
 boolean isOpenNetwork = locationManager.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER);
 if (isOpenGPS || isOpenNetwork) {
  return true;
 }
 return false;
 } 
  /**
   * 根据基站信息获取经纬度 
   * 
   * 原理向http://www.google.com/loc/json发送http的post请求,根据google返回的结果获取经纬度
   * 
   * @param cellIds
   * @return
   * @throws Exception 
   */
  public static Location callGear(Context ctx, ArrayList cellIds) throws Exception {
   String result="";
   JSONObject data=null;
   if (cellIds == null||cellIds.size()==0) {
   UtilTool.alert(ctx, "cell request param null");
   return null;
   };
 try {
  result = UtilTool.getResponseResult(ctx, "http://www.google.com/loc/json", cellIds);
  if(result.length() <= 1)
  return null;
  data = new JSONObject(result);
  data = (JSONObject) data.get("location");
  Location loc = new Location(LocationManager.NETWORK_PROVIDER);
  loc.setLatitude((Double) data.get("latitude"));
  loc.setLongitude((Double) data.get("longitude"));
  loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString()));
  loc.setTime(UtilTool.getUTCTime());
  return loc;
 } catch (JSONException e) {
  return null;
 } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
 } catch (ClientProtocolException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 }
 return null;
 }
  /**
   * 接收Google返回的数据格式
   * 
 * 出参:{"location":{"latitude":26.0673834,"longitude":119.3119936,
 *    "address":{"country":"中å&#155;½","country_code":"CN","region":"ç¦&#143;建ç&#156;&#129;","city":"ç¦&#143;å·&#158;å¸&#130;",
 *    "street":"äº&#148;ä¸&#128;中路","street_number":"128å&#143;·"},"accuracy":935.0},
 *    "access_token":"2:xiU8YrSifFHUAvRJ:aj9k70VJMRWo_9_G"}
 * 请求路径:http://maps.google.cn/maps/geo&#63;key=abcdefg&q=26.0673834,119.3119936
   * 
   * @param cellIds
   * @return
   * @throws UnsupportedEncodingException
   * @throws MalformedURLException
   * @throws IOException
   * @throws ProtocolException
   * @throws Exception
   */
 public static String getResponseResult(Context ctx,String path, ArrayList cellInfos)
  throws UnsupportedEncodingException, MalformedURLException,
  IOException, ProtocolException, Exception {
 String result="";
 Log.i(ctx.getApplicationContext().getClass().getSimpleName(), 
  "in param: "+getRequestParams(cellInfos));
 InputStream inStream=UtilTool.sendPostRequest(path, 
  getRequestParams(cellInfos), "UTF-8");
 if(inStream!=null){
  byte[] datas=UtilTool.readInputStream(inStream);
  if(datas!=null&&datas.length>0){
  result=new String(datas, "UTF-8");
  //Log.i(ctx.getClass().getSimpleName(), "receive result:"+result);//服务器返回的结果信息
  Log.i(ctx.getApplicationContext().getClass().getSimpleName(), 
    "google cell receive data result:"+result);
  }else{
  Log.i(ctx.getApplicationContext().getClass().getSimpleName(), 
   "google cell receive data null");
  }
 }else{
  Log.i(ctx.getApplicationContext().getClass().getSimpleName(), 
    "google cell receive inStream null");
 }
 return result;
 }
 /**
 * 拼装json请求参数,拼装基站信息
 * 
 * 入参:{'version': '1.1.0','host': 'maps.google.com','home_mobile_country_code': 460,
 *    'home_mobile_network_code': 14136,'radio_type': 'cdma','request_address': true,
 *    'address_language': 'zh_CN','cell_towers':[{'cell_id': '12835','location_area_code': 6,
 *    'mobile_country_code': 460,'mobile_network_code': 14136,'age': 0}]}
 * @param cellInfos
 * @return
 */
 public static String getRequestParams(List cellInfos){
 StringBuffer sb=new StringBuffer("");
 sb.append("{");
 if(cellInfos!=null&&cellInfos.size()>0){
  sb.append("'version': '1.1.0',"); //google api 版本[必]
  sb.append("'host': 'maps.google.com',"); //服务器域名[必]
  sb.append("'home_mobile_country_code': "+cellInfos.get(0).getMobileCountryCode()+","); //移动用户所属国家代号[选 中国460]
  sb.append("'home_mobile_network_code': "+cellInfos.get(0).getMobileNetworkCode()+","); //移动系统号码[默认0]
  sb.append("'radio_type': '"+cellInfos.get(0).getRadioType()+"',"); //信号类型[选 gsm|cdma|wcdma]
  sb.append("'request_address': true,"); //是否返回数据[必]
  sb.append("'address_language': 'zh_CN',"); //反馈数据语言[选 中国 zh_CN]
  sb.append("'cell_towers':["); //移动基站参数对象[必]
  for(CellInfo cellInfo:cellInfos){
  sb.append("{");
  sb.append("'cell_id': '"+cellInfo.getCellId()+"',"); //基站ID[必]
  sb.append("'location_area_code': "+cellInfo.getLocationAreaCode()+","); //地区区域码[必]
  sb.append("'mobile_country_code': "+cellInfo.getMobileCountryCode()+","); 
  sb.append("'mobile_network_code': "+cellInfo.getMobileNetworkCode()+",");
  sb.append("'age': 0"); //使用好久的数据库[选 默认0表示使用最新的数据库]
  sb.append("},");
  }
  sb.deleteCharAt(sb.length()-1);
  sb.append("]");
 }
 sb.append("}");
 return sb.toString();
 }
 /**
 * 获取UTC时间
 * 
 * UTC + 时区差 = 本地时间(北京为东八区)
 * 
 * @return
 */
 public static long getUTCTime() { 
   //取得本地时间
    Calendar cal = Calendar.getInstance(Locale.CHINA);
    //取得时间偏移量
    int zOneOffset= cal.get(java.util.Calendar.ZONE_OFFSET); 
    //取得夏令时差
    int dstOffset = cal.get(java.util.Calendar.DST_OFFSET); 
    //从本地时间里扣除这些差量,即可以取得UTC时间
    cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); 
    return cal.getTimeInMillis();
  }
 /**
 * 初始化,记得放在onCreate()方法里初始化,获取基站信息
 * 
 * @return
 */
 public static ArrayList init(Context ctx) {
 ArrayList cellInfos = new ArrayList();
 TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
 //网络制式
 int type = tm.getNetworkType();
   /**
   * 获取SIM卡的IMSI码
   * SIM卡唯一标识:IMSI 国际移动用户识别码(IMSI:International Mobile Subscriber Identification Number)是区别移动用户的标志,
   * 储存在SIM卡中,可用于区别移动用户的有效信息。IMSI由MCC、MNC、MSIN组成,其中MCC为移动国家号码,由3位数字组成,
   * 唯一地识别移动客户所属的国家,我国为460;MNC为网络id,由2位数字组成,
   * 用于识别移动客户所归属的移动网络,中国移动为00,中国联通为01,中国电信为03;MSIN为移动客户识别码,采用等长11位数字构成。
   * 唯一地识别国内GSM移动通信网中移动客户。所以要区分是移动还是联通,只需取得SIM卡中的MNC字段即可
  */
 String imsi = tm.getSubscriberId(); 
 alert(ctx, "imsi: "+imsi);
 //为了区分移动、联通还是电信,推荐使用imsi来判断(万不得己的情况下用getNetworkType()判断,比如imsi为空时)
 if(imsi!=null&&!"".equals(imsi)){ 
  alert(ctx, "imsi");
  if (imsi.startsWith("46000") || imsi.startsWith("46002")) {// 因为移动网络编号46000下的IMSI已经用完,所以虚拟了一个46002编号,134/159号段使用了此编号
  // 中国移动
  mobile(cellInfos, tm);
  } else if (imsi.startsWith("46001")) {
  // 中国联通
  union(cellInfos, tm);
  } else if (imsi.startsWith("46003")) {
  // 中国电信
  cdma(cellInfos, tm);
  }  
 }else{
  alert(ctx, "type");
  // 在中国,联通的3G为UMTS或HSDPA,电信的3G为EVDO
  // 在中国,移动的2G是EGDE,联通的2G为GPRS,电信的2G为CDMA
  // String OperatorName = tm.getNetworkOperatorName(); 
  //中国电信 
  if (type == TelephonyManager.NETWORK_TYPE_EVDO_A 
   || type == TelephonyManager.NETWORK_TYPE_EVDO_0
   || type == TelephonyManager.NETWORK_TYPE_CDMA 
   || type ==TelephonyManager.NETWORK_TYPE_1xRTT){
  cdma(cellInfos, tm);
  }
  //移动(EDGE(2.75G)是GPRS(2.5G)的升级版,速度比GPRS要快。目前移动基本在国内升级普及EDGE,联通则在大城市部署EDGE。)
  else if(type == TelephonyManager.NETWORK_TYPE_EDGE
   || type == TelephonyManager.NETWORK_TYPE_GPRS ){
  mobile(cellInfos, tm);
  }
  //联通(EDGE(2.75G)是GPRS(2.5G)的升级版,速度比GPRS要快。目前移动基本在国内升级普及EDGE,联通则在大城市部署EDGE。)
  else if(type == TelephonyManager.NETWORK_TYPE_GPRS
   ||type == TelephonyManager.NETWORK_TYPE_EDGE
   ||type == TelephonyManager.NETWORK_TYPE_UMTS
   ||type == TelephonyManager.NETWORK_TYPE_HSDPA){
  union(cellInfos, tm);
  }
 }
 return cellInfos;
 }
 /**
 * 电信
 * 
 * @param cellInfos
 * @param tm
 */
 private static void cdma(ArrayList cellInfos, TelephonyManager tm) {
 CdmaCellLocation location = (CdmaCellLocation) tm.getCellLocation();
 CellInfo info = new CellInfo();
 info.setCellId(location.getBaseStationId());
 info.setLocationAreaCode(location.getNetworkId());
 info.setMobileNetworkCode(String.valueOf(location.getSystemId()));
 info.setMobileCountryCode(tm.getNetworkOperator().substring(0, 3));
 info.setRadioType("cdma");
 cellInfos.add(info);
 //前面获取到的都是单个基站的信息,接下来再获取周围邻近基站信息以辅助通过基站定位的精准性
 // 获得邻近基站信息
 List list = tm.getNeighboringCellInfo();
 int size = list.size();
 for (int i = 0; i  cellInfos,
  TelephonyManager tm) {
 GsmCellLocation location = (GsmCellLocation)tm.getCellLocation(); 
 CellInfo info = new CellInfo();
 info.setCellId(location.getCid());
 info.setLocationAreaCode(location.getLac());
 info.setMobileNetworkCode(tm.getNetworkOperator().substring(3, 5));
 info.setMobileCountryCode(tm.getNetworkOperator().substring(0, 3));
 info.setRadioType("gsm");
 cellInfos.add(info);
 //前面获取到的都是单个基站的信息,接下来再获取周围邻近基站信息以辅助通过基站定位的精准性
 // 获得邻近基站信息
 List list = tm.getNeighboringCellInfo();
 int size = list.size();
 for (int i = 0; i  cellInfos, TelephonyManager tm) {
 GsmCellLocation location = (GsmCellLocation)tm.getCellLocation(); 
 CellInfo info = new CellInfo();
 //经过测试,获取联通数据以下两行必须去掉,否则会出现错误,错误类型为JSON Parsing Error
 //info.setMobileNetworkCode(tm.getNetworkOperator().substring(3, 5)); 
 //info.setMobileCountryCode(tm.getNetworkOperator().substring(0, 3));
 info.setCellId(location.getCid());
 info.setLocationAreaCode(location.getLac());
 info.setMobileNetworkCode("");
 info.setMobileCountryCode("");
 info.setRadioType("gsm");
 cellInfos.add(info);
 //前面获取到的都是单个基站的信息,接下来再获取周围邻近基站信息以辅助通过基站定位的精准性
 // 获得邻近基站信息
 List list = tm.getNeighboringCellInfo();
 int size = list.size();
 for (int i = 0; i xml数据 application/x-Javascript->json对象 application/x-www-form-urlencoded->表单数据
 conn.setRequestProperty("Content-Type", "application/x-Javascript; charset="+ encoding);
 conn.setRequestProperty("Content-Length", String.valueOf(data.length));
 conn.setConnectTimeout(5 * 1000);
 OutputStream outStream = conn.getOutputStream();
 outStream.write(data);
 outStream.flush();
 outStream.close();
 if(conn.getResponseCode()==200)
  return conn.getInputStream();
 return null;
 }
 /**
 * 发送get请求
 * 
 * @param path 请求路径
 * @return
 * @throws Exception
 */
 public static String sendGetRequest(String path) throws Exception {
 URL url = new URL(path);
 HttpURLConnection cOnn= (HttpURLConnection) url.openConnection();
 conn.setConnectTimeout(5 * 1000);
 conn.setRequestMethod("GET");
 InputStream inStream = conn.getInputStream();
 byte[] data = readInputStream(inStream);
 String result = new String(data, "UTF-8");
 return result;
 }
 /**
 * 从输入流中读取数据
 * @param inStream
 * @return
 * @throws Exception
 */
 public static byte[] readInputStream(InputStream inStream) throws Exception{
 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
 byte[] buffer = new byte[1024];
 int len = 0;
 while( (len = inStream.read(buffer)) !=-1 ){
  outStream.write(buffer, 0, len);
 }
 byte[] data = outStream.toByteArray();//网页的二进制数据
 outStream.close();
 inStream.close();
 return data;
 }
}

6、main.xml–>布局文件

<&#63;xml version="1.0" encoding="utf-8"&#63;>

  

7、清单文件

<&#63;xml version="1.0" encoding="utf-8"&#63;>

 
 
  
  
  
  
 
 
 
 
 
 
 
 
 

效果如下:

希望本文所述对大家的Android程序设计有所帮助。


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • Java如何导入和导出Excel文件的方法和步骤详解
    本文详细介绍了在SpringBoot中使用Java导入和导出Excel文件的方法和步骤,包括添加操作Excel的依赖、自定义注解等。文章还提供了示例代码,并将代码上传至GitHub供访问。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 本文介绍了一个React Native新手在尝试将数据发布到服务器时遇到的问题,以及他的React Native代码和服务器端代码。他使用fetch方法将数据发送到服务器,但无法在服务器端读取/获取发布的数据。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • Apache Shiro 身份验证绕过漏洞 (CVE202011989) 详细解析及防范措施
    本文详细解析了Apache Shiro 身份验证绕过漏洞 (CVE202011989) 的原理和影响,并提供了相应的防范措施。Apache Shiro 是一个强大且易用的Java安全框架,常用于执行身份验证、授权、密码和会话管理。在Apache Shiro 1.5.3之前的版本中,与Spring控制器一起使用时,存在特制请求可能导致身份验证绕过的漏洞。本文还介绍了该漏洞的具体细节,并给出了防范该漏洞的建议措施。 ... [详细]
  • Sleuth+zipkin链路追踪SpringCloud微服务的解决方案
    在庞大的微服务群中,随着业务扩展,微服务个数增多,系统调用链路复杂化。Sleuth+zipkin是解决SpringCloud微服务定位和追踪的方案。通过TraceId将不同服务调用的日志串联起来,实现请求链路跟踪。通过Feign调用和Request传递TraceId,将整个调用链路的服务日志归组合并,提供定位和追踪的功能。 ... [详细]
author-avatar
小宇宇宙2502862273
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有