Skip to content

Instantly share code, notes, and snippets.

View rosuH's full-sized avatar
🥰
Healthy living, happy coding

rosuH

🥰
Healthy living, happy coding
View GitHub Profile
@rosuH
rosuH / Loop.java
Last active November 1, 2017 08:24
Use the modulo operator to implement loops
/*
* use modulo operator to implement loops
*/
public class Loop {
// index
private static int index;
// a objects array
private static obj[] mObjs = new obj[]{
new obj(0),
@rosuH
rosuH / SaveDataToDealWithAutoRotate.java
Last active November 3, 2017 08:11
Save data to deal with auto-rotate
/**
* All activity data would be destoryed after the device rotate
* So we need save the data first and recall it after the device rotate
*/
public class MainActiviy extends AppCompatActivity {
// example key variable
private static final String KEY_INDEX = "index";
// example data variable
@rosuH
rosuH / preview_layout.xml
Created November 3, 2017 08:01
Preview the layout during editing XML files.
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.PreviewLayoutActivity">
<TextView
android:id="@+id/answer_text_view"
android:layout_width="wrap_content"
@rosuH
rosuH / OptimizeIntent.java
Created November 9, 2017 07:55
Encapsulate implementation details in a static method to optimize intent communication
/**
* public name: SendActivity
* comment: start activity(using intent) by calling static final method instead of creating one by itself.
*/
public class SendActivity extends AppCompatActivity{
...
//Start Receiver Activity
boolean exampleKey = someFunToGetExampleKey();
Intent intent = ReceiveActivity.newIntent(SendActivity.this, exampleKey);
startActivity(intent);
@rosuH
rosuH / getTheFIles.sh
Created November 22, 2017 06:25
Open files in shell
#!/bin/bash
var="$1"
xdg-open $(locate $var) > 2&
accerciser
adobe-source-code-pro-fonts
adobe-source-han-sans-cn-fonts
adobe-source-han-serif-cn-fonts
adwaita-icon-theme
aisleriot
alsa-utils
android-studio
anjuta
archlinuxcn-keyring
@rosuH
rosuH / Regex.java
Created February 5, 2018 09:54
Regex_Java_Filter_out_special_symbols
// global search REGEX
String regex="[^\\u4e00-\\u9fa5\\w]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(data);
data = matcher.replaceAll("");
@rosuH
rosuH / WebVIewMemoryLeaks.java
Created February 22, 2018 02:14
Avoid webview memory leaks
@Override
protected void onDestroy() {
if(mWebView !=null ){
mWebView.loadDataWithBaseURL(null, "", "text/html",
"utf-8", null);
((ViewGroup)mWebView.getParent()).removeView(mWebView);
mWebView.destroy();
mWebView = null;
}
super.onDestroy();
@rosuH
rosuH / ParameterConstructor.java
Last active March 9, 2018 16:07
Pass parameter from default constructor to Custom constructor.
import java.util.Random;
public class Main {
public static void main(String[] args){
B b = new B();
System.out.println("-----");
B bB = new B(12);
}
}
@rosuH
rosuH / TakePhoto.java
Last active March 17, 2018 02:58
Launch camera to take photo by intent.
public class MyFregment extends Fregment {
private static final int REQUEST_PHOTO = 2;
@Override
public View onCreateView(layoutflater inflater, ViewGroup container, Bundle savedinstanceState) {
mPhotoButton = (imageButton) v.findViewById(R.id.camera);
// 默认只能拍出低分辨率图片
// 要想获得全尺寸照片,要让它使用文件系统存储图片
// 需要传入保存在 MediaStore.EXTRA_OUTPUT 中的 Uri,该 Uri 会指向 FileProvider 提供的位置
final Intent captureImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);