Search code examples
androidandroid-intentpreference

Share the App email send from PreferenceScreen


I am trying to create a feature in my Android App which will let users share the link to my app in android market to whoever they want to email it to.

In preferences.xml I have created this as below

<PreferenceScreen
            android:title="Share the App" 
            android:summary="Share the App with your friends.">

        <intent android:action="" />

</PreferenceScreen>

I am not sure what intent should I put here and how I do I handle it when Share the App is clicked in PreferenceScreen. I want to open Email and pre-populate it with a subject and the Android Market link of the App in the subject.

The user will enter the email and send it to their friends.


Solution

  • Here is what I did and it worked.

    preferences.xml

    <PreferenceScreen
                android:title="Share the App" 
                android:summary="Share the App with your Friends.">
    
            <intent android:action="myapp.action.SHARE_APP" />
    
    </PreferenceScreen>
    

    Then I added this to the AndroidManifest.xml

    <activity android:name=".ShareApp"
          android:theme="@style/Theme.MyApp">
          <intent-filter>
            <action android:name="myapp.action.SHARE_APP" />
            <category android:name="android.intent.category.DEFAULT"/>                      
          </intent-filter>
    </activity>
    

    Then I created a ShareApp.java and added the code here.

    public class ShareApp extends UrSettings {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("text/plain"); 
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hey there! Cheers!");
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Try MyApp!"); 
            startActivity(emailIntent);  
            super.onCreate(savedInstanceState);
        }
    }
    

    And it worked!! By the way the UrSettings class is extended from PreferenceActivity.