小编afo*_*eca的帖子

如何在一个应用程序中获取多个图标以启动不同的活动?

我有一个有两个活动的应用程序,我希望能够在启动器中显示两个图标,每个图标在应用程序中启动相应的活动.

具体来说,我想要一个图标来启动我的主应用程序,另一个图标来启动我的设置活动.这可能吗?

这是我到目前为止所尝试的:

    <activity android:label="MyApp" android:name=".MyApp">
        <intent-filter>
            <action android:name=".MyApp"/>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>


    <activity android:label="Settings" android:name=".Settings">
        <intent-filter>
            <action android:name=".Settings"/>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

这会创建两个启动器图标,但它们都运行我的主应用程序而不是运行我的设置应用程序的第二个图标.我尝试过只有启动器类别,但后来我没有得到一个图标,所以看起来我也需要主要动作.

这是正确的方法还是应该在清单中声明两个应用程序?

android android-manifest

46
推荐指数
2
解决办法
2万
查看次数

我如何使用PackageManager.addPreferredActivity()?

在SDK 1.5中,我使用PackageManager类使用PackageManager.addPackageToPreferred()将首选主屏幕设置为我的应用程序.在新的SDK(使用2.1)中,这已被弃用,因此我尝试使用addPreferredActivity()获得相同的结果,但它没有按预期工作.

一些必要的背景.我正在写一个锁屏替换应用程序,所以我希望主键启动我的应用程序(已经运行,因此具有禁用密钥的效果).当用户"解锁"屏幕时,我打算恢复映射,以便一切正常.

在我的AndroidManifest.xml中,我有:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    <category android:name="android.intent.category.HOME"/>
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<uses-permission android:name="android.permission.SET_PREFERRED_APPLICATIONS">
</uses-permission>
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我有以下代码段:

// Set as home activity
// This is done so we can appear to disable the Home key.
PackageManager pm = getPackageManager();
//pm.addPackageToPreferred(getPackageName());

IntentFilter filter = new IntentFilter("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");

ComponentName[] components = new ComponentName[] 
{
    new ComponentName("com.android.launcher", ".Launcher")
};

Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(),
MyApp.class.getName());

pm.clearPackagePreferredActivities("com.android.launcher");
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY,
components, component);
Run Code Online (Sandbox Code Playgroud)

由此产生的行为是,当我按下Home键时,应用程序选择器出现,这表示clearPackagePreferredActivities()调用有效但我的应用程序没有被添加为首选.此外,下面日志中的第一行说明了"删除Intent的首选活动":

04-06 02:34:42.379:INFO/PackageManager(1017):结果集已更改,丢弃Intent的首选活动{act = …

android android-sdk-2.1

19
推荐指数
1
解决办法
3万
查看次数

如何创建启动任何电子邮件应用程序的意图?

我已经在这里和其他地方找到了关于创建发送电子邮件的意图的各种主题,这看起来非常简单.我正在寻找启动用户可能拥有的任何电子邮件客户端的意图.

这是我看到的用于发送电子邮件的代码(仅供参考,这不符合我的需求,因为我不想发送新消息):

Intent i = new Intent(Intent.ACTION_SEND); 
i.setType("text/plain"); 
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"}); 
i.putExtra(Intent.EXTRA_SUBJECT, "Subject of the message"); 
i.putExtra(Intent.EXTRA_TEXT   , "Body of the message"); 
Run Code Online (Sandbox Code Playgroud)

以下是我根据包名称专门启动Gmail客户端的代码:

PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.google.android.gm");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

上述代码有效但不灵活,因为用户可能没有使用Gmail,而是使用其他内置电子邮件应用程序或第三方电子邮件应用程序.我正在寻找一个能够在这种情况下调出选择器的意图,以便用户可以决定启动哪个应用来阅读电子邮件.

有谁知道如何做到这一点?

android android-package-managers

3
推荐指数
1
解决办法
2359
查看次数