Menu Home

android

Android: Check Long Press Helper

基本思想: 在Down事件中向UI线程塞入一个定时任务,如果在定时时间终了前任务没有被取消,就认为是一个长按事件。 /* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the “License”); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * […]

Android:FragmentPagerAdapter Demo

package com.example.sijiewang.testviewpager; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import java.util.ArrayList; public class MainActivity extends FragmentActivity { private ViewPager mAlarmCreateViewPager; private Fragment mFragmentOne; private Fragment mFragmentTwo; private ArrayList<Fragment> mFragmentList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFragmentList = new ArrayList<Fragment>(2); mFragmentOne = new mFragmentOne(); […]

Android的坑: Intent setData and setType

public Intent setData (Uri data) Added in API level 1 Set the data this intent is operating on. This method automatically clears any type that was previously set by setType(String) or setTypeAndNormalize(String). Note: scheme matching in the Android framework is case-sensitive, unlike the formal RFC. As a result, you should […]

Android:onTouchEvent & TouchListener的执行

public boolean dispatchTouchEvent(MotionEvent event) { if (mInputEventConsistencyVerifier != null) { mInputEventConsistencyVerifier.onTouchEvent(event, 0); } if (onFilterTouchEventForSecurity(event)) { // noinspection SimplifiableIfStatement ListenerInfo li = mListenerInfo; if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED && li.mOnTouchListener.onTouch(this, event)) { return true; } if (onTouchEvent(event)) { return true; } […]

Andorid Wear调试

通过USB adb -d forward tcp:5601 tcp:5601 通过蓝牙 1、在手机端打开Android Wear应用 2、点击界面右上的菜单并选择设置(Settings) 3、开启“通过蓝牙调试”(Debugging over Bluetooth)。你可以看到设备连接状态: Host: disconnected Target: connected 4、手机用usb连接电脑,并执行adb命令: adb forward tcp:4444 localabstract:/adb-hub; adb connect localhost:4444 你就可以看到刚才的连接状态就变成了: Host: connected Target: connected

Android:关于ViewGroup的ClipChildren

ClipChildren属性对需要做动画的View非常有用,特别是对ScaleAnimation。 需要注意的是ClipChildren = false只是告诉他的子View可以超出他本身的大小,并不是说他自己可以超出他的父View的大小,所以如果你想允许某个View超过其父View的边界,你需要一直找到最顶层的父View并设置ClipChildren = false。 下面需要提一下的是一个关于ClipChildren的坑,在4.2的系统上(4.3未测试,4.4及5.0版本没有这个 BUG),在启用了hardware accelerated的情况下,ScaleAnimation会出现撕裂等不流畅的现象,处理方法如下: if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { parentView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); } 这里的parentView一定要是你启用了ClipChildren = false的那个ViewGroup,否则没有效果,最佳实践是只在需要做ScaleAnimation的时候才关闭硬件加速,做完动画后应该重新开启。 关于这个BUG的更多信息请移步:这里 关于更多Android Hardware Accelerated信息请阅读:这里

Android:监控电量

获取初始电量 Android系统发送的电量广播是一个sticky broadcast,所以可以通过给registerReceiver传递一个null参数来获取上次系统发送的电量广播。 private int getLastBatteryLevel() { Intent batteryIntent = registerReceiver(null, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); return batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); } 监控电量 mBatteryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final int level = intent.getIntExtra( BatteryManager.EXTRA_LEVEL, -1); Runnable updateBatteryLevel = new Runnable() { @Override public void run() { […]