Android代码片段收集

有用的Android代码片段收集,方便自己和大家

1,采用Global变量在多个应用间共享配置数据

定义变量:
settings.java : TEST_IN_SETUP_USER_FLOW
设置变量:
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.TEST_IN_SETUP_USER_FLOW, 1 );
获得变量:
int  testIsUserFlow = Settings.Global.getInt(mTSBLauncherActivity.getContentResolver(), Settings.Global.TEST_IN_SETUP_USER_FLOW, 0);


2,实现超时操作

if(timer != null){
	timer.cancel();
	timer =null;
}
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
	@Override
	public void run() 
	{
		// TODO Auto-generated method stub
		Log.i(TAG," TimerTask time out...");
	}	
};
timer.schedule(task ,2*1000);  //set the time

 

3,从桌面移除图标

PackageManager pm = getPackageManager();  
ComponentName name = new ComponentName(this, Tutorial_spec.class);  
pm.setComponentEnabledSetting(name,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);

4,得到当前时间

1,long time = SystemClock.uptimeMillis();
2,Date curDate = new Date(System.currentTimeMillis());
3,SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
4,SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");     
5,String date = sDateFormat.format(new java.util.Date());


5,手机的横屏显示

XML:android:screenOrientation="landscape"//可能不会起作用
代码中:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

6,判断当前状态栏高度

 /** 
     * 用于获取状态栏的高度。 
     *  
     * @return 返回状态栏高度的像素值。 
     */  
    private int getStatusBarHeight() {  
        if (statusBarHeight == 0) {  
            try {  
                Class<?> c = Class.forName("com.android.internal.R$dimen");  
                Object o = c.newInstance();  
                Field field = c.getField("status_bar_height");  
                int x = (Integer) field.get(o);  
                statusBarHeight = getResources().getDimensionPixelSize(x);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
        return statusBarHeight;  
    }  

7,按照字符顺排序

    /**
     * Perform alphabetical comparison of application entry objects.
     */
    public static final Comparator<AppEntry> ALPHA_COMPARATOR = new Comparator<AppEntry>() {
        private final Collator sCollator = Collator.getInstance();
        @Override
        public int compare(AppEntry object1, AppEntry object2) {
            return sCollator.compare(object1.getLabel(), object2.getLabel());
        }
    };
调用排序
// Sort the list.
    Collections.sort(entries, ALPHA_COMPARATOR);


      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值