当前位置: 编程技术>移动开发
本页文章导读:
▪SharedPreferences存与取 SharedPreferences存与取,
android中常用有五种存储,今天记录的是最简单一种。就一个矿建里面有着变化不算大的几步。。。 所谓存储就是先存后取。1。先存:SharedPreferences set_choose = .........
▪ byte[]将图片变为字节存入流中,然后Bit地图将流读出,显示图片 byte[]将图片变为字节存入流中,然后Bitmap将流读出,显示图片
这几天遇到一个问题,绊住好长时间,为了以后有据可查,现在记录下来。问题:选中图片还会用在别的地方,所以先把图.........
▪ 施用基站、wifi实现定位 使用基站、wifi实现定位
转载请注明出处 android可以借助于gps实现定位,但是很多地方是使用gps无法定位比如在室内,而且gps定位的话速度慢。 那么如何克服这样的缺点.........
[1]SharedPreferences存与取
来源: 互联网 发布时间: 2014-02-18
SharedPreferences存与取,
android中常用有五种存储,今天记录的是最简单一种。就一个矿建里面有着变化不算大的几步。。。
所谓存储就是先存后取。
1。先存:
SharedPreferences set_choose = getSharedPreferences(PHONE_PREF,0);
set_choose.edit().putString(CHOOSEIMAGE,"true").commit();
PHONE_PREF是专门为存储而建的文件的文件名。
putString();是存放键值对的。而且也有很多类型的存储,如:putBoolean,putLong,putInt。如果用得到的都可以尝试一下。
2。后取:
SharedPreferences set_choose = getSharedPreferences(PHONE_PREF,0);
String choose = set_choose.getString(CHOOSE_ISTRUE,"");
在此要注意的便是上下一致。即,文件名一致,键一致,数据类型也要一直。
set_choose.getString(CHOOSE_ISTRUE,"");
键后面的双引号中为null,意思便是:当去这个键而没有对应东西的时候,取出来的是null。当然这个值也可以是别的。但是再次提醒。一定要数据类型一致。
android中常用有五种存储,今天记录的是最简单一种。就一个矿建里面有着变化不算大的几步。。。
所谓存储就是先存后取。
1。先存:
SharedPreferences set_choose = getSharedPreferences(PHONE_PREF,0);
set_choose.edit().putString(CHOOSEIMAGE,"true").commit();
PHONE_PREF是专门为存储而建的文件的文件名。
putString();是存放键值对的。而且也有很多类型的存储,如:putBoolean,putLong,putInt。如果用得到的都可以尝试一下。
2。后取:
SharedPreferences set_choose = getSharedPreferences(PHONE_PREF,0);
String choose = set_choose.getString(CHOOSE_ISTRUE,"");
在此要注意的便是上下一致。即,文件名一致,键一致,数据类型也要一直。
set_choose.getString(CHOOSE_ISTRUE,"");
键后面的双引号中为null,意思便是:当去这个键而没有对应东西的时候,取出来的是null。当然这个值也可以是别的。但是再次提醒。一定要数据类型一致。
[2] byte[]将图片变为字节存入流中,然后Bit地图将流读出,显示图片
来源: 互联网 发布时间: 2014-02-18
byte[]将图片变为字节存入流中,然后Bitmap将流读出,显示图片
这几天遇到一个问题,绊住好长时间,为了以后有据可查,现在记录下来。
问题:选中图片还会用在别的地方,所以先把图片以某种方式保存起来,然后到了用的时候再将图片取出来用。
这个的做法比较容易想到的就是流来存然后取;
1。首先就是先把图片读成字节,然后将字节保存在流中。
//存储图片,变成byte形式,便于在上面取出
byte[] buffer=new byte[1024];
//等号后面是图片的路径
InputStream in= cr.openInputStream(uri);
//下面是将图片读成字节,而且是以1024为单位读的
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//定义整数类型对象
int number;
//利用循环方式将图片读完整
while((number=in.read(buffer, 0, buffer.length))>0)
{
baos.write(buffer, 0, number);
}
//将字节保存到byte[]数组对象中
byte[] reallyByte=baos.toByteArray();
//byte存储了图片,然后再用流存储图片,定义文件名和访问权限
OutputStream os = openFileOutput("file.txt",Activity.MODE_PRIVATE);
os.write(reallyByte);
os.close();
//log一下,检验是否真的有字节存放到了指定文件夹中
Log.v("MyPhoneText","reallyByte.length="+reallyByte.length);
2。取出来
现在用到了再android中很重要的一个点Bitmap。一定记得要多留意此小东西的用法。前途无量。
还要提醒下的是,要放在try中存储和读取流
try{
//定义BitmapFactory工厂的decodeStream方法,这个工厂方法很多。继续学习。方法内为存储图片的那个流文件的路径
Bitmap bitmap = BitmapFactory.decodeStream(this.openFileInput("file.txt"));
//取出的bitmap对象就直接可以用来放在ImageView中显示
imageShow.setImageBitmap(bitmap);
}catch(Exception e){
e.printStackTrace();
}
这几天遇到一个问题,绊住好长时间,为了以后有据可查,现在记录下来。
问题:选中图片还会用在别的地方,所以先把图片以某种方式保存起来,然后到了用的时候再将图片取出来用。
这个的做法比较容易想到的就是流来存然后取;
1。首先就是先把图片读成字节,然后将字节保存在流中。
//存储图片,变成byte形式,便于在上面取出
byte[] buffer=new byte[1024];
//等号后面是图片的路径
InputStream in= cr.openInputStream(uri);
//下面是将图片读成字节,而且是以1024为单位读的
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//定义整数类型对象
int number;
//利用循环方式将图片读完整
while((number=in.read(buffer, 0, buffer.length))>0)
{
baos.write(buffer, 0, number);
}
//将字节保存到byte[]数组对象中
byte[] reallyByte=baos.toByteArray();
//byte存储了图片,然后再用流存储图片,定义文件名和访问权限
OutputStream os = openFileOutput("file.txt",Activity.MODE_PRIVATE);
os.write(reallyByte);
os.close();
//log一下,检验是否真的有字节存放到了指定文件夹中
Log.v("MyPhoneText","reallyByte.length="+reallyByte.length);
2。取出来
现在用到了再android中很重要的一个点Bitmap。一定记得要多留意此小东西的用法。前途无量。
还要提醒下的是,要放在try中存储和读取流
try{
//定义BitmapFactory工厂的decodeStream方法,这个工厂方法很多。继续学习。方法内为存储图片的那个流文件的路径
Bitmap bitmap = BitmapFactory.decodeStream(this.openFileInput("file.txt"));
//取出的bitmap对象就直接可以用来放在ImageView中显示
imageShow.setImageBitmap(bitmap);
}catch(Exception e){
e.printStackTrace();
}
[3] 施用基站、wifi实现定位
来源: 互联网 发布时间: 2014-02-18
使用基站、wifi实现定位
转载请注明出处
android可以借助于gps实现定位,但是很多地方是使用gps无法定位比如在室内,而且gps定位的话速度慢。
那么如何克服这样的缺点使得应用程序在室内也可以定位呢?办法是有的借助于基站和wifi进行定位。具体的细节可参考:
http://code.google.com/intl/zh-CN/apis/gears/geolocation_network_protocol.html
下面的代码实现了定位的大致功能
CellIDInfo.java 封装了cellid的信息
WifiInfo.java 封装了wifi的信息
CellIDInfoManager.java 可获取所有的CellIDInfo
WifiInfoManager.java 可获取wifi的信息,目前我只取了当前连接的wifi,没有获取所有能扫描到的wifi信息。
调用google gears的方法,该方法调用gears来获取经纬度
目前已经测试过中国移动、联通的卡,以及测试过中兴的MIC,都可以准确定位。
不支持的是cdma,将cdma的数据传入到gears后返回的经纬度显示是在美国。
提外话,将gps和基站、wifi三者定位结合的话效果更好。基站的定位没有wifi准确,gps的定位速度比基站、wifi都来得更慢。
可以做啊,做一个app放到别人的手机里,然后她每次更隔一段时间更新位置或者说更新经纬度的时候,发邮件或者sms给你。
转载请注明出处
android可以借助于gps实现定位,但是很多地方是使用gps无法定位比如在室内,而且gps定位的话速度慢。
那么如何克服这样的缺点使得应用程序在室内也可以定位呢?办法是有的借助于基站和wifi进行定位。具体的细节可参考:
http://code.google.com/intl/zh-CN/apis/gears/geolocation_network_protocol.html
下面的代码实现了定位的大致功能
CellIDInfo.java 封装了cellid的信息
public class CellIDInfo {
public int cellId;
public String mobileCountryCode;
public String mobileNetworkCode;
public int locationAreaCode;
public String radioType;
public CellIDInfo(){}
}
WifiInfo.java 封装了wifi的信息
public class WifiInfo {
public String mac;
public WifiInfo(){}
}CellIDInfoManager.java 可获取所有的CellIDInfo
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.telephony.NeighboringCellInfo;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
public class CellIDInfoManager {
private TelephonyManager manager;
private PhoneStateListener listener;
private GsmCellLocation gsm;
private CdmaCellLocation cdma;
int lac;
String current_ci,mcc, mnc;
public CellIDInfoManager(){}
public ArrayList<CellIDInfo> getCellIDInfo(Context context){
manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
listener = new PhoneStateListener();
manager.listen(listener, 0);
ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
CellIDInfo currentCell = new CellIDInfo();
int type = manager.getNetworkType();
if (type == TelephonyManager.NETWORK_TYPE_GPRS || type ==TelephonyManager.NETWORK_TYPE_EDGE
|| type ==TelephonyManager.NETWORK_TYPE_HSDPA) {
gsm = ((GsmCellLocation) manager.getCellLocation());
if (gsm == null) return null;
lac = gsm.getLac();
mcc = manager.getNetworkOperator().substring(0, 3);
mnc = manager.getNetworkOperator().substring(3, 5);
currentCell.cellId = gsm.getCid();
currentCell.mobileCountryCode = mcc;
currentCell.mobileNetworkCode = mnc;
currentCell.locationAreaCode = lac;
currentCell.radioType = "gsm";
CellID.add(currentCell);
List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();
int size = list.size();
for (int i = 0; i < size; i++) {
CellIDInfo info = new CellIDInfo();
info.cellId = list.get(i).getCid();
info.mobileCountryCode = mcc;
info.mobileCountryCode = mnc;
info.locationAreaCode = lac;
CellID.add(info);
}
return CellID;
} else if (type == TelephonyManager.NETWORK_TYPE_CDMA || type ==TelephonyManager.NETWORK_TYPE_1xRTT) {
cdma = ((CdmaCellLocation) manager.getCellLocation());
if (cdma == null) return null;
if ("460".equals(manager.getSimOperator().substring(0, 3)))
return null;
}
return null;
}
}WifiInfoManager.java 可获取wifi的信息,目前我只取了当前连接的wifi,没有获取所有能扫描到的wifi信息。
import java.util.ArrayList;
import android.content.Context;
import android.net.wifi.WifiManager;
public class WifiInfoManager {
WifiManager wm;
public WifiInfoManager(){}
public ArrayList getWifiInfo(Context context){
ArrayList<WifiInfo> wifi = new ArrayList();
wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = new WifiInfo();
info.mac = wm.getConnectionInfo().getBSSID();
wifi.add(info);
return wifi;
}
}调用google gears的方法,该方法调用gears来获取经纬度
private Location callGear(ArrayList<WifiInfo> wifi,
ArrayList<CellIDInfo> cellID) {
if (cellID == null) return null;
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://www.google.com/loc/json");
JSONObject holder = new JSONObject();
try {
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode);
holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode);
holder.put("radio_type", cellID.get(0).radioType);
holder.put("request_address", true);
if ("460".equals(cellID.get(0).mobileCountryCode))
holder.put("address_language", "zh_CN");
else
holder.put("address_language", "en_US");
JSONObject data,current_data;
JSONArray array = new JSONArray();
current_data = new JSONObject();
current_data.put("cell_id", cellID.get(0).cellId);
current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode);
current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);
current_data.put("age", 0);
array.put(current_data);
if (cellID.size() > 2) {
for (int i = 1; i < cellID.size(); i++) {
data = new JSONObject();
data.put("cell_id", cellID.get(i).cellId);
data.put("location_area_code", cellID.get(0).locationAreaCode);
data.put("mobile_country_code", cellID.get(0).mobileCountryCode);
data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);
data.put("age", 0);
array.put(data);
}
}
holder.put("cell_towers", array);
if (wifi.get(0).mac != null) {
data = new JSONObject();
data.put("mac_address", wifi.get(0).mac);
data.put("signal_strength", 8);
data.put("age", 0);
array = new JSONArray();
array.put(data);
holder.put("wifi_towers", array);
}
StringEntity se = new StringEntity(holder.toString());
Log.e("Location send", holder.toString());
post.setEntity(se);
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while (result != null) {
Log.e("Locaiton reseive", result);
sb.append(result);
result = br.readLine();
}
data = new JSONObject(sb.toString());
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(AppUtil.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;
}目前已经测试过中国移动、联通的卡,以及测试过中兴的MIC,都可以准确定位。
不支持的是cdma,将cdma的数据传入到gears后返回的经纬度显示是在美国。
提外话,将gps和基站、wifi三者定位结合的话效果更好。基站的定位没有wifi准确,gps的定位速度比基站、wifi都来得更慢。
1 楼
superhanliu
2010-12-02
如果能定位别人就爽了。
2 楼
fengzhizi715
2010-12-02
superhanliu 写道
如果能定位别人就爽了。
可以做啊,做一个app放到别人的手机里,然后她每次更隔一段时间更新位置或者说更新经纬度的时候,发邮件或者sms给你。
3 楼
大柳树
2011-07-11
没有例子吗?
4 楼
llyrike
2012-05-11
google不是自带了定位的api吗?网络定位也在其中吧,为什么还要自己写这么多呢,设置一个监听不就得了?请指示
最新技术文章: