Generating SHA-1 for Google API

When you want to interface with any Google API e.g. YouTube from within your Android App, you need to register your app in the developer console.

You have to provide a SHA-1 key which can be generated as following:

Command line

$ chmod u+x gradlew
$ ./gradlew signingReport

Passing data between AndroidActivities

Sending data

private void openVideoActivity(String video) {
    Intent newActivity = new Intent(this, PlayerActivity.class);
    newActivity.putExtra("videoId", video);
    startActivity(newActivity);
}

Retrieving data

final String videoID = getIntent().getExtras().getString("videoID");

In Kotlin it looks like this:

val intent = Intent(this, NewActivity::class.java).apply {
    putExtra("EXTRA_MESSAGE", "Test 123")
}
startActivity(intent)

Retrieving

val message = intent.getStringExtra("EXTRA_MESSAGE")

Features and Permissions in Android

Features and Permissions

A permission is something an app is allowed to do 🙂

E.g. using the camera, reading location or accessing your contacts.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />

Internet Access

If your app uses e.g. web-APIs you need internet access. This can be achieved by adding android.permission.INTERNET to your manifest. In this case you need the uses-permission tag.

<manifest package="de.creatronix.sample_app"
          xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

INTERNET

Allows applications to open network sockets.

ACCESS_NETWORK_STATE

Allows applications to access information about networks.

Non-TLS APIs

When You have resources which do not use TLS (https but just http) you have to add

<application
        ...
        android:usesCleartextTraffic="true">
</application>

 

Permissions in Services

Checking Permission

import android.content.pm.PackageManager;
int uid = getCallingUid(); 
int pid = getCallingPid(); 
Log.d(LOGTAG, "Message from PID " + pid + " UID " + uid);
int perm = mContext.checkPermission("de.creatronix.my_service.MY_PERM", pid, uid); 
if (perm == PackageManager.PERMISSION_GRANTED) 
    Log.d(LOGTAG, "Granted"); 
else 
    Log.d(LOGTAG, "Denied");

Enforcing Permissions

mContext.enforceCallingPermission("de.creatronix.my_service.MY_PERM", "echoString permission not granted"); 

More about permissions:
https://developer.android.com/reference/android/Manifest.permission.html