Why two parameters to ComponentName constructor?
From the Pro Android 2 book: "ComponentName wraps a package name and a class name together. For example..."
Intent intent = new Intent();
Intent.setCompn开发者_开发技巧onet(new ComponentName(
"com.android.contacts"
,"com.android.contacts.DialContactsEntryActivity");
startActivity(intent)
If you look at the example, you'll notice that the package name can be easily derived from the class name. So the obvious question is: why two parameters? Why not provide only the class name?
Is there a scenario where the class passed to the ComponentName constructor doesn't belong to the package passed to the same constructor?
The application component may exist within an application whose package name (declared in its Android manifest) is completely different from the Java package for the particular class that defines the component. An example is the MapsActivity in the Google Maps application:
intent.setComponent(new ComponentName("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity"));
or:
intent.setComponent(new ComponentName("com.google.android.apps.maps",
MapsActivity.class.getName()));
精彩评论