wm
1.
handler.postDelayed(new Runnable() {
@Override
public void run() {
//兩秒鐘后執(zhí)行到這里
//運行在主線程中
startActivity();
Log.e(TAG,"當(dāng)前線程的名字: " + Thread.currentThread().getName());
}
},2000);
可以延時執(zhí)行一個線程围来,在兩秒鐘后才執(zhí)行這個函數(shù)攻晒。
注意:這個線程是運行在主線程中的,handler中的線程是運行在主線程中的洽议。
Thread.currentThread().getName()
:得到當(dāng)前線程的名稱
2.
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e(TAG,"onTouchEvent == Action" + event.getAction());
startActivity();
return super.onTouchEvent(event);
}
重寫onTouchEvent()函數(shù)偿衰,實現(xiàn)點擊執(zhí)行里面的函數(shù)挂疆,這里會執(zhí)行兩次,down一次下翎,up一次缤言。
3.
@Override
protected void onDestroy() {
//把所有的消息和回調(diào)移除
handler.removeCallbacksAndMessages(null);
super.onDestroy();
}
handler.removeCallbacksAndMessages(null);
注意這一句是把所有的handler消息都移除掉。
4.用RadioGroup和RadioButton來做底部導(dǎo)航欄
rg_bottom_tag.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
設(shè)置RadioGroup的點擊的監(jiān)聽
rg_bottom_tag.check(R.id.rb_video);
設(shè)置RadioGroup的默認(rèn)選中
SystemClock.sleep(2000); //休眠兩秒
可以讓線程休眠兩秒鐘
5.
從本地的SD卡得到數(shù)據(jù)有兩種方法
1.遍歷SD卡數(shù)據(jù)的后綴名(比較慢)
2.從內(nèi)容提供者里面獲取數(shù)據(jù)(推薦)
ContentResolver resolver = context.getContentResolver();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] objs = new String[] {
MediaStore.Video.Media.DISPLAY_NAME, //視頻的名稱
MediaStore.Video.Media.DURATION, //視頻的時長
MediaStore.Video.Media.SIZE, //視頻的大小
MediaStore.Video.Media.DATA, //視頻的絕對路徑
MediaStore.Video.Media.ARTIST //視頻的演唱者
};
Cursor cursor = resolver.query(uri,objs,null,null,null);
if (cursor != null) {
while (cursor.moveToNext()) {
MediaItem mediaItem = new MediaItem();
String name = cursor.getString(0); //視頻的名稱
mediaItem.setName(name);
long duration = cursor.getLong(1); //視頻的時長
mediaItem.setDuration(duration);
long size = cursor.getLong(2); //視頻的大小
mediaItem.setSize(size);
String data = cursor.getString(3); //視頻的絕對路徑
mediaItem.setData(data);
String artist = cursor.getString(4); //視頻的演唱者
mediaItem.setArtist(artist);
mediaItems.add(mediaItem);
}
cursor.close();
}
這一段代碼是將手機(jī)中的視頻查詢出來视事。
Formatter.formatFileSize(context,mediaItem.getSize())
這個公式可以將獲取到的視頻大小byte的格式轉(zhuǎn)化為MB的形式胆萧。
6.
//1.調(diào)起系統(tǒng)所有的播放 (隱式意圖)
Intent intent = new Intent();
intent.setDataAndType(Uri.parse(mediaItem.getData()),"video/*");
context.startActivity(intent);
//2. 調(diào)用自己寫的播放器 (顯式意圖)
Intent intent = new Intent(context, SystemVideoPlayer.class);
intent.setDataAndType(Uri.parse(mediaItem.getData()),"video/*");
context.startActivity(intent);
第二行傳遞的是一個Uri。接收的時候也要是Uri接收俐东。
3. 傳遞對象數(shù)據(jù)---序列化
Intent intent = new Intent(context, SystemVideoPlayer.class);
Bundle bundle = new Bundle();
bundle.putSerializable("videolist", (Serializable) mediaItems);
intent.putExtras(bundle);
intent.putExtra("position",position);
context.startActivity(intent);
傳遞對象的時候跌穗,要將對象進(jìn)行序列化操作,然后放到Bundle里面進(jìn)行傳遞虏辫。
7 .
//移除消息(單個)
handler.removeMessages(PROGRESS);
//延遲一秒再發(fā)送一個消息
handler.sendEmptyMessageDelayed(PROGRESS, 1000);
8
android:configChanges="keyboardHidden|screenSize|orientation"
橫豎屏切換的時候蚌吸,生命周期不切換
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
第一個默認(rèn)是橫屏
第二個去除橫屏(全屏)時的TitleBar,但是活動必須繼承Activity
Activity和服務(wù)交互的方式:
廣播砌庄,Intent羹唠,Handler,接口回調(diào)娄昆,Appliaction佩微,EventBus,AIDL萌焰。