Last active
December 23, 2015 08:08
-
-
Save ChrisZou/6605082 to your computer and use it in GitHub Desktop.
Android commonly used code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Toaster { | |
public static void s(Context context, int msg) { | |
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); | |
} | |
public static void l(Context context , int msg) { | |
Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Download a file from the given url and save it to the given file path | |
* @param urlString | |
* @param filepath | |
*/ | |
public void downloadFile(String urlString, String filepath) { | |
try { | |
URL url = new URL(urlString); | |
URLConnection conn = url.openConnection(); | |
conn.connect(); | |
InputStream is = conn.getInputStream(); | |
final int fileSize = conn.getContentLength(); | |
if (fileSize <= 0) { | |
return; | |
} | |
if (is == null) { | |
return; | |
} | |
FileOutputStream fos = new FileOutputStream(filepath); | |
byte buf[] = new byte[1024]; | |
int numread = 0; | |
while ((numread = is.read(buf)) != -1) { | |
fos.write(buf, 0, numread); | |
} | |
is.close(); | |
fos.close(); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String respondseToString(HttpResponse response) { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); | |
StringBuilder builder = new StringBuilder(); | |
for (String line = null; (line = reader.readLine()) != null;) { | |
builder.append(line).append("\n"); | |
} | |
return builder.toString(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String getContent(String urlString) { | |
try { | |
URL url = new URL(urlString); | |
InputStream is = url.openStream(); | |
ByteArrayOutputStream data = new ByteArrayOutputStream(); | |
byte[] buffer = new byte[1024]; | |
int len = 0; | |
while ((len = is.read(buffer)) != -1) { | |
data.write(buffer, 0, len); | |
} | |
is.close(); | |
String str = new String(data.toByteArray(), "utf-8"); | |
data.close(); | |
return str; | |
} catch (Exception ee) { | |
System.out.print("ee:" + ee.getMessage()); | |
} | |
return null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void toGooglePlay(String packageName) { | |
try { | |
Intent intent = new Intent(Intent.ACTION_VIEW); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
intent.setData(Uri.parse("market://details?id=" + packageName)); | |
startActivity(intent); | |
} catch (ActivityNotFoundException e) { | |
Toast.makeText(this, R.string.toast_no_market_app, Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Helper class for android Logcat functions | |
* @author chuang | |
* | |
*/ | |
@SuppressLint("DefaultLocale") | |
public class L { | |
private static String TAG = "zyzy"; | |
private static boolean DEBUG = true; | |
private static final String CLASS_METHOD_LINE_FORMAT = "%s.%s() Line:%d---------%s"; | |
/** | |
* Log debug method. | |
* @param caller the Class that calls this method. | |
* @param msg | |
*/ | |
public static void l() { | |
if(DEBUG) { | |
StackTraceElement traceElement = Thread.currentThread().getStackTrace()[3];// 从堆栈信息中获取当前被调用的方法信息 | |
String className = traceElement.getClassName(); | |
String simpleClassName = className.contains(".")?className.substring(className.lastIndexOf(".")+1):className; | |
String logText = String.format(CLASS_METHOD_LINE_FORMAT, simpleClassName, traceElement.getMethodName(), | |
traceElement.getLineNumber()); | |
Log.d(TAG, logText); | |
} | |
} | |
/** | |
* Log debug method. | |
* @param caller the Class that calls this method. | |
* @param msg | |
*/ | |
public static void l(Object msg) { | |
if(DEBUG) { | |
StackTraceElement traceElement = Thread.currentThread().getStackTrace()[3];// 从堆栈信息中获取当前被调用的方法信息 | |
String className = traceElement.getClassName(); | |
String simpleClassName = className.contains(".")?className.substring(className.lastIndexOf(".")+1):className; | |
String logText = String.format(CLASS_METHOD_LINE_FORMAT, simpleClassName, traceElement.getMethodName(), | |
traceElement.getLineNumber(), msg.toString()); | |
Log.d(TAG, logText); | |
} | |
} | |
public static void trace() { | |
StackTraceElement[] traceElements = Thread.currentThread().getStackTrace(); | |
Log.d(TAG, "trace-------------------------------------------------------------------------"); | |
int len = traceElements.length; | |
for (int i=len-1; i>=0; i--) { | |
StackTraceElement traceElement = traceElements[i]; | |
String logText = String.format(CLASS_METHOD_LINE_FORMAT, traceElement.getClassName(), traceElement.getMethodName(), | |
traceElement.getLineNumber(), traceElement.getFileName()); | |
Log.d(TAG, logText);// 打印Log | |
} | |
Log.d(TAG, "end trace-------------------------------------------------------------------------"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//START: add this block | |
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
// replace with the current version of the Android plugin | |
classpath 'com.android.tools.build:gradle:0.12.1' | |
// the latest version of the android-apt plugin | |
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' | |
} | |
} | |
//END | |
apply plugin: 'com.android.application' | |
//START: Add this line | |
apply plugin: 'com.neenbedankt.android-apt' | |
//END | |
android { | |
compileSdkVersion 19 | |
buildToolsVersion "20.0.0" | |
defaultConfig { | |
applicationId "com.chriszou.studyharder" | |
minSdkVersion 14 | |
targetSdkVersion 19 | |
} | |
buildTypes { | |
release { | |
runProguard false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' | |
} | |
} | |
} | |
dependencies { | |
//START: Add these two lines | |
apt "org.androidannotations:androidannotations:3.0.1" | |
compile "org.androidannotations:androidannotations-api:3.0.1" | |
//END | |
compile project(':androidlibs') | |
} | |
//START: Add this block | |
apt { | |
arguments { | |
resourcePackageName android.defaultConfig.applicationId | |
androidManifestFile variant.processResources.manifestFile | |
} | |
} | |
//END | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Singleton { | |
private volatile static Singleton singleInstance; | |
private Singleton() { | |
} | |
public static Singleton getInstance() { | |
if(singleInstance==null) { | |
synchronized(Singleton.class) { | |
if(singleInstance==null) { | |
singleInstance = new Singleton(); | |
} | |
} | |
} | |
return singleInstance; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void writeToFile(String str) { | |
BufferedWriter writer = null; | |
try { | |
writer = new BufferedWriter(new FileWriter("filename.txt")); | |
writer.write(str); | |
} catch (IOException e) { | |
L.l("get ex wrting file"); | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (writer != null) { | |
writer.close(); | |
} | |
} catch (IOException e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment