How to implement "share" functionality for Android app

Hi,

Today basics of implementing the share action which should let share content via e-mail, SMS message, etc. The common way would most likely be to use the action bar and it's menu icons.
So first step is to add 'share' menu item:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >


<item
    android:id="@+id/menu_item_share"
    android:title="@string/share_action"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    app:showAsAction="always"/>


We use ShareActionProvider widget from support library to account for older devices.

Next step is to override onCreateOptionsMenu method in your MainActivity:

ShareActionProvider mShareActionProvider;
Intent mShareIntent;
 
public boolean onCreateOptionsMenu(Menu menu) {
    // this is common just inflate the menu
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem shareItem = menu.findItem(R.id.menu_item_share);

    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
    if (mShareActionProvider != null) {
        if (mShareIntent == null) {
            mShareIntent = new Intent();
            mShareIntent.setAction(Intent.ACTION_SEND);
            mShareIntent.setType("text/plain");

    // here you can put data that you would like to share
            mShareIntent.putExtra(Intent.EXTRA_TEXT, "SOME_SHARE_DATA");
        }
        mShareActionProvider.setShareIntent(mShareIntent);
    }

    return true;

}

And that's basically it. Usually you don't need to do anything special in the implementation of onOptionsItemSelected.

Important
You may notice that it works in the debug mode / locally but when you deploy to live it does not. That may happen if you use ProGuard. Following line should be added to the proguard configuration in order to make it work:
# to keep shareactionprovider working
-keep class android.support.v7.widget.ShareActionProvider { *; }



Cheers!

Create bootable Win 7 USB pendrive on Ubuntu

Hiya!

If anyone is looking for how to install Windows 7 from USB drive here it is. This guide is for people who run Linux.

One of the most popular and common tool for creating bootable pendrive is called unetbootin and will be using it for completing this task.

First of all you need to format your USB device to ntfs. You can do it using gparted tool (be careful to choose correct drive ;)

You may also need to install support for NTFS:
sudo apt-get install ntfs-3g

If you want your device to be bootable you can set such flag in gparted itself.

Here is the version of unetbootin that happens to support the NTFS:
http://sourceforge.net/projects/unetbootin/files/UNetbootin/494/unetbootin-linux-494/download


After you run unetbootin follow on screen instruction, i.e. select image from ISO,
and select the correct drive - you may need to tick the checkbox called: "Show all drives". Make sure you know what drive is your USB to avoid destroying data on your real drive - follow at your own risk of course.

That's it.

ssh-add problems with SSH agent and related

Hi,

Some time ago we have posted about resolving git errors related to permissions denied for the ssh keys: http://devmain.blogspot.co.uk/2014/09/git-error-permission-denied-publickey.html

The solution was to try running the following command:
ssh-add

However what if running it results in the following error:
"Could not open a connection to your authentication agent"

Well there is a solution to that one too :)

It may mean that the SSH agent is not running and you can try to start it prior to running the command. If you run GIT bash on Windows you can try the following command:
eval `ssh-agent -s`

or if that's not the case you can try directly start ssh-agent

After that just try again with ssh-add.

Hope that helps someone.

Cheers!

Java Mail API for Android and proguard errors

Hi,

If you would like to send e-mails directly from within your mobile app you can use Java Mail API.  Here is the special lightweight port for Android:
https://code.google.com/p/javamail-android/

You will need to include all 3 jar files in your libs folder:
  • additional.jar
  • mail.jar
  • activation.jar
Then you can use common way to implement sending e-mails. Both should work: TLS and SSL.

Everything seems to be working fine until you are ready to publish your app. If you are going to export signed APK and use ProGuard to obfuscator you may notice errors and warning for proguard saying about missing classes, references or jars.

In order to fix just add the following rules to your proguard config file:
-dontwarn java.awt.**
-dontwarn java.beans.Beans
-dontwarn javax.security.**

-keep class javamail.** {*;}
-keep class javax.mail.** {*;}
-keep class javax.activation.** {*;}

-keep class com.sun.mail.dsn.** {*;}
-keep class com.sun.mail.handlers.** {*;}
-keep class com.sun.mail.smtp.** {*;}
-keep class com.sun.mail.util.** {*;}
-keep class mailcap.** {*;}
-keep class mimetypes.** {*;}
-keep class myjava.awt.datatransfer.** {*;}
-keep class org.apache.harmony.awt.** {*;}
-keep class org.apache.harmony.misc.** {*;}



This may or may not work for you but it did the trick for me :)

How to: backup and restore MySQL data

Dumping/backup table for the MySQL database
mysqldump -u <dbuser> -h <dbhost> -p <dbname> tablename > tablename.sql

Restoring data
mysql -u <dbuser> -h <dbhost> -p <dbname>
mysql> source <path>/tablename.sql

OR
mysql -u <dbuser> -h <dbhost> -p db_name < /path/to/tablename.sql