Last active
July 8, 2019 10:49
-
-
Save baruckis/f3d4e8eac019b61eb0a8835c8db48939 to your computer and use it in GitHub Desktop.
Send Email Feedback Intent in Kotlin for Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2019 Andrius Baruckis www.baruckis.com | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import android.content.ActivityNotFoundException | |
import android.content.Context | |
import android.content.Intent | |
import android.net.Uri | |
import android.util.Log | |
import android.widget.Toast | |
import androidx.core.content.ContextCompat.startActivity | |
fun sendEmailFeedback(context: Context, appVersionName: String = "") { | |
val to = "your@email.com" | |
val subject = "User Feedback for Android $appVersionName" | |
val body = "My feedback: " | |
val uriBuilder = StringBuilder("mailto:" + Uri.encode(to)) | |
uriBuilder.append("?subject=" + Uri.encode(subject)) | |
uriBuilder.append("&body=" + Uri.encode(body)) | |
val uriString = uriBuilder.toString() | |
val intent = Intent(Intent.ACTION_SENDTO, Uri.parse(uriString)) | |
try { | |
startActivity(context, intent, null) | |
} catch (e: Exception) { | |
Log.e("LOG_TAG", e.localizedMessage) | |
// If there is no email client application, than show error message for the user. | |
if (e is ActivityNotFoundException) { | |
Toast.makeText( | |
context, | |
"No application can handle this request. Please install an email client app.", | |
Toast.LENGTH_LONG).show() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment