Skip to content

Instantly share code, notes, and snippets.

@bluemyria
Last active March 12, 2018 12:39
Show Gist options
  • Save bluemyria/e883e1f9e780d94881868c4e6e6a6f48 to your computer and use it in GitHub Desktop.
Save bluemyria/e883e1f9e780d94881868c4e6e6a6f48 to your computer and use it in GitHub Desktop.
Android - 016 - Implicit Intents
//////////////////////////////////////////////////////////////////////////////////////////////
// in AndroidManifest.xml
//////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package=".......w3t1_impliziteintents_mariab">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Bildanzeige">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
</application>
</manifest>
//////////////////////////////////////////////////////////////////////////////////////////////
// in MainActivity
//////////////////////////////////////////////////////////////////////////////////////////////
public class MainActivity extends AppCompatActivity {
private Button btnAnrufen;
private Button btnAnrufEinleiten;
private Button btnMaps;
private Button btnSMS;
private Button btnFoto;
private ImageView ivBild;
private void init() {
MyOCL ocl = new MyOCL();
btnAnrufen = findViewById(R.id.btnAnrufen);
btnAnrufen.setOnClickListener(ocl);
btnAnrufEinleiten = findViewById(R.id.btnAnrufEinleiten);
btnAnrufEinleiten.setOnClickListener(ocl);
btnMaps = findViewById(R.id.btnMaps);
btnMaps.setOnClickListener(ocl);
btnSMS = findViewById(R.id.btnSMS);
btnSMS.setOnClickListener(ocl);
btnFoto = findViewById(R.id.btnFoto);
btnFoto.setOnClickListener(ocl);
ivBild = findViewById(R.id.ivBild);
String[] permissions = {Manifest.permission.CALL_PHONE,
Manifest.permission.SEND_SMS,
Manifest.permission.CAMERA};
requestPermissions(permissions, 1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private class MyOCL implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnAnrufen:
anrufen();
break;
case R.id.btnAnrufEinleiten:
anrufEinleiten();
break;
case R.id.btnMaps:
maps();
break;
case R.id.btnSMS:
sms();
break;
case R.id.btnFoto:
foto();
break;
}
}
}
/*
* Eine implizite Intent ist ein Aufruf einer App die wir nicht explizit festlegen, sondern
* deren Name dem System bekannt zB das Telefonieren. Wir sagen mittels "Intent.ACTION_CALL",
* dass eine bestimmte Nummer aufgerufen werden soll und das System ruft diese Nummer an
* oder schlägt uns alle Apps vor, die in Frage kommen, von denen wir eine auswählen
*
* Die Info, die diese aufgerufene App verarbeiten soll, wird als URI uebergeben. Der Aufbau
* ist immer der gleiche: Protokoll - Doppelpunkt - Info
*/
private void anrufen() {
Uri uri = Uri.parse("tel:+49 1234 567890");
Intent intent = new Intent(Intent.ACTION_CALL, uri);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
==PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
} else {
Toast.makeText(this,"keine Anruf-Berechtigung", Toast.LENGTH_LONG).show();
}
}
private void anrufEinleiten() {
Uri uri = Uri.parse("tel:+49 1234 567890");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
==PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
} else {
Toast.makeText(this,"keine Anruf-Berechtigung", Toast.LENGTH_LONG).show();
}
}
private void maps() {
try {
Uri uri = Uri.parse("geo:48.0571652,11.6588264");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} catch (ActivityNotFoundException ex) {
Toast.makeText(this,"keine App gefunden", Toast.LENGTH_LONG).show();
}
}
private void sms() {
Uri uri = Uri.parse("sms:+49 1234 567890?body=Hallo Maria");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
==PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
} else {
Toast.makeText(this,"keine SMS-Berechtigung", Toast.LENGTH_LONG).show();
}
}
private void foto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
==PackageManager.PERMISSION_GRANTED) {
startActivityForResult(intent,0);
} else {
Toast.makeText(this,"keine Kamera-Berechtigung", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((resultCode==RESULT_OK) && (requestCode==0)) {
Bundle extras = data.getExtras();
// Bitmap aus dem Bundle lesen, Key ist "data"
Bitmap bm = (Bitmap)extras.get("data");
// Bild in der ImageView anzeigen
ivBild.setImageBitmap(bm);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
// in Bildanzeige
//////////////////////////////////////////////////////////////////////////////////////////////
public class Bildanzeige extends AppCompatActivity {
private ImageView bild;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bildanzeige);
bild = findViewById(R.id.bild);
Intent intent = getIntent();
Uri uri = intent.getData();
bild.setImageURI(uri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment