问题
i have and app in which the user can scroll through a list of all installed apps and he can pick any app and starts it.
For most Apps this works fine with:
startActivity(getPackageManager().getLaunchIntentForPackage(packageName_selectedapp));
where packageName_selectedapp is the string of the selected app from the list.
BUT for some apps the function getLaunchIntentForPackage(packageName_selectedapp) returns "null", for instance if it is the dialer or contacts activity.
How can i get the launchIntent for dialer and contacts?
thx in advance!
回答1:
How can i get the launchIntent for dialer and contacts?
Those are not apps. Those are other launchable activities of another app.
Hence, you need to decide what it is that you are writing.
You said that you have "a list of all installed apps and he can pick any app and starts it". In that case, you specifically do not want "dialer and contacts", as those are not apps.
If, instead, you want to show a list of all launchable activities, from which the user can pick, you would not be using getLaunchIntentForPackage()
. Instead, you would use queryIntentActivities()
to find those launchable activities. I have a sample app that demonstrates this, in the form of a launcher.
回答2:
some applications such as wallpapers, dont have a default activity, and cannot be launched via launch intent, but you should be able to use a regular intent if your context is correct. im not sure however if this applies to the particular apps in question....
i would assume your making another home screen or a rom and thats why you want this?
this should help
Contacts/People App Select a contact
To have the user select a contact and provide your app access to all the contact information, use the ACTION_PICK action and specify the MIME type to Contacts.CONTENT_TYPE.
The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact. The response grants your app temporary permissions to read that contact using the Contacts Provider API even if your app does not include the READ_CONTACTS permission.
Tip: If you need access to only a specific piece of contact information, such as a phone number or email address, instead see the next section about how to select specific contact data.
Action ACTION_PICK Data URI Scheme None MIME Type Contacts.CONTENT_TYPE Example intent:
static final int REQUEST_SELECT_CONTACT = 1;
public void selectContact() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_SELECT_CONTACT);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
// Do something with the selected contact at contactUri
...
}
}
For information about how to retrieve contact details once you have the contact URI, read Retrieving Details for a Contact. Remember, when you retrieve the contact URI with the above intent, you do not need the READ_CONTACTS permission to read details for that contact.
Select specific contact data
To have the user select a specific piece of information from a contact, such as a phone number, email address, or other data type, use the ACTION_PICK action and specify the MIME type to one of the content types listed below, such as CommonDataKinds.Phone.CONTENT_TYPE to get the contact's phone number.
If you need to retrieve only one type of data from a contact, this technique with a CONTENT_TYPE from the ContactsContract.CommonDataKinds classes is more efficient than using the Contacts.CONTENT_TYPE (as shown in the previous section) because the result provides you direct access to the desired data without requiring you to perform a more complex query to Contacts Provider.
The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact data. The response grants your app temporary permissions to read that contact data even if your app does not include the READ_CONTACTS permission.
Action ACTION_PICK Data URI Scheme None MIME Type CommonDataKinds.Phone.CONTENT_TYPE Pick from contacts with a phone number. CommonDataKinds.Email.CONTENT_TYPE Pick from contacts with an email address. CommonDataKinds.StructuredPostal.CONTENT_TYPE Pick from contacts with a postal address. Or one of many other CONTENT_TYPE values under ContactsContract.
Example intent:
static final int REQUEST_SELECT_PHONE_NUMBER = 1;
public void selectContact() {
// Start an activity for the user to pick a phone number from contacts
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
// Get the URI and query the content provider for the phone number
Uri contactUri = data.getData();
String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, projection,
null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst()) {
int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
...
}
}
}
i will refer you here
http://developer.android.com/guide/components/intents-common.html
来源:https://stackoverflow.com/questions/22693185/starting-any-of-the-installed-apps