Skip to content

Instantly share code, notes, and snippets.

@li2
li2 / HelloGist.md
Last active May 6, 2018 11:48
First gist note test #tags: git

This a my first gist note.

printf("Hello world.")
@li2
li2 / SdcardService.java
Last active March 20, 2018 11:59
Auto run a shell script when SDCard is plugged in android device. 实现方式:启动一个 service,由 service 动态注册监听 sdcard 插拔事件的 receiver,当接收到系统发出的插拔事件的 broadcast 后,执行相应的操作。#tags: android-service
public class SDcardService extends Service {
@Override
public IBinder onBind(Intent intent) {
// Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
@li2
li2 / Android Studio .gitignore
Last active March 20, 2018 11:18 — forked from iainconnor/Android Studio .gitignore
A .gitignore for use in Android Studio #tags: git
# Built application files
/*/build/
# Crashlytics configuations
com_crashlytics_export_strings.xml
# Local configuration file (sdk path, etc)
local.properties
# Gradle generated files
@li2
li2 / ForegroundLinearLayout.java
Last active March 20, 2018 05:22 — forked from chrisbanes/ForegroundLinearLayout.java
ForegroundLinearLayout. An layout which supports a foreground drawable. Base on this class, we can implement ripple effect on layout. #tags: android-view, android-animation
/*
* Copyright (C) 2006 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
*
* Unless required by applicable law or agreed to in writing, software
@li2
li2 / toolbar.xml
Last active March 20, 2018 06:52
[移除 Toolbar 左侧空白. Remove left margin from actionbars custom layout.] https://stackoverflow.com/a/32320632/2722270 #tags: android-view
<android.support.v7.widget.Toolbar
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
@li2
li2 / translucent_theme_activity.xml
Last active May 6, 2018 11:44
实现背景半透明的 Android Activity?How to get appcompat translucent theme with support actionbar?通过修改 theme 的某些属性字段值来实现。 #tags: android-activity
<!--Refer to: http://stackoverflow.com/questions/20862258/android-how-to-get-appcompat-translucent-type-theme-with-support-actionbar-->
<!-- /res/values/styles.xml -->
<style name="Theme.AppCompat.Translucent">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
@li2
li2 / DialogThemeActivity.java
Last active May 6, 2018 11:44
实现 alert dialog 样式的 activity. Implement a dialog theme activity. #tags: android-activity
import android.app.AlertDialog;
// Refer to: http://stackoverflow.com/questions/1979369/android-activity-as-a-dialog
public class DialogThemeActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this)
.setTitle("title")
@li2
li2 / RandomInt.java
Last active March 20, 2018 07:09
[如何生成指定范围内的随机数?How can i generate random number in specific range in android?] http://stackoverflow.com/questions/6029495/how-can-i-generate-random-number-in-specific-range-in-android #tags: java
/**
* Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and
* the specified value (exclusive), drawn from this random number generator's sequence.
* 返回一个在 [0, n-1] 之间均匀分布的伪随机数.
*
* @return [0, n-1]
*/
int nextInt(int n);
@li2
li2 / coordinatorLayout_overlapping.diff
Last active July 1, 2018 05:39
解决 ViewPager 和 AppBarLayout 重叠问题. Android support AppBarLayout overlapping content. Add app:layout_behavior to ViewPager, this changes the View height to be below the AppBarLayout rather than full match_parent height. #tags: android-activity
<!-- http://stackoverflow.com/questions/30674648/android-design-support-tablayout-overlapping-content -->
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
@li2
li2 / IsInstanceOf.java
Last active March 20, 2018 07:09
[如何判断某个对象是否继承自某类? How to determine an object's class (in Java)?] http://stackoverflow.com/questions/541749/how-to-determine-an-objects-class-in-java #tags: java
/**
* Method #0:操作符(或者说关键字)instanceof
* object 是 C 的实例吗?
*/
if (object instanceof C)
;
/**
* Method #1: isInstance(Object object)
* Tests whether the given object can be cast to the class represented by this Class.