Work Note

This is work note and will be update anytime.

Timer Task hanlder 案例应用

使用timerTask制定定时任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//TimerTas 类
private class LongPressTask extends TimerTask {
int mSelection;
public LongPressTask(int selection) {
mSelection = selection;
}
@Override
public void run() {
longClicked = true;
Message msg = new Message();
msg.what = mSelection;
mTempHanlder.sendMessage(msg);
}
};
//初始化
private LongPressTask lpTask;
private Timer lpTimer;
private Handler mTempHanlder = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what == FASTFORWARD)
;
else if(msg.what == REVERSE)
;
}
};
//启用task
lpTask = new LongPressTask(FASTFORWARD);
lpTimer =new Timer();
lpTimer.schedule(lpTask,300,500);
//停用Task
lpTimer.cancel();

Android窗口打开动画

控制进入界面的动画

1
2
3
4
5
6
7
8
9
<!-- Theme中应用 -->
<item name="android:windowAnimationStyle">@style/music_windowAnimationStyle</item>
<!-- sytle -->
<style name="music_windowAnimationStyle" parent="android:style/Animation.Translucent">
<item name="android:windowEnterAnimation">@null</item>
<item name="android:windowExitAnimation">@null</item>
<item name="android:activityOpenEnterAnimation">@null</item>
<item name="android:activityOpenExitAnimation">@null</item>
</style>

根据当前文件获取当前目录

1
2
3
4
5
String[] filePath = mFileUri.toString().split("/");
String fileDirectoryString = "";
for (int i = 0; i < filePath.length - 1; i ++) {
fileDirectoryString += filePath[i] + "/";
}

查询当前目录下的所有视频不包括子目录

query(); @arg1 uri, @arg2 要查的列,@arg3 where 语句,@arg4 where语句参数,@arg5排序。
sqlit3语句不太会用,应该有更好的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
private boolean getFilePathVideoList(String filePath) {
String[] cols = new String[] {MediaStore.Audio.Media._ID, MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.DATE_TAKEN, MediaStore.Video.Media.DATA};
String where = MediaStore.Video.Media.DATA +" like '%" + filePath + "%'";
ContentResolver resolver = getContentResolver();
if (resolver == null) {
Log.d(TAG, "getFilePathVideoList: resolver is null");
return false;
} else {
Cursor cursor = null;
try {
cursor = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, cols,
where, null, null);//查询当前目录下所有的视频
if (cursor != null && cursor.moveToFirst()) {
do {
String[] pathArray= cursor.getString(4).split(filePath);
if (!pathArray[1].contains("/")) { //过滤子目录的视频
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(MediaStore.Audio.Media.TITLE,
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
map.put(MediaStore.Audio.Media.MIME_TYPE,
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)));
map.put(KEY_EXTERNAL_CONTENT_URI, ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID))));
map.put("filedate",
cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_TAKEN)));
fileManagerVideoList.add(map);
}
} while (cursor.moveToNext());
}
} catch (Exception e) {
Log.d(TAG, "getFilePathVideoList: fail");
return false;
} finally {
Log.d(TAG, "getVideoList: fileManagerVideoListSize::" + fileManagerVideoList.size());
if (cursor != null)
cursor.close();
}
}
return true;
}

调节视频播放速度接口

1
2
//f (0.5~2.0)
mMediaPlayer.setPlaybackParams(mMediaPlayer.getPlaybackParams().setSpeed(f));

SharedPreferences使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//存入
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
mSharedPreferences.edit().putInt("search_position", 1).commit();
//取出
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
int serachIndex = mSharedPreferences.getInt("serach_area_tip", 0);
//跨应用取值
//写入com.test.app
SharedPreferences preferences = mContext.getSharedPreferences("other_app_tag", Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE|Context.MODE_MULTI_PROCESS);
Editor editor = preferences.edit();
editor.putLong("keyword", value);
editor.clear().commit();
//取值
try {
Context contextFromGallery = null;
contextFromApp = createPackageContext(
"com.test.app", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE | MODE_MULTI_PROCESS);
SharedPreferences sp = contextFromApp
.getSharedPreferences("other_app_tag",
MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE | MODE_MULTI_PROCESS);
vaule = sp.getLong("keyword", 0);
Log.d(TAG, getUri + " value = " + vaule);
} catch (Exception e) {
Log.d(TAG, "com.test.app Package not found!");
}

格式化文件大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private static String formateFileSize(long fileSize) {
DecimalFormat dFormat = new DecimalFormat("#.00");
String fileSizeString = "";
String wrongString = "0 B";
if (fileSize == 0) {
return wrongString;
}
if (fileSize < 1024) {
fileSizeString = dFormat.format((double)fileSize) +" B";
} else if (fileSize < 1048576) {
fileSizeString = dFormat.format((double)fileSize/1024) + " KB";
} else if (fileSize < 1073741824) {
fileSizeString = dFormat.format((double)fileSize/1024/1024) + " MB";
} else {
fileSizeString = dFormat.format((double)fileSize/1024/1024/1024) + " GB";
}
return fileSizeString;
}

格式化时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static String formatDuration( int duration) {
int h = duration / 3600;
int m = (duration - h * 3600) / 60;
int s = duration - (h * 3600 + m * 60);
String durationValue;
String wrongString = "00:00:00";
if (duration == 0) {
return wrongString;
}
if (h == 0) {
if (m == 0 && s == 0) s = 1;//[BUGFIX]-Modify by TCTNJ,su.jiang, 2016-03-09,PR1759891
durationValue = String.format("00:%02d:%02d", m, s);
} else {
durationValue = String.format("%d:%02d:%02d", h, m, s);
}
return durationValue;
}

如果您觉得本博客还不错,欢迎继续关注本博客,欢迎多提宝贵意见,非常感谢!

作者Jam 有问题请 留言 或者私信我的 微博
------本文结束    感谢阅读------
坚持原创技术分享,您的支持将鼓励我继续创作!