Delete all cached file after uninstall Android app

Overview

When running app, app will create a lot of cached files. If user uninstall app, I think that these files need to be removed. I suggest a solution for this issue:

  • Write cached file to External Cached Directory
  • If the user uninstalls your application, these files will be automatically deleted.

 

How to implement

You can also access a cache directory on a secondary external storage (if available) by calling ContextCompat.getExternalCacheDirs()

Integrating Google photo to Android application

Overview

Picasa – Google photo is a fast and easy sharing photo from Google. We can access Google photo album by integrating Android application.

Our steps:

  • Ask user to pick a google account
  • When we get the account back, ask for access to the picasa picture
  • Then we get our token
  • Get albums and images from picasa by our token

How to integrating

  1. Adding libraries by gradle
    compile 'com.google.android.gms:play-services:9.0.1'
    compile group: 'com.google.gdata', name: 'core', version: '1.47.1'
    
  2. Ask user to pick a google account
    Intent googlePicker = AccountPicker.newChooseAccountIntent(null, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null);
    super.startActivityForResult(googlePicker, Config.PICK_PICASA_ALBUM);
    
  3. Get token
    @Override
    public void onActivityResult(int requestCode, int result, Intent data) {
        if (result == RESULT_OK && requestCode == Config.PICK_PICASA_ALBUM) {
            super.onActivityResult(requestCode, result, data);
            this.email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            this.type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
            this.getTokenOfPicasa();
        }
    }
    
    private void getTokenOfPicasa() {
        Account account = new Account(this.email, this.type);
        AccountManager accountManager = AccountManager.get(this);
        accountManager.getAuthToken(account, "lh2", null, this,
            new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> result) {
                    try {
                        Bundle bundle = result.getResult();
                        if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                            authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                            new GetAlbumsOfPicasa().execute();
                        }
                    } catch (Exception e) {
                            Log.e(TAG, "getTokenOfPicasa", e);
                    }
                }
            }
        }, null);
    }
    
  4. Get albums and images
    private class GetAlbumsOfPicasa extends AsyncTask<Void, Void, Void> {
        private List<Album> albums;
    
        @Override
        protected Void doInBackground(Void... params) {
            // Get albums and images of Picasa
            getAlbumsOfPicasa(email, authToken);
            return null;
        }
    
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
    }
    
    public List<AlbumEntry> getAlbumsOfPicasa(String email, String authToken) {
        try {
            String API_PREFIX = "https://picasaweb.google.com/data/feed/api/user/";
    
            PicasawebService picasaService = new PicasawebService("pictureframe");
            picasaService.setUserToken(authToken);
    
            String albumUrl = API_PREFIX + email;
            UserFeed userFeed = picasaService.getFeed(new URL(albumUrl), UserFeed.class);
    
            List<GphotoEntry> entries = userFeed.getEntries();
            List<AlbumEntry> albums = new ArrayList<>();
            for (GphotoEntry entry : entries) {
                AlbumEntry albumEntry = new AlbumEntry(entry);
                albums.add(albumEntry);
            }
            return albums;
        } catch (Exception e) {
            Log.e(TAG, "getAlbumsOfPicasa", e);
            return null;
        }
    }
    

OnTextChanged with ButterKnife

XML file

<EditText
    android:id="@+id/feature_manager_search"
    android:layout_width="match_parent"
    android:layout_height="@dimen/row_feature_search_height"/></pre>

Java file

@OnTextChanged(R.id.feature_manager_search)
protected void onTextChanged(CharSequence text) {
    String featureName = text.toString();
    Log.i(TAG, "feature name:" + featureName);
}

Building Apps with Over 65K Methods

Overview

As the Android platform has continued to grow, so has the size of Android apps. When your application and the libraries it references reach a certain size, you encounter build errors that indicate your app has reached a limit of the Android app build architecture. Earlier versions of the build system report this error as follows:

Conversion to Dalvik format failed:
Unable to execute dex: method ID not in [0, 0xffff]: 65536

Both these error conditions display a common number: 65,536. This number is significant in that it represents the total number of references that can be invoked by the code within a single Dalvik Executable (dex) bytecode file. If you have built an Android app and received this error, then congratulations, you have a lot of code! This document explains how to move past this limitation and continue building your app.

About the 65K Reference Limit

Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.

How to implement

    • Import to build.gradle

      android {
          compileSdkVersion 21
          buildToolsVersion "21.1.0"
      
          defaultConfig {
              ...
              minSdkVersion 14
              targetSdkVersion 21
              ...
      
              // Enabling multidex support.
              multiDexEnabled true
          }
          ...
      }
      
      dependencies {
        compile 'com.android.support:multidex:1.0.0'
      }
      


    • Create application file
public class SimplePrintsApp extends MultiDexApplication
    ...
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    ...
}
    • Import to AndroidManifest.xml
<application
        android:name="me.storytree.simpleprints.SimplePrintsApp"

In conclusion

This above solution will help us to create application that has many libraries and method.

Simple Splash screen on Android

Overview

Splash screen is the first screen of application. It often shows your logo, slogan  in a few seconds. I will introduce a simple way to implement Splash screen on Android application

1418337663850-2014-12-11 15.16.39

How to implement

Add SplashActivity.java

public class SplashActivity extends BaseActivity {
    private static final String TAG = SplashActivity.class.getSimpleName();

    private static final int TIME_TO_DISPLAY_SPLASH_SCREEN = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_splash);
        this.launchGreenFood();
    }

    private void launchGreenFood() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                goToMainScreen();
            }
        }, TIME_TO_DISPLAY_SPLASH_SCREEN);
    }

    private void goToMainScreen() {
        Intent intent = new Intent(this, MainActivity.class);
        super.startActivity(intent);
        super.finish();
    }
}

Add activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>
</RelativeLayout>

Declare SplashActivity  in manifest

<activity
            android:name=".view.activity.SplashActivity"
            android:theme="@style/AppThemeNoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

In summary

This solution is easy way to implement Splash screen. You can add business login in SplashActivity such as: load database, init app, load data from server.

Install Genymotion for testing app

Overview

Genymotion is a system that provide many Android virtual devices for testing. Genymotion is 100% compliant with Android APIs because we follow exactly the same rules as every Android device factory.

You can research about Genymotion in this link

How to install

To install Genymotion, follow the steps corresponding to your operating system.