1、android:process
解決訪問SharedPreferences,不在同一進程
private SharedPreferencesDB(Context cxt) {
this.context = cxt;
Context context;
try {
context = cxt.createPackageContext(cxt.getPackageName(), Context.CONTEXT_IGNORE_SECURITY);
prefs = context.getSharedPreferences(PREFS_FILE, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);//可讀可寫
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
如果該屬性指定名稱以“:”開頭,則一個新的專屬於該應用的進程將會被創建。
如果該進程名以小寫字母開頭,則爲該activity提供權限以讓其在一個全局的進程中運行。這樣會允許多個應用的不同組件共用一個進程,以便節省資源。
2、applicationContext與context的區別
applicationContext與application生命週期一致,儘量用它
3、notifyDataSetChanged與notifyDataSetInvalidated
notifyDataSetInvalidated:更新數據並把列表組件移到頂部
notifyDataSetChanged:容易使列表閃動
4、drawable-m與drawable-h
高清圖放入drawable-h不容易佔內存,若高清圖放入drawable-m則內存增大,若將低清圖放入drawable-h容易造成UI失真
5、應用存在時清緩存
清除緩存時會執行onSaveInstanceState方法
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("key", "value");//緩存數據
}
清除緩存後打開應用時,會再次執行onCreate(Bundle savedInstanceState)
在這個方法中把剛剛被清除的數據給找回
if (savedInstanceState != null) {
String str=savedInstanceState.getString("key"));
}
6、全局異常處理UncaughtExceptionHandler
在public void uncaughtException(Thread thread, Throwable ex) 方法中執行捕獲全局異常
7、android4.0前與後對於flash的處理
4.0需要硬件加速
mainfest.xml設置android:hardwareAccelerated="true"
8、加載圖片
TransitionDrawable td = new TransitionDrawable(new Drawable[] { new ColorDrawable(R.color.transparent),
new BitmapDrawable(resources, bitmap) });
holder.getInfoListImg().setImageDrawable(td);
td.startTransition(150);
9、防止睡眠
<uses-permission android:name="android.permission.WAKE_LOCK" />
private void acquireWakeLock() {
if (wakeLock == null) {
PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, this.getClass().getCanonicalName());
wakeLock.acquire();
}
}
private void releaseWakeLock() {
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
wakeLock = null;
}
}
10、電話狀態監聽
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new MobliePhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
private class MobliePhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (currentMusic == null)
return;
MediaPlayer mPlayer = playerMap.get(currentMusic.getPlayUrl());
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: /* 無任何狀態時 */
break;
case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起電話時 */
break;
case TelephonyManager.CALL_STATE_RINGING: /* 電話進來時 */
break;
default:
break;
}
}
}
IntentFilter filter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
this.registerReceiver(new AudioBroadCastReceiver(), filter);
public class AudioBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {/*耳機被拔出*/
}
}
}
12、監控apk安裝
<receiver
android:enabled="true"
android:name="com.mobcent.ad.android.ui.activity.receiver.AdBootReceiver" >
<intent-filter >
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String packageName = intent.getData().getSchemeSpecificPart();
adService = new AdServiceImpl(context);
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {//添加
}else if(Intent.ACTION_PACKAGE_REMOVED.equals(action)){//移除
}else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){//安裝
}
}