Intents für Mail und Website
Beispielhaft wird anhand eines Impressums eine Beispiel für implizite Intents gegeben. Die Intents befinden sich in einem Fragment.
Xml Datei
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".ImprintFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Impressum"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="Angaben gemäß § 5 TMG"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:text="David Höll"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Südstraße 33"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:text="48153 Münster"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kontakt"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-Mail: info@hl-dev.de"
android:autoLink="email"
android:textSize="16sp" />
<TextView
android:id="@+id/tvWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:text="Website: https://hl-dev.de"
android:autoLink="web"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Redaktionell verantwortlich"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="David Höll"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Südstraße 33"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="48153 Münster"
android:textSize="16sp" />
</LinearLayout>
Java Datei
public class ImprintFragment extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_imprint, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView tvEmail = view.findViewById(R.id.tvEmail);
tvEmail.setOnClickListener(v -> callMailApp());
TextView tvWebsite = view.findViewById(R.id.tvWebsite);
tvWebsite.setOnClickListener(v -> callBrowserApp());
}
private void callBrowserApp() {
String url = "https://hl-dev.de";
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getContext(), "No browser app installed", Toast.LENGTH_LONG).show();
}
}
private void callMailApp() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plan/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"info@hl-dev.de"});
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getContext(), "No mailing app installed", Toast.LENGTH_LONG).show();
}
}
}