Android中常用代码

2016-10-08  本文已影响58人  元亨利贞o

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;
}
@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;
}
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);
}
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;
    }
}
  /**
     * 判断视频是否为横屏
     * @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();
        }
    }

努力更新中 ......

上一篇下一篇

猜你喜欢

热点阅读