2

感谢本教程: http: //mobile.tutsplus.com/tutorials/android/android-sdk-implement-a-share-intent/我知道如何简单地分享我的信息:我只是创建意图并开始活动。但是,在 startActivity 调用和共享窗口的有效显示之间有一点延迟,所以我想使用例如适合该类型共享的应用程序的列表视图进行自定义活动。

因此我需要:

编辑:1)一种获取可以处理意图的应用程序列表的方法,在我的例子中是带有文本/纯 mime 类型的 ACTION.SEND 意图。

2)一种以我的邮件正文和主题开头的方法。

4

1 回答 1

4

获取已安装应用程序列表:

final PackageManager pm = getPackageManager();

List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {

   Log.d(TAG, "Installed package :" + packageInfo.packageName);
   Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); 

   }// the getLaunchIntentForPackage returns an intent that you can use with startActivity() 
}

并启动该软件包:

protected void launchApp(String packageName) {
    Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
    if (mIntent != null) {
        try {
            startActivity(mIntent);
        } catch (ActivityNotFoundException err) {
            Toast t = Toast.makeText(getApplicationContext(),
            "Application not found", Toast.LENGTH_SHORT);
            t.show();
        }
    }
}

编辑

此方法查询包管理器以查找可以使用指定操作响应意图的已安装包:

public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
    packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

并使用特定应用程序(可以处理 ACTION_SEND)共享您的文本:

ResolveInfo info = list.get(index);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
((Activity)context).startActivity(intent);
于 2012-10-01T08:23:28.453 回答