Part 2
Intent (specific/intentional message between android apps) is not sent directly from your app to another app, It sent to android os which handle it & make decision for delivering it to another app.
So, Android os can modify your message or adding extra parameters/actions to your message to be deliverable.
In previous part we simply exploring top most intent methods/properties , In fact there are more properties & methods intent has.
Let's exploring more of them now because we'll want them a lot in next tutorial parts.
1- When you miss important part of message (intent), Android OS evaluates the most suitable property/method for it, simply Android OS try to fill the blank you missed; It will fill it automatically & send your message, or pick a list of choices for user to select/fill manually , or if Android OS can't evaluate missed info: simply your app will CRASH!
Example 1: intent says: Hey Android: show me the contact people number 100 in the list, note we don't specify the target app by setPackage, we let this on android to decide which target app or android requests the user to select the appropirate app.
B4X:
Dim intent1 As Intent
intent1.Initialize("android.intent.action.VIEW","content://contacts/people/100")
StartActivity(intent1)
Example 2: We going more specific, so result more percise. intent says: Hey Android send "Hello Hamied!" text message to phone number "967772332333" using whatsapp app.
B4X:
Dim sendIntent As Intent
sendIntent.Initialize(sendIntent.ACTION_MAIN,"")
sendIntent.PutExtra("jid","[email protected]")
sendIntent.Action=sendIntent.ACTION_SEND
sendIntent.SetPackage("com.whatsapp")
sendIntent.SetType("text/plain")
sendIntent.PutExtra("android.intent.extra.TEXT","Hello Hamied!")
StartActivity(sendIntent)
Example 3: This code will crash! because Android can't evaluate it, you will get this error: android.content.ActivityNotFoundException: No Activity found to handle Intent.
B4X:
Dim intent1 As Intent
intent1.Initialize("android.intent.action.MAIN","content://contacts/people/100")
StartActivity(intent1)
2- Another important things: some message requires user permission, so if you don't add the appropriate manifest declaration of permission & runtime request permisssion from user, your app will be CRASH.
Example: This code will crash because we don't set the appropirate user permission, error: java.lang.SecurityException: Permission Denial: starting Intent "requires android.permission.CALL_PHONE"
B4X:
Dim intent1 As Intent
intent1.Initialize("android.intent.action.CALL","tel:967772332333")
StartActivity(intent1)
If you set the correct permission, code will run.
3- Any Android device contains a lot of apps, so Android categorized/grouped them according thier functionality/purpose, this will help our intent message coding, for example if you want android to browse a website, You are targeting the browsers apps. This help & ease the situation for android os.
intent has addCategory method to tell Android about targeted apps group.
You can explore all the intent categories in Android SDK directory, under /platforms/android-x(x being the API level)/data/categories.txt
Example: This will work "apps category is browsers":
B4X:
Dim intent1 As Intent
intent1.Initialize("android.intent.action.VIEW","https://b4x.com")
intent1.AddCategory("android.intent.category.BROWSABLE")
StartActivity(intent1)
Example: This will not work "apps category is calculator":
B4X:
Dim intent1 As Intent
intent1.Initialize("android.intent.action.VIEW","https://b4x.com")
intent1.AddCategory("android.intent.category.CALCULATOR")
StartActivity(intent1)
4- The Android OS evalutes your missing info in selecting most appropirate app, but many times you want Android to force user to select the app, in this case we should add intent1.WrapAsIntentChooser method:
B4X:
Dim intent1 As Intent
intent1.Initialize("android.intent.action.SEND", "")
intent1.SetType("text/*")
intent1.PutExtra("android.intent.extra.TEXT", "this is the message")
intent1.WrapAsIntentChooser("Choose")
StartActivity(intent1)
See you in Part 3 ...