public void run() {
Intent i = new Intent(SplashScreen.this, Main.class); //通过Intent打开最终真正的主界面Main这个Activity
SplashScreen.this.startActivity(i); //启动Main界面
SplashScreen.this.finish(); //关闭自己这个开场屏
}
}, 5000); //5秒,够用了吧
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); //Android开发网提示大家相关的还有Intent.FLAG_ACTIVITY_CLEAR_TOP,都试试
startActivity(i);
void close() //关闭,因为在Linux内部mmap占用一个句柄,不用时一定要释放了
InputStream getInputStream() 返回读取的内容用Java层的InputStream保存
OutputStream getOutputStream() 把一个OutputSream写入到MemoryFile中
boolean isPurgingAllowed() //判断是否允许清理
int length() //返回内存映射文件大小
int readBytes(byte[] buffer, int srcOffset, int destOffset, int count)
void writeBytes(byte[] buffer, int srcOffset, int destOffset, int count)
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 16*1024); //强制缓存大小为16KB,一般Java类默认为8KB
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) { //处理换行符
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cwj"
/>
<include layout="@layout/view" />
<include android:id="@+id/block" layout="@layout/item" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/android123"
/>
</LinearLayout>
private boolean mChecked; //状态是否选中
private int mButtonResource;
private boolean mBroadcasting;
private Drawable mButtonDrawable; //按钮的图标
private OnCheckedChangeListener mOnCheckedChangeListener; //选中状态改变监听
private OnCheckedChangeListener mOnCheckedChangeWidgetListener;
R.attr.state_checked
};
this(context, null);
}
this(context, attrs, 0);
}
super(context, attrs, defStyle);
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.CompoundButton, defStyle, 0);
if (d != null) {
setButtonDrawable(d);
}
.getBoolean(com.android.internal.R.styleable.CompoundButton_checked, false);
setChecked(checked);
}
setChecked(!mChecked);
}
public boolean performClick() {
toggle();
return super.performClick();
}
return mChecked;
}
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState(); //更新当前状态的按钮图标
return;
}
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
}
if (mOnCheckedChangeWidgetListener != null) {
mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
}
}
}
mOnCheckedChangeListener = listener;
}
mOnCheckedChangeWidgetListener = listener;
}
void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
}
if (resid != 0 && resid == mButtonResource) {
return;
}
if (mButtonResource != 0) {
d = getResources().getDrawable(mButtonResource);
}
setButtonDrawable(d);
}
if (d != null) {
if (mButtonDrawable != null) {
mButtonDrawable.setCallback(null);
unscheduleDrawable(mButtonDrawable);
}
d.setCallback(this);
d.setState(getDrawableState());
d.setVisible(getVisibility() == VISIBLE, false);
mButtonDrawable = d;
mButtonDrawable.setState(null);
setMinHeight(mButtonDrawable.getIntrinsicHeight());
}
}
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
boolean populated = super.dispatchPopulateAccessibilityEvent(event);
int resourceId = 0;
if (mChecked) {
resourceId = R.string.accessibility_compound_button_selected;
} else {
resourceId = R.string.accessibility_compound_button_unselected;
}
String state = getResources().getString(resourceId);
event.getText().add(state);
event.setChecked(mChecked);
}
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (buttonDrawable != null) {
final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
final int height = buttonDrawable.getIntrinsicHeight();
case Gravity.BOTTOM:
y = getHeight() - height;
break;
case Gravity.CENTER_VERTICAL:
y = (getHeight() - height) / 2;
break;
}
buttonDrawable.draw(canvas);
}
}
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
protected void drawableStateChanged() { //android123提示状态改变时需要更换按钮的图标
super.drawableStateChanged();
if (mButtonDrawable != null) {
int[] myDrawableState = getDrawableState();
mButtonDrawable.setState(myDrawableState);
invalidate();
}
}
protected boolean verifyDrawable(Drawable who) {
return super.verifyDrawable(who) || who == mButtonDrawable;
}
boolean checked;
super(superState);
}
private SavedState(Parcel in) {
super(in);
checked = (Boolean)in.readValue(null);
}
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeValue(checked);
}
public String toString() {
return "CompoundButton.SavedState{"
+ Integer.toHexString(System.identityHashCode(this))
+ " checked=" + checked + "}";
}
= new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
return new SavedState[size];
}
};
}
public Parcelable onSaveInstanceState() {
// Force our ancestor class to save its state
setFreezesText(true);
Parcelable superState = super.onSaveInstanceState();
return ss;
}
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
setChecked(ss.checked);
requestLayout();
}
}
private CharSequence mTextOn;
private CharSequence mTextOff;
private Drawable mIndicatorDrawable;
private float mDisabledAlpha;
public ToggleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.ToggleButton, defStyle, 0);
mTextOn = a.getText(com.android.internal.R.styleable.ToggleButton_textOn);
mTextOff = a.getText(com.android.internal.R.styleable.ToggleButton_textOff);
mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.ToggleButton_disabledAlpha, 0.5f);
syncTextState();
a.recycle();
}
this(context, attrs, com.android.internal.R.attr.buttonStyleToggle);
}
this(context, null);
}
public void setChecked(boolean checked) {
super.setChecked(checked);
syncTextState();
}
boolean checked = isChecked();
if (checked && mTextOn != null) {
setText(mTextOn);
} else if (!checked && mTextOff != null) {
setText(mTextOff);
}
}
return mTextOn;
}
mTextOn = textOn;
}
return mTextOff;
}
super.onFinishInflate();
updateReferenceToIndicatorDrawable(getBackground());
}
public void setBackgroundDrawable(Drawable d) {
super.setBackgroundDrawable(d);
updateReferenceToIndicatorDrawable(d);
}
if (backgroundDrawable instanceof LayerDrawable) {
LayerDrawable layerDrawable = (LayerDrawable) backgroundDrawable;
mIndicatorDrawable =
layerDrawable.findDrawableByLayerId(com.android.internal.R.id.toggle);
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mIndicatorDrawable != null) {
mIndicatorDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
}
}
}
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count)100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
private final ContentResolver mContentResolver;
private final Cursor mCursor;
private final String mOriginalUrl;
private final String mUrl;
private final String mUserAgent;
/* package */ BrowserActivity mActivity;
Cursor c, WebView view) { //构造方法
mActivity = activity;
mContentResolver = cr;
mCursor = c;
mOriginalUrl = view.getOriginalUrl();
mUrl = view.getUrl();
mUserAgent = view.getSettings().getUserAgentString();
}
mActivity = null;
mContentResolver = cr;
mCursor = c;
mOriginalUrl = null;
mUrl = url;
mUserAgent = null;
}
public Bitmap doInBackground(String... values) { //返回Bitmap类型
String url = values[0];
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream content = entity.getContent(); //将图标保存到InputStream中,因为是二进制内容
if (content != null) {
Bitmap icon = BitmapFactory.decodeStream( //从流中取出Bitmap,这里使用了BitmapFactory类的静态方法decodeStream
content, null, null);
return icon;
}
}
}
} catch (IllegalArgumentException ex) {
request.abort();
} catch (IOException ex) {
request.abort();
} finally {
client.close();
}
return null;
}
protected void onCancelled() {
if (mCursor != null) {
mCursor.close();
}
}
public void onPostExecute(Bitmap icon) {
if (mActivity != null) {
mActivity.mTouchIconLoader = null;
}
return;
}
icon.compress(Bitmap.CompressFormat.PNG, 100, os); //将Bitmap压缩成PNG编码,质量为100%存储
ContentValues values = new ContentValues(); //构造SQLite的Content对象,这里也可以使用raw sql代替
values.put(Browser.BookmarkColumns.TOUCH_ICON,os.toByteArray()); //写入数据库的Browser.BookmarkColumns.TOUCH_ICON字段
do {
mContentResolver.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI, mCursor.getInt(0)),values, null, null);
} while (mCursor.moveToNext());
}
mCursor.close();
}
}
private Time mCalendar;
private Drawable mMinuteHand; //分针
private Drawable mDial; //表盘背景
private int mDialHeight; //表盘高度
private float mMinutes;
private float mHour;
private boolean mChanged; //时间是否改变
this(context, null);
}
this(context, attrs, 0);
}
int defStyle) {
super(context, attrs, defStyle);
Resources r = mContext.getResources();
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.AnalogClock, defStyle, 0);
if (mDial == null) {
mDial = r.getDrawable(com.android.internal.R.drawable.clock_dial);
}
if (mHourHand == null) {
mHourHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_hour);
}
if (mMinuteHand == null) {
mMinuteHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_minute);
}
mDialHeight = mDial.getIntrinsicHeight(); //高度,同上
}
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mAttached = true;
IntentFilter filter = new IntentFilter(); //注册一个消息过滤器,获取时间改变、时区改变的action
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
}
}
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAttached) {
getContext().unregisterReceiver(mIntentReceiver); //反注册消息过滤器
mAttached = false;
}
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
float vScale = 1.0f;
hScale = (float) widthSize / (float) mDialWidth;
}
vScale = (float )heightSize / (float) mDialHeight;
}
resolveSize((int) (mDialHeight * scale), heightMeasureSpec));
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mChanged = true;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (changed) {
mChanged = false;
}
int availableHeight = mBottom - mTop;
int y = availableHeight / 2;
int w = dial.getIntrinsicWidth();
int h = dial.getIntrinsicHeight();
scaled = true;
float scale = Math.min((float) availableWidth / (float) w,
(float) availableHeight / (float) h);
canvas.save();
canvas.scale(scale, scale, x, y);
}
dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
dial.draw(canvas);
canvas.rotate(mHour / 12.0f * 360.0f, x, y); //计算时针旋转的角度,android123提示就是那个时针图片的旋转角度,直接反应的就是表盘上那个针的时间
final Drawable hourHand = mHourHand;
if (changed) {
w = hourHand.getIntrinsicWidth();
h = hourHand.getIntrinsicHeight();
hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
hourHand.draw(canvas);
canvas.restore();
canvas.rotate(mMinutes / 60.0f * 360.0f, x, y); //同理,分针旋转的角度
if (changed) {
w = minuteHand.getIntrinsicWidth();
h = minuteHand.getIntrinsicHeight();
minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
minuteHand.draw(canvas);
canvas.restore();
canvas.restore();
}
}
mCalendar.setToNow();
int minute = mCalendar.minute;
int second = mCalendar.second;
mHour = hour + mMinutes / 60.0f;
mChanged = true;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
String tz = intent.getStringExtra("time-zone");
mCalendar = new Time(TimeZone.getTimeZone(tz).getID());
}
invalidate(); //刷新屏幕,强制类调用onDraw方法实现分针时针的走动
}
};
阻止当前线程知道条件是open,或直到超时,这里参数long timeout为超时设置,Android123提示大家如果你们从事过Win32开发,这个方法类似DWORD WaitForSingleObject(HANDLE hHandle,DWORD dwMilliseconds); 函数。
阻止当前线程知道条件 open ,是上面的无超时等待重载版本。
重置条件为 close状态。
Open条件,释放所有线程的阻塞.
Android性能与调试很重要
ndroid提供了很多开发调试工具除了ADB、TraceView、Logcat外,今天这个名为Dev Tools的Android开发调试工具隐藏在Android模拟器中,为我们提供了强大的调试支持。我们在功能表中找到Dev Tools,运行后可以看到有很多条目,比如Development Settings,用来开发设置,进入后我们看到了比如Show CPU Usage这样的实用功能显示CPU占用率,帮助Android开发人员分析当前软件性能情况,今天就分析下Development Settings中的选项:
Enable ADB 启用ADB(android调试桥)
Show running processs (显示运行中的进程)
Show screen updates (显示屏幕更新)
Show CPU usage (显示CPU占用率)
Show background (显示北京)
Show Sleep state on LED (在休眠状态下LED开启)
Keep screen on while plugged in (保持屏幕开启当插入后)
Show GTalk service connection status (显示GTalk服务连接状态)