Android(安卓)tips3
16lz
2021-01-24
1 intent中调用电话本
Intent intent = new Intent();
// 设置Intent Action属性
intent.setAction(Intent.ACTION_GET_CONTENT);
// 设置Intent Type 属性
intent.setType("vnd.android.cursor.item/phone");
// 启动Activity
startActivity(intent);
2 Intent回到home界面
// 实例化Intent
Intent i = new Intent();
// 添加Action属性
i.setAction(Intent.ACTION_MAIN);
// 添加Category属性
i.addCategory(Intent.CATEGORY_HOME);
// 启动Activity
startActivity(i);
3 常见的intent的解析
1) // 查看_id 为1的用户电话信息
data = "content://contacts/people/1";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
2) 编辑_id 为1的用户电话信息
data = "content://contacts/people/1";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_EDIT);
intent.setData(uri);
startActivity(intent);
3) // 显示拨打电话界面
data = "tel:13800138000";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_DIAL);
intent.setData(uri);
startActivity(intent);
4) 直接打电话
data = "tel:13800138000";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_CALL);
intent.setData(uri);
5) // 访问浏览器
data = "http://www.google.com";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
6) 访问地图
data = "geo:39.92,116.46";
uri = Uri.parse(data);
intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
4 ) preference
例子保存临时短信
比如在oncreate中:
SharedPreferences pre = getSharedPreferences(TEMP_SMS, MODE_WORLD_READABLE);
String content = pre.getString("sms_content", "");
myEditText.setText(content);
onstop比如接电话时:
super.onStop();
SharedPreferences.Editor editor = getSharedPreferences(TEMP_SMS, MODE_WORLD_WRITEABLE).edit();
editor.putString("sms_content", myEditText.getText().toString());
editor.commit();
实际上是保存在/data/data/package/shared_prefs/下的XML文件
5)bitmap读网上一个URL的地址,然后
String urlStr = "http://xxxxx/zs.jpg";
try {
URL url = new URL(urlStr);
// 1. 直接使用URL获得输入流
//InputStream in = url.openStream();
// 2. 获得URLconnection
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
// 3. 如果是HTTP协议可以使用HttpURLConnection
//HttpURLConnection httpConn = (HttpsURLConnection)conn;
//in = httpConn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(in);
imageView.setImageBitmap(bm);
} catch (Exception e) {
e.printStackTrace();
}
6 android中HTTP URL方式访问服务端的工具类及使用
public class HttpUtil {
// 基础URL
public static final String BASE_URL="http://10.0.2.2:8085/test/";
// 获得Get请求对象request
public static HttpGet getHttpGet(String url){
HttpGet request = new HttpGet(url);
return request;
}
// 获得Post请求对象request
public static HttpPost getHttpPost(String url){
HttpPost request = new HttpPost(url);
return request;
}
// 根据请求获得响应对象response
public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException,
IOException{
HttpResponse response = new DefaultHttpClient().execute(request);
return response;
}
// 根据请求获得响应对象response
public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException,
IOException{
HttpResponse response = new DefaultHttpClient().execute(request);
return response;
}
// 发送Post请求,获得响应查询结果
public static String queryStringForPost(String url){
// 根据url获得HttpPost对象
HttpPost request = HttpUtil.getHttpPost(url);
String result = null;
try {
// 获得响应对象
HttpResponse response = HttpUtil.getHttpResponse(request);
// 判断是否请求成功
if(response.getStatusLine().getStatusCode()==200){
// 获得响应
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}
// 获得响应查询结果
public static String queryStringForPost(HttpPost request){
String result = null;
try {
// 获得响应对象
HttpResponse response = HttpUtil.getHttpResponse(request);
// 判断是否请求成功
if(response.getStatusLine().getStatusCode()==200){
// 获得响应
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}
// 发送Get请求,获得响应查询结果
public static String queryStringForGet(String url){
// 获得HttpGet对象
HttpGet request = HttpUtil.getHttpGet(url);
String result = null;
try {
// 获得响应对象
HttpResponse response = HttpUtil.getHttpResponse(request);
// 判断是否请求成功
if(response.getStatusLine().getStatusCode()==200){
// 获得响应
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}
使用:String username = userEditText.getText().toString();
// 获得密码
String pwd = pwdEditText.getText().toString();
// 获得登录结果
String result=query(username,pwd);
// 根据用户名称密码查询
private String query(String account,String password){
// 查询参数
String queryString = "account="+account+"&password="+password;
// url
String url = HttpUtil.BASE_URL+"servlet/LoginServlet?"+queryString;
// 查询返回结果
return HttpUtil.queryStringForPost(url);
将参数提交给服务端:使用apache http client:
List<NameValuePair> params = new ArrayList<NameValuePair>();
// 添加请求参数
params.add(new BasicNameValuePair("orderTime", orderTime));
params.add(new BasicNameValuePair("userId", userId));
params.add(new BasicNameValuePair("tableId", tableId));
params.add(new BasicNameValuePair("personNum", personNum));
UrlEncodedFormEntity entity1=null;
try {
entity1 = new UrlEncodedFormEntity(params,HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 请求服务器url
String url = HttpUtil.BASE_URL+"servlet/StartTableServlet";
// 获得请求对象HttpPost
HttpPost request = HttpUtil.getHttpPost(url);
// 设置查询参数
request.setEntity(entity1);
// 获得响应结果
String result= HttpUtil.queryStringForPost(request);
Intent intent = new Intent();
// 设置Intent Action属性
intent.setAction(Intent.ACTION_GET_CONTENT);
// 设置Intent Type 属性
intent.setType("vnd.android.cursor.item/phone");
// 启动Activity
startActivity(intent);
2 Intent回到home界面
// 实例化Intent
Intent i = new Intent();
// 添加Action属性
i.setAction(Intent.ACTION_MAIN);
// 添加Category属性
i.addCategory(Intent.CATEGORY_HOME);
// 启动Activity
startActivity(i);
3 常见的intent的解析
1) // 查看_id 为1的用户电话信息
data = "content://contacts/people/1";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
2) 编辑_id 为1的用户电话信息
data = "content://contacts/people/1";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_EDIT);
intent.setData(uri);
startActivity(intent);
3) // 显示拨打电话界面
data = "tel:13800138000";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_DIAL);
intent.setData(uri);
startActivity(intent);
4) 直接打电话
data = "tel:13800138000";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_CALL);
intent.setData(uri);
5) // 访问浏览器
data = "http://www.google.com";
uri = Uri.parse(data);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
6) 访问地图
data = "geo:39.92,116.46";
uri = Uri.parse(data);
intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
4 ) preference
例子保存临时短信
比如在oncreate中:
SharedPreferences pre = getSharedPreferences(TEMP_SMS, MODE_WORLD_READABLE);
String content = pre.getString("sms_content", "");
myEditText.setText(content);
onstop比如接电话时:
super.onStop();
SharedPreferences.Editor editor = getSharedPreferences(TEMP_SMS, MODE_WORLD_WRITEABLE).edit();
editor.putString("sms_content", myEditText.getText().toString());
editor.commit();
实际上是保存在/data/data/package/shared_prefs/下的XML文件
5)bitmap读网上一个URL的地址,然后
String urlStr = "http://xxxxx/zs.jpg";
try {
URL url = new URL(urlStr);
// 1. 直接使用URL获得输入流
//InputStream in = url.openStream();
// 2. 获得URLconnection
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
// 3. 如果是HTTP协议可以使用HttpURLConnection
//HttpURLConnection httpConn = (HttpsURLConnection)conn;
//in = httpConn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(in);
imageView.setImageBitmap(bm);
} catch (Exception e) {
e.printStackTrace();
}
6 android中HTTP URL方式访问服务端的工具类及使用
public class HttpUtil {
// 基础URL
public static final String BASE_URL="http://10.0.2.2:8085/test/";
// 获得Get请求对象request
public static HttpGet getHttpGet(String url){
HttpGet request = new HttpGet(url);
return request;
}
// 获得Post请求对象request
public static HttpPost getHttpPost(String url){
HttpPost request = new HttpPost(url);
return request;
}
// 根据请求获得响应对象response
public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException,
IOException{
HttpResponse response = new DefaultHttpClient().execute(request);
return response;
}
// 根据请求获得响应对象response
public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException,
IOException{
HttpResponse response = new DefaultHttpClient().execute(request);
return response;
}
// 发送Post请求,获得响应查询结果
public static String queryStringForPost(String url){
// 根据url获得HttpPost对象
HttpPost request = HttpUtil.getHttpPost(url);
String result = null;
try {
// 获得响应对象
HttpResponse response = HttpUtil.getHttpResponse(request);
// 判断是否请求成功
if(response.getStatusLine().getStatusCode()==200){
// 获得响应
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}
// 获得响应查询结果
public static String queryStringForPost(HttpPost request){
String result = null;
try {
// 获得响应对象
HttpResponse response = HttpUtil.getHttpResponse(request);
// 判断是否请求成功
if(response.getStatusLine().getStatusCode()==200){
// 获得响应
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}
// 发送Get请求,获得响应查询结果
public static String queryStringForGet(String url){
// 获得HttpGet对象
HttpGet request = HttpUtil.getHttpGet(url);
String result = null;
try {
// 获得响应对象
HttpResponse response = HttpUtil.getHttpResponse(request);
// 判断是否请求成功
if(response.getStatusLine().getStatusCode()==200){
// 获得响应
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
return null;
}
使用:String username = userEditText.getText().toString();
// 获得密码
String pwd = pwdEditText.getText().toString();
// 获得登录结果
String result=query(username,pwd);
// 根据用户名称密码查询
private String query(String account,String password){
// 查询参数
String queryString = "account="+account+"&password="+password;
// url
String url = HttpUtil.BASE_URL+"servlet/LoginServlet?"+queryString;
// 查询返回结果
return HttpUtil.queryStringForPost(url);
将参数提交给服务端:使用apache http client:
List<NameValuePair> params = new ArrayList<NameValuePair>();
// 添加请求参数
params.add(new BasicNameValuePair("orderTime", orderTime));
params.add(new BasicNameValuePair("userId", userId));
params.add(new BasicNameValuePair("tableId", tableId));
params.add(new BasicNameValuePair("personNum", personNum));
UrlEncodedFormEntity entity1=null;
try {
entity1 = new UrlEncodedFormEntity(params,HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 请求服务器url
String url = HttpUtil.BASE_URL+"servlet/StartTableServlet";
// 获得请求对象HttpPost
HttpPost request = HttpUtil.getHttpPost(url);
// 设置查询参数
request.setEntity(entity1);
// 获得响应结果
String result= HttpUtil.queryStringForPost(request);
更多相关文章
- android 中调用ajax的问题
- android中自定义view构造函数ContentItemView(Context context,
- android 自定义属性
- Android按钮设置文字变色
- TextView设置android:textAllCaps="true"带来的问题
- Android(安卓)5.0以上版本去掉Button自带阴影效果的方法
- android animator
- java.net.UnknownServiceException: CLEARTEXT communication to
- Android的资源获取的相关方法