简体   繁体   English

启动任何已安装的应用程序

[英]Starting ANY of the installed apps

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. 其中packageName_selectedapp是从列表中选择的应用程序的字符串。

BUT for some apps the function getLaunchIntentForPackage(packageName_selectedapp) returns "null", for instance if it is the dialer or contacts activity. 但是对于某些应用程序,功能getLaunchIntentForPackage(packageName_selectedapp)返回“空”,例如,如果它是拨号程序或联系人活动。

How can i get the launchIntent for dialer and contacts? 如何获得拨号器和联系人的launchIntent?

thx in advance! 提前谢谢!

How can i get the launchIntent for dialer and contacts? 如何获得拨号器和联系人的launchIntent?

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() . 相反,如果要显示用户可以从中选择的所有可启动活动的列表,则不会使用getLaunchIntentForPackage() Instead, you would use queryIntentActivities() to find those launchable activities. 相反,您将使用queryIntentActivities()查找那些可启动的活动。 I have a sample app that demonstrates this , in the form of a launcher. 我有一个示例应用程序,以启动器的形式演示了这一点

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? 我会假设您正在制作另一个主屏幕或rom,这就是为什么您想要这个?

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. 要让用户选择一个联系人并为您的应用提供对所有联系人信息的访问权限,请使用ACTION_PICK操作并将MIME类型指定为Contacts.CONTENT_TYPE。

The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact. 传递到onActivityResult()回调的结果Intent包含以下内容:指向所选联系人的URI。 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. 即使您的应用程序不包含READ_CONTACTS权限,响应也会授予您的应用程序临时权限,以使用Contacts Provider API读取该联系人。

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: 操作ACTION_PICK数据URI方案无MIME类型Contacts.CONTENT_TYPE示例意图:

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. 有关在获取联系人URI后如何检索联系人详细信息的信息,请阅读检索联系人的详细信息。 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. 请记住,当您出于上述目的检索联系人URI时,不需要READ_CONTACTS权限即可读取该联系人的详细信息。

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. 若要让用户从联系人中选择一条特定的信息,例如电话号码,电子邮件地址或其他数据类型,请使用ACTION_PICK操作并将MIME类型指定为下面列出的一种内容类型,例如CommonDataKinds。 Phone.CONTENT_TYPE获取联系人的电话号码。

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. 如果您只需要从联系人中检索一种类型的数据,则使用ContactsContract.CommonDataKinds类中的CONTENT_TYPE的这种方法比使用Contacts.CONTENT_TYPE的方法(如上一节中所示)更有效,因为结果使您可以直接访问所需的数据,而无需您对Contacts Provider执行更复杂的查询。

The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact data. 传递到onActivityResult()回调的结果Intent包含以下内容:指向所选联系人数据的URI。 The response grants your app temporary permissions to read that contact data even if your app does not include the READ_CONTACTS permission. 即使您的应用程序不包含READ_CONTACTS权限,该响应也会授予您的应用程序临时权限来读取该联系人数据。

Action ACTION_PICK Data URI Scheme None MIME Type CommonDataKinds.Phone.CONTENT_TYPE Pick from contacts with a phone number. 操作ACTION_PICK数据URI方案无MIME类型CommonDataKinds.Phone.CONTENT_TYPE从具有电话号码的联系人中选取。 CommonDataKinds.Email.CONTENT_TYPE Pick from contacts with an email address. CommonDataKinds.Email.CONTENT_TYPE从具有电子邮件地址的联系人中选择。 CommonDataKinds.StructuredPostal.CONTENT_TYPE Pick from contacts with a postal address. CommonDataKinds.StructuredPostal.CONTENT_TYPE从具有邮政地址的联系人中选择。 Or one of many other CONTENT_TYPE values under ContactsContract. 或ContactsContract下的许多其他CONTENT_TYPE值之一。

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 http://developer.android.com/guide/components/intents-common.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM