美文网首页
Android中常用代码

Android中常用代码

作者: 元亨利贞o | 来源:发表于2016-10-08 16:48 被阅读58次

    • (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();
            }
        }
    

    努力更新中 ......

    相关文章

      网友评论

          本文标题:Android中常用代码

          本文链接:https://www.haomeiwen.com/subject/paiuyttx.html