首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

手机安全卫士开发系列(七)——知识点整理(2)

2013-09-08 
手机安全卫士开发系列(7)——知识点整理(2)六、安装新的apk通过Activity自带的getSharedPreferences方法,可以

手机安全卫士开发系列(7)——知识点整理(2)

六、  安装新的apk

     通过Activity自带的getSharedPreferences方法,可以得到SharedPreferences对象。
     public abstract SharedPreferences getSharedPreferences (String name, int mode);
     name:表示保存后 xml 文件的名称
     mode:表示 xml 文档的操作权限模式(私有,可读,可写),使用0或者MODE_PRIVATE作为默认的操作权限模式。 
     1.数据读取:
     通过SharedPreferences对象的键key可以获取到对应key的键值。对于不同类型的键值有不同的函数:getBoolean,getInt,getFloat,getLong.
     public abstract String getString (String key, String defValue);
     2.数据存入:
     数据的存入是通过SharedPreferences对象的编辑器对象Editor来实现的。通过编辑器函数设置键值,然后调用commit()提交设置,写入xml文件。
     public abstract SharedPreferences.Editor edit ();
     public abstract SharedPreferences.Editor putString (String key, String value);
     public abstract boolean commit ();

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.       
  7.     <TextView    
  8.         android:id="@+id/textview"  
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"   
  11.         android:text="@string/hello"/>  
  12.       
  13. </LinearLayout>  

[c-sharp] view plaincopy
  1. package com.android.test;  
  2. import android.app.Activity;  
  3. import android.content.SharedPreferences;  
  4. import android.os.Bundle;  
  5. import android.preference.PreferenceManager;  
  6. import android.widget.TextView;  
  7. public class TestSharedPreferences extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.           
  14.         SharedPreferences mSharedPreferences = getSharedPreferences("TestSharedPreferences", 0);  
  15. //        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);  
  16.           
  17.         int counter = mSharedPreferences.getInt("counter", 0);  
  18.           
  19.         TextView mTextView = (TextView)findViewById(R.id.textview);  
  20.         mTextView.setText("This app has been started " + counter + " times.");  
  21.           
  22.         SharedPreferences.Editor mEditor = mSharedPreferences.edit();  
  23.         mEditor.putInt("counter", ++counter);  
  24.         mEditor.commit();  
  25.           
  26.     }  
  27. }  

说明:

    SharedPreferences的获取有两种方法:一是上面提到的通过Activity自带(本质来讲是Context的)的getSharedPreferences方法,可以得到SharedPreferences对象。这种方法的好处是可以指定保存的xml文件名。另一种是通过PreferenceManager.getSharedPreferences(Context)获取SharedPreferences对象。这种方法不能指定保存的xml文件名,文件名使用默认的:<package name>+"_preferences.xml"的形式,不过如果在一个包里面采用这种方式需要保存多个这样的xml文件,可能会乱掉。建议采用第一种指定xml文件名的形式。数据的存入必须通过SharedPreferences对象的编辑器对象Editor来实现,存入(put)之后与写入数据库类似一定要commit。

十三、自定义对话框的写法

定义一个样式文件 重写了系统的一些默认配置

name="android:windowBackground">@drawable/title_background

name="android:windowNoTitle">true</item>

十四.Md5的编码和加密 (不可逆的加密算法)

package cn.itcast.mobilesafe.util;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class MD5Encoder {public static String encode(String pwd) {try {MessageDigest  digest = MessageDigest.getInstance("MD5");byte[]  bytes = digest.digest(pwd.getBytes());StringBuffer sb = new  StringBuffer();for(int i = 0;i<bytes.length;i++){String s = Integer.toHexString(0xff&bytes[i]);if(s.length()==1){sb.append("0"+s);}else{sb.append(s);}}return sb.toString();} catch (NoSuchAlgorithmException e) {e.printStackTrace();throw new RuntimeException("buhuifasheng");}}}


十五.style的使用

十六.更改activity切换的动画效果

overridePendingTransition(R.anim.alpha_in,R.anim.alpha_out);

Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画。
它包括两个部分:
一部分是第一个activity退出时的动画;
另外一部分时第二个activity进入时的动画;
在Android的2.0版本之后,有了一个函数来帮我们实现这个动画。这个函数就是YoverridePendingTransition
j这个函数有两个参数,一个参数是第一个activity退出时的动画,另外一个参数则是第二个activity进入时的动画。

这里需要特别说明的是,关于overridePendingTransition这个函数,有两点需要主意

1.它必需紧挨着startActivity()或者finish()函数之后调用"
````2.它只在android2.0以及以上版本上适用  

常见的效果:

淡入淡出效果

overridePendingTransition(R.anim.fade, R.anim.hold);
放大淡出效果
overridePendingTransition(R.anim.my_scale_action,R.anim.my_alpha_action);
转动淡出效果
overridePendingTransition(R.anim.scale_rotate,R.anim.my_alpha_action);
转动淡出效果
overridePendingTransition(R.anim.scale_translate_rotate,R.anim.my_alpha_action);
左上角展开淡出效果
overridePendingTransition(R.anim.scale_translate,R.anim.my_alpha_action);
压缩变小淡出效果
overridePendingTransition(R.anim.hyperspace_in,R.anim.hyperspace_out);
右往左推出效果
overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
下往上推出效果
overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
左右交错效果
overridePendingTransition(R.anim.slide_left,R.anim.slide_right);
放大淡出效果
overridePendingTransition(R.anim.wave_scale,R.anim.my_alpha_action);
缩小效果
overridePendingTransition(R.anim.zoom_enter,R.anim.zoom_exit);
上下交错效果
overridePendingTransition(R.anim.slide_up_in,R.anim.slide_down_out); 

十七.获取新打开的activity的返回值

StartactivityforResult();

SetResultData();

OnActivityResult();

十八.DeviceAdmin的技术 2.2版本支持 ->wipedata() setpwd();

不能直接被卸载 在设备管理器里面取消激活

十九 .checkbox的状态 状态变更的监听器

二十、.gps 单态类 , gps wifi 基站

获取系统服务LOCATION_SERVICE ->locationManager 

记得把权限加到清单文件

二十一 .广播接受者

有序广播 ->1.一般的有序广播 abortbroadcast()  (-1000~1000)

2.指定了接受者的有序广播setResult();

 

无序的广播

二十二. 短信内容的处理

Object[] pdus = (Object[])intent.getExtras().get("pdus");

 

热点排行