当前位置: 编程技术>移动开发
本页文章导读:
▪关于银幕解锁的实例 关于屏幕解锁的实例
AndroidManifest.xml加两个权限 <uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission><uses-permission android:name="android.permission.WAKE_LOCK"></uses-permissio.........
▪ 批改launcher的抽屉图标 修改launcher的抽屉图标
1、 准备3张图片
2、找到res/layout-port/Launcher.xml
3) 找到Launcher.xml中的<com.zwq.launcher.HandleView>标签,这个就是显示handle的地方,看它的src和background属性分别引用了哪.........
▪ urlconnection跟httpclient urlconnection和httpclient
urlconnection:String urlAddress = "http://192.168.1.102:8080/AndroidServer/login.do";
URL url;
HttpURLConnection uRLConnection;
public UrlConnectionToServer(){
}
public String doGet(String username,String passwor.........
[1]关于银幕解锁的实例
来源: 互联网 发布时间: 2014-02-18
关于屏幕解锁的实例
AndroidManifest.xml加两个权限
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
Android屏幕解锁:
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("");
keyguardLock.disableKeyguard();
Android 点亮屏幕:
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
WakeLock mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "SimpleTimer");
mWakelock.acquire();
//...
mWakelock.release();//关闭
import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;
public class ScreenLockManager {
private Context mContext;
private KeyguardManager.KeyguardLock mKeyguardLock;
private boolean isScreenLock;
private PowerManager.WakeLock mWakelock;
public ScreenLockManager(Context context) {
mContext = context;
}
public void getUnlock() {
// acquire wake lock
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "SimpleTimer");
mWakelock.acquire();
// unlock screen
KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = km.newKeyguardLock(Log.TAG);
if (km.inKeyguardRestrictedInputMode()) {
mKeyguardLock.disableKeyguard();
isScreenLock = true;
} else {
isScreenLock = false;
}
}
public void releaseUnlock() {
// release screen
if (isScreenLock) {
mKeyguardLock.reenableKeyguard();
isScreenLock = false;
}
// release wake lock
if (mWakelock.isHeld()) {
mWakelock.release();
}
}
------------
public class Log {
public static final String TAG = "SimpleTimer";
public static void v(String msg){
android.util.Log.v(TAG, msg);
}
public static void d(String msg){
android.util.Log.d(TAG, msg);
}
public static void e(String msg){
android.util.Log.e(TAG, msg);
}
public static void e(Exception e){
android.util.Log.e(TAG, e.getMessage(),e);
}
}
AndroidManifest.xml加两个权限
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
Android屏幕解锁:
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("");
keyguardLock.disableKeyguard();
Android 点亮屏幕:
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
WakeLock mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "SimpleTimer");
mWakelock.acquire();
//...
mWakelock.release();//关闭
import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;
public class ScreenLockManager {
private Context mContext;
private KeyguardManager.KeyguardLock mKeyguardLock;
private boolean isScreenLock;
private PowerManager.WakeLock mWakelock;
public ScreenLockManager(Context context) {
mContext = context;
}
public void getUnlock() {
// acquire wake lock
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "SimpleTimer");
mWakelock.acquire();
// unlock screen
KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = km.newKeyguardLock(Log.TAG);
if (km.inKeyguardRestrictedInputMode()) {
mKeyguardLock.disableKeyguard();
isScreenLock = true;
} else {
isScreenLock = false;
}
}
public void releaseUnlock() {
// release screen
if (isScreenLock) {
mKeyguardLock.reenableKeyguard();
isScreenLock = false;
}
// release wake lock
if (mWakelock.isHeld()) {
mWakelock.release();
}
}
------------
public class Log {
public static final String TAG = "SimpleTimer";
public static void v(String msg){
android.util.Log.v(TAG, msg);
}
public static void d(String msg){
android.util.Log.d(TAG, msg);
}
public static void e(String msg){
android.util.Log.e(TAG, msg);
}
public static void e(Exception e){
android.util.Log.e(TAG, e.getMessage(),e);
}
}
[2] 批改launcher的抽屉图标
来源: 互联网 发布时间: 2014-02-18
修改launcher的抽屉图标
1、 准备3张图片
2、找到res/layout-port/Launcher.xml
3) 找到Launcher.xml中的<com.zwq.launcher.HandleView>标签,这个就是显示handle的地方,看它的src和background属性分别引用了哪个配置文件。android:background="@drawable/handle"和android:src="/blog_article/@drawable/handle_icon/index.html"。
4) 现在大家清楚了吧,背景的定义在drawable/handle.xml中,显示图标的定义在drawable/handle_icon.xml中,要修改哪个就去哪个配置文件找图片的位置。
5) 按drawable/handle.xml中定义的找到res/drawable-port-mdpi文件夹,替换tray_handle_normal、tray_handle_pressed和tray_handle_selected三张图片。
[3] urlconnection跟httpclient
来源: 互联网 发布时间: 2014-02-18
urlconnection和httpclient
urlconnection:
httpclient:
servlet端json转化:
android端接收:
数据库查询信息:
urlconnection:
String urlAddress = "http://192.168.1.102:8080/AndroidServer/login.do"; URL url; HttpURLConnection uRLConnection; public UrlConnectionToServer(){ } public String doGet(String username,String password){ String getUrl = urlAddress + "?username="+username+"&password="+password; try { url = new URL(/blog_article/getUrl/index.html); uRLConnection = (HttpURLConnection)url.openConnection(); InputStream is = uRLConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); uRLConnection.disconnect(); return response; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public String doPost(String username,String password){ try { url = new URL(/blog_article/urlAddress/index.html); uRLConnection = (HttpURLConnection)url.openConnection(); uRLConnection.setDoInput(true); uRLConnection.setDoOutput(true); uRLConnection.setRequestMethod("POST"); uRLConnection.setUseCaches(false); uRLConnection.setInstanceFollowRedirects(false); uRLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); uRLConnection.connect(); DataOutputStream out = new DataOutputStream(uRLConnection.getOutputStream()); String content = "username="+username+"&password="+password; out.writeBytes(content); out.flush(); out.close(); InputStream is = uRLConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); uRLConnection.disconnect(); return response; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
httpclient:
String urlAddress = "http://192.168.1.102:8080/qualityserver/login.do"; public HttpClientServer(){ } public String doGet(String username,String password){ String getUrl = urlAddress + "?username="+username+"&password="+password; HttpGet httpGet = new HttpGet(getUrl); HttpParams hp = httpGet.getParams(); hp.getParameter("true"); //hp. //httpGet.setp HttpClient hc = new DefaultHttpClient(); try { HttpResponse ht = hc.execute(httpGet); if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity he = ht.getEntity(); InputStream is = he.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); //String str = EntityUtils.toString(he); System.out.println("========="+response); return response; }else{ return "error"; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } } public String doPost(String username,String password){ //String getUrl = urlAddress + "?username="+username+"&password="+password; HttpPost httpPost = new HttpPost(urlAddress); List params = new ArrayList(); NameValuePair pair1 = new BasicNameValuePair("username", username); NameValuePair pair2 = new BasicNameValuePair("password", password); params.add(pair1); params.add(pair2); HttpEntity he; try { he = new UrlEncodedFormEntity(params, "gbk"); httpPost.setEntity(he); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } HttpClient hc = new DefaultHttpClient(); try { HttpResponse ht = hc.execute(httpPost); //连接成功 if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity het = ht.getEntity(); InputStream is = het.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); //String str = EntityUtils.toString(he); System.out.println("=========&&"+response); return response; }else{ return "error"; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } }
servlet端json转化:
resp.setContentType("text/json"); resp.setCharacterEncoding("UTF-8"); toDo = new ToDo(); List<UserBean> list = new ArrayList<UserBean>(); list = toDo.queryUsers(mySession); String body; try { //设定JSON JSONArray array = new JSONArray(); for(UserBean bean : list) { JSONObject obj = new JSONObject(); try { obj.put("username", bean.getUserName()); obj.put("password", bean.getPassWord()); }catch(Exception e){} array.add(obj); } pw.write(array.toString()); System.out.println(array.toString());
android端接收:
String urlAddress = "http://192.168.1.102:8080/qualityserver/result.do"; String body = getContent(urlAddress); JSONArray array = new JSONArray(body); for(int i=0;i<array.length();i++) { obj = array.getJSONObject(i); sb.append("用户名:").append(obj.getString("username")).append("\t"); sb.append("密码:").append(obj.getString("password")).append("\n"); HashMap<String, Object> map = new HashMap<String, Object>(); try { userName = obj.getString("username"); passWord = obj.getString("password"); } catch (JSONException e) { e.printStackTrace(); } map.put("username", userName); map.put("password", passWord); listItem.add(map); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(sb!=null) { showResult.setText("用户名和密码信息:"); showResult.setTextSize(20); } else extracted(); //设置adapter SimpleAdapter simple = new SimpleAdapter(this,listItem, android.R.layout.simple_list_item_2, new String[]{"username","password"}, new int[]{android.R.id.text1,android.R.id.text2}); listResult.setAdapter(simple); listResult.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int positionId = (int) (id+1); Toast.makeText(MainActivity.this, "ID:"+positionId, Toast.LENGTH_LONG).show(); } }); } private void extracted() { showResult.setText("没有有效的数据!"); } //和服务器连接 private String getContent(String url)throws Exception{ StringBuilder sb = new StringBuilder(); HttpClient client =new DefaultHttpClient(); HttpParams httpParams =client.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 3000); HttpConnectionParams.setSoTimeout(httpParams, 5000); HttpResponse response = client.execute(new HttpGet(url)); HttpEntity entity =response.getEntity(); if(entity !=null){ BufferedReader reader = new BufferedReader(new InputStreamReader (entity.getContent(),"UTF-8"),8192); String line =null; while ((line= reader.readLine())!=null){ sb.append(line +"\n"); } reader.close(); } return sb.toString(); }
数据库查询信息:
while(rs.next()) { UserBean bean = new UserBean(); bean.setUserName(rs.getString(1)); bean.setPassWord(rs.getString(2)); list.add(bean); } return list;
最新技术文章: