Skip to content

Instantly share code, notes, and snippets.

/**
* This function runs some actions after some delay
*
* @param delayInMilliSeconds the time delay in ms
* @param runnable the action to perform after the delay
* @return the Handler object, with which one can cancel this action.
*/
public static Handler runDelayedAction(int delayInMilliSeconds, Runnable runnable) {
final Handler handler = new Handler();
handler.postDelayed(runnable, delayInMilliSeconds);
/**
* Check if the app is debug version or not.
* This is done by checking "debuggable" attribute set in AndroidManifest.xml.
* <p/>
* Android Studio/ADT automatically sets this debuggable attribute to true, when not exporting
* a signed package.
*
* @param context
* @return
*/
final String key1 = "EMAIL";
final String key2 = "ACCESS_TOKEN";
mSomeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = getPackageManager().getLaunchIntentForPackage("com.abc.secondapp");
if(intent != null) {
Bundle extras = new Bundle();
extras.putString(key1, "SomeEmail@gmail.com");
/**
* This function captures any Android views to a bitmap shapshot
* and save it to a file path
* and optionally open it with android pciture viewer
*/
public void CaptureView(View view, String filePath, boolean bOpen) {
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
try {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//DoSth()
}
});
// Example usage:
// new GetJsonTask().execute("https://api.stackexchange.com/2.2/info?site=stackoverflow&key=app key");
class GetJsonTask extends AsyncTask<String, Void, String> {
private Exception exception;
protected String doInBackground(String... urls) {
try {
Request request = new Request.Builder()
.url(urls[0])
private static Activity getForegroundActivity(){
Activity activity = null;
try {
Class activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
activitiesField.setAccessible(true);
Map<Object, Object> activities = (Map<Object, Object>) activitiesField.get(activityThread);
if(activities == null)
/**
* Get version number from build.gradle
* You might also use BuildConfig.VERSION_CODE to see if this works
* @return version code integer
*/
public static int getAppVersionCode() {
if (context == null) return -1;
PackageManager packageManager = context.getPackageManager();
int versionCode = -1;
try {
@hkusoft
hkusoft / ObjectToString.cs
Created October 19, 2016 09:26
C#: Serialize and Deserialize Objects to and from xml strings
public static string SerializeObject<T> (T objectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer (objectToSerialize.GetType ());
using (StringWriter textWriter = new StringWriter ()) {
xmlSerializer.Serialize (textWriter, objectToSerialize);
return textWriter.ToString ();
}
}
@hkusoft
hkusoft / example_react_native.js
Last active May 17, 2017 02:31
Example template for React-Native JS file
var React = require('react');
var Foo = require('Foo');
/**
* General component description, for instance use below description
*
* The button component represents generic button UI control with
* an image and a title sting.
*
* props: