-
Notifications
You must be signed in to change notification settings - Fork 775
Formulating intents
It is possible to build an Intent directly with reflection:
intent = self.new("android.content.Intent")
intent.setAction("android.intent.action.ACTION_VIEW")
self.getContext().startActivity(intent)
However, Mercury provides a simple API to reduce the amount of code required:
from mwr.droidhg import android
intent = android.intent(action="android.intent.action.ACTION_VIEW")
self.getContext().startActivity(intent.buildIn(self))
The benefit of using Mercury's API is that it provides helpful short-cuts for adding flags and extras to the intent.
When specifying the component, a tuple containing the package and component names should be passed:
intent = android.Intent(component=("com.android.browser", "com.android.browser.BrowserActivity"))
Mercury accepts a list of flags, either as hexadecimal strings, or as constant names as defined in the Android SDK:
intent.flags.append("0x10000000")
intent.flags.append("ACTIVITY_NEW_TASK")
Mercury will combine all specified flags, and pass a single hexadecimal string.
Intents can carry messages or commands inside of them in the form of extras. Applications may want to pass additional information inside of the intents they send to one another, possibly containing the data to perform a task on, or any other user-defined task to initiate from the received data.
As with flags, Mercury can pass many extras in an Intent:
intent.extras.append(("string", "mykey", "myvalue"))
Extras of different types can be passed by setting the first argument (type). Mercury supports: boolean, byte, char, double, float, integer, short, string.
In all instances, the value should be specified as a string, containing a suitable value that will be cast to the correct type:
intent.extras.append(("boolean", "mybool", "true"))
intent.extras.append(("integer", "myint", "42"))
intent.extras.append(("double", "mydouble", "3.14"))