Android中常用代码
程序员文章站
2022-04-28 11:24:55
...
- (1) 判断当前进程名是否是指定的名称
public static boolean isNamedProcess(@NonNull Context context, @NonNull String processName) {
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processInfoList = manager.getRunningAppProcesses();
if (processInfoList == null) return false;
for (RunningAppProcessInfo processInfo : processInfoList) {
if (processInfo.pid == pid && processName.equals(processInfo.processName)) {
return true;
}
}
return false;
}
- (2) 获取当前进程名
@Nullable
public static String getCurrentProcessName(Context context) {
if(context == null) return null;
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processInfoList = manager.getRunningAppProcesses();
if (processInfoList == null) return null;
for (RunningAppProcessInfo processInfo : processInfoList) {
if (processInfo.pid == pid) {
return processInfo.processName;
}
}
return null;
}
- (3) 判断某个时间点是否在某段时间内
public static boolean fallInSection(Date timePoint, Date sectionStart, Date sectionEnd) {
return timePoint.after(sectionStart) && timePoint.before(sectionEnd);
}
有了上面的方法, 那么判断当前时间是否在某段时间内, 就可以这像下面样写:
public static boolean duringHalloween() {
Calendar calendar = Calendar.getInstance();
Date currentTimePoint = calendar.getTime();
calendar.set(2016, 9, 29, 0, 0, 0);
Date sectionStart = calendar.getTime();
calendar.set(2016, 9, 31, 23, 59, 59);
Date sectionEnd = calendar.getTime();
return DateUtil.fallInSection(currentTimePoint, sectionStart, sectionEnd);
}
- (4) 判断Android主进程是否在后台
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
public class MyApplication extends Application {
private static MyApplication sInstance;
public boolean isBackground; //进程是否在后台
public static MyApplication getInstance() {
return sInstance;
}
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks);
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (sInstance == null) return;
//app切换到后台
if (level == TRIM_MEMORY_UI_HIDDEN) {
isBackground = true;
// TODO: 16/10/8 在此处编写从前台切换到后台的逻辑
}
}
@Override
public void onTerminate() {
super.onTerminate();
unregisterActivityLifecycleCallbacks(mActivityLifecycleCallbacks);
}
private ActivityLifecycleCallbacks mActivityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
@Override
public void onActivityStarted(Activity activity) {}
@Override
public void onActivityResumed(Activity activity) {
if (isBackground) { //app从后台切换到前台
isBackground = false;
// TODO: 16/10/8 在此处编写从后台切换到前台的逻辑
}
}
@Override
public void onActivityPaused(Activity activity) {}
@Override
public void onActivityStopped(Activity activity) {}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivityDestroyed(Activity activity) {}
};
}
像如下方式使用:
public class DownloadService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(MyApplication.getInstance().isBackground) {
// TODO: 16/10/8 do something here ......
}
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
- (5) 判断视频是否是横屏
/**
* 判断视频是否为横屏
* @param context
* @param videoUri
* @return
*/
public static boolean isHorizontalVideo(Context context, Uri videoUri) {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
try {
mmr.setDataSource(context, videoUri);
//MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION, required api17
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
String rotation = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
int rotationInt = Integer.parseInt(rotation);
return rotationInt == 0 || rotationInt == 180;
}
//less-than api17
Bitmap bm = mmr.getFrameAtTime();
return bm != null && bm.getWidth() > bm.getHeight();
} finally {
mmr.release();
}
}
努力更新中 ......
上一篇: 使用xampp搭建运行php虚拟主机的详细步骤_PHP
下一篇: css中常用技巧进阶
推荐阅读
-
php获取mysql数据库中的所有表名的代码_PHP
-
UCenter中的一个可逆加密函数authcode函数代码_php技巧
-
PHP中fwrite与file_put_contents性能测试代码
-
JavaScript中的星星评分效果的实现代码分享
-
phpstorm中js和php代码混用有错误提示,但是其实没问题,怎么关闭呢?
-
php中通过curl模拟登陆discuz论坛的实现代码_PHP教程
-
PHP中codeigniter文件上传类代码实例_PHP教程
-
php中无限级分类程序代码_PHP教程
-
Android中ACTION_CANCEL的触发机制与滑出子view的情况
-
如何在java程序中运行js的代码分享