2017年12月25日 星期一

Get the user id (Check if the user is owner)

Reference:
https://stackoverflow.com/questions/14749504/android-usermanager-check-if-user-is-owner-admin
http://blog.csdn.net/zhanglianyu00/article/details/50238447
http://blog.csdn.net/zhanglianyu00/article/details/50253187
http://gityuan.com/2016/11/20/user_manager/


Code snippet:
public static long getUserId(Context context){
    UserHandle uh = Process.myUserHandle();
    UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
    long userSerialNumber = -1;
    if(null != um) {
        userSerialNumber = um.getSerialNumberForUser(uh);
    }
    return userSerialNumber;
}

Get duration of the video by uri

Reference:

https://stackoverflow.com/questions/33770188/how-can-i-get-the-duration-resolution-of-a-video-file-programmatically-in-andro/33770238


Code snippet:

MediaPlayer mp = MediaPlayer.create(this, Uri.parse(uriOfFile));
if(mp == null){
    Log.e(TAG, "The record file is invalid");
     return;
}
int duration = mp.getDuration();

Detect switching between users

Reference:

https://stackoverflow.com/questions/15392126/how-to-detect-switching-between-users


Description:

Listen the ACTION_USER_BACKGROUND and ACTION_USER_FOREGROUND broadcast to detect switching between users.


Code snippet:


IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_USER_BACKGROUND);
filter.addAction(Intent.ACTION_USER_FOREGROUND);
registerReceiver(mUserSwitchReceiver, filter);



public class UserSwitchReceiver extends BroadcastReceiver {
    private static final String TAG = "UserSwitchReceiver";
    @Override    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        
        if(Intent.ACTION_USER_FOREGROUND.equals(action)){
            // Todo: do something when switch in the user
        } else if(Intent.ACTION_USER_BACKGROUND.equals(action)){
            // Todo: do something when switch out the user
        }
    }
}

2017年12月5日 星期二

當Low memory時,Activity被銷毀了,但是Fragment重複產生

Reference:

http://juniperphoton.net/2017/03/27/jie-jue-viewpager-fragment-de-hui-fu-wen-ti/
http://y-anz-m.blogspot.tw/2013/07/viewpager-fragment.html
http://www.voidcn.com/article/p-weicgtjm-kv.html

Issue description:

當Low memory時,process被系統刪除,Activity被銷毀了,但是Fragment還在,導致Fragment重疊,造成開啟app的時間不斷增加

使用 adb shell dumpsys activity top 指令
可以發現 fragment數量不停累加

Solution:

在Activity被銷毀前,先將正在使用的fragment object儲存起來
下次開啟app後,再將前一次使用的fragment object (現在用不到的) 刪除掉

Code snippet:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Store the current used fragments
        if (mFragmentAdapter != null) {
            getSupportFragmentManager().putFragment(outState, KEY_FRAG_NAME1, mFragmentAdapter.getFragment(0));
            getSupportFragmentManager().putFragment(outState, KEY_FRAG_NAME2, mFragmentAdapter.getFragment(1));
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
.................
        // Remove previous unused fragments
        if (savedInstanceState != null) {
            mFragment1 = getSupportFragmentManager().getFragment(savedInstanceState,
                    KEY_FRAG_NAME1);
            mFragment2 = getSupportFragmentManager().getFragment(savedInstanceState,
                    KEY_FRAG_NAME2);

            if(mFragment1 != null || mFragment2 != null){
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.remove(mFragment1 );
                transaction.remove(mFragment2 );
                transaction.commit();
            }
        }
...............
    }