2

Here is a part of my preference.xml.

 <Preference
            android:summary="Write me"
            android:title="Title">
            <intent
                android:action="android.intent.action.VIEW"
                android:data="mailto:support@xxxxx.com"
                />
        </Preference>

When i'm clicking to this preference i have a crash with

 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=mailto:xxxxxx.x@x-xxxxxx.xxx }

What do i do wrong?

This is my class for preference. I read lots of theads but don't find an answer :

public class Preferences extends PreferenceActivity  implements SharedPreferences.OnSharedPreferenceChangeListener {
    public static final String KEY_PREF_INSTANT_PRINT = "instantPrinting";
    public static final String KEY_PREF_INSTANT_PRINT_SCREEN = "instantPrintingScreen";
    public static final String KEY_PREF_PAY_BUTTONS = "paymentTypes";

    @Override
    protected void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        addPreferencesFromResource(R.xml.preference);
        Preference instantPrintingScreen = findPreference(KEY_PREF_INSTANT_PRINT_SCREEN);
        instantPrintingScreen.setEnabled(sharedPref.getBoolean(KEY_PREF_INSTANT_PRINT, false));
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                          String key) {
        if (key.equals(KEY_PREF_INSTANT_PRINT)) {
            Preference connectionPref = findPreference(KEY_PREF_INSTANT_PRINT_SCREEN);
            connectionPref.setEnabled(sharedPreferences.getBoolean(key, false));
        }
    }
}
Andrew
  • 79
  • 9

1 Answers1

1

The preference will work perfectly, the problem here is that you don´t have any client application to handle an email.

I have installed 3 applications that can handle the email, so if i try to open the preference i see the applications:

enter image description here

enter image description here

The problem is because your device doesn´t have any app that can open the scheme mailto: or can handle email.


How to validate ActivityNotFoundException: No Activity found to handle Intent?

Well in this case you are opening directly from the layout the intent :

<intent
            android:action="android.intent.action.VIEW"
            android:data="mailtoa:support@xxxxx.com"
            />

So, as an option you can make a pre verification to establish if you can open the email with some app installed in your android device.

public boolean canOpenEmail(){
    try {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:support@xxxxx.com")));
    }catch (ActivityNotFoundException afe){
        Log.e(TAG, afe.getMessage());
        return false;
    }
    return true;
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268