Intents can used in many ways to invoke another Activity. The tutorial is not very explicit on the two main kinds of Intents so the I had to discover them myself in the documentation of the Intent class.
- Explicit intent targets a particular class that has been declared intent receiver in the AndroidManifest.xml
- Implicit intent targets an intent receiver with particular characteristics (like a particular action)
I tried out three setups in the simple test program (that actually consists of two Android applications).
- Explicit intent addressing with the invoked activity internal to the application (exp/int).
- Explicit intent addressing with the invoked activity external to the application (IntentSender application invokes IntentReceiver application) (exp/ext).
- Implicit intent addressing with external activity invocation (imp/ext). The invoked activity is again in the IntentReceiver application.
The implementation of this simple application did have its adventures. :-) The tutorial uses a simple form of explicit intent creation.
Intent i = new Intent(this, NoteEdit.class);
This was not usable for me because in the exp/ext case the target activity class was not located within the application. The solution seemed to be simple and relied on the Intent class' setClassName( pkgName, className ) method. Who could have thought that the right form of parametrization requires full path in className too (after having received the package part in the pkgName parameter)? This took me something like an hour wasted and was resolved by finding out, how the tutorial version of explicit invocation works.
The critical piece of code is the following:
Intent i = new Intent();
i.setClassName( "aexp.intentreceiver", "aexp.intentreceiver.IntentReceiver" );
i.putExtra( "i1", new Integer( 4 ) );
i.putExtra( "i2", new Integer( 5 ) );
startSubActivity(i, ACTIVITY_INVOKE);
The target activity needs to be declared in the AndroidManifest.xml. IntentReceiver class in IntentReceiver application happens to be the app's main intent receiver therefore no additional declaration is necessary. The internal IntentReceiver in IntentSender needs to be declared in IntentSender's AndroidManifest.xml.
[activity class=".IntentReceiver"]
(of course, this is XML with <> characters, I just can't get it through the #&@@! blog engine).
After getting through the explicit addressing's arcane package name/class name convention, I went after the implicit addressing which seemed similarly simple. In this case, there is an extra level of indirection between the invoker and invoked activity. The intent does not carry the target class, it carries a set of information that is used by the system to identify the target activity. In my example, I used only the intent action that I set to an action string I made up myself (aexp.intentsender.add).
Invoking code is simple:
Intent i = new Intent();
i.setAction( "aexp.intentsender.add" );
i.putExtra( "i1", new Integer( 5 ) );
i.putExtra( "i2", new Integer( 6 ) );
startSubActivity(i, ACTIVITY_INVOKE);
The intent receiver is identified according to the intent filter declaration in IntentReceiver's AndroidManifest.xml.
[intent-filter]
[action android:value="aexp.intentsender.add"/]
[category android:value="android.intent.category.DEFAULT"/]
[/intent-filter]
There is one point here to note that costed me again some half an hour wasted. :-) Although I don't use any categories, Intent's constructor creates one category by default, the Intent.DEFAULT_CATEGORY whose string format is android.intent.category.DEFAULT. I did not include the category originally into the AndroidManifest.xml and that's why the intent resolution was not succesful. This problem was rectified using the fantastic adb logcat command.
Guess, what goes into the log if there is an action match whose category does not match?
W/IntentResolver( 461): resolveIntent failed: found match, but none with Intent
.DEFAULT_CATEGORY
Thanks, emulator developers, this log message was really helpful!
0 comments:
Post a Comment