android - 如何注册一个 Intent 被其他应用程序发现?

标签 android

我正在开发一个需要被另一个应用程序调用的应用程序。这两个应用程序都将安装在我的 Android 设备中。我需要应用程序 A 来搜索应用程序 B 并调用它。

我的公司有以下要求:

an intent must also have its type attribute set to application/mycompanyname.com and it will be discovered using PackageManager.queryIntentActivities(Intent intent, int flags) with the intent described to see if any activity is available.

但是我在注册时遇到了问题。从现在开始我试过这个:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testpublishintent"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".ReceiverDemo">
        <intent-filter>
            <action android:name="marakana.intent.action.ReceiverDemo" />
          </intent-filter>
    </receiver>
</application>

</manifest>

我正在寻找所有已安装的应用程序,如下所示:

void GetInstalledAppList()
{
  final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  final List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
  for (Object object : pkgAppsList) 
  {
    ResolveInfo info = (ResolveInfo) object;
    Drawable icon    = getBaseContext().getPackageManager().getApplicationIcon(info.activityInfo.applicationInfo);
    String strAppName   = info.activityInfo.applicationInfo.publicSourceDir.toString();
    String strPackageName  = info.activityInfo.applicationInfo.packageName.toString();
    final String title  = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
   }
 }

但我不知道这是否正确,因为我可以看到已安装的应用程序但无法实例化它。有人可以纠正我吗?

最佳答案

我想你可以看看this page在文档中,它们基本上解释了您需要的所有内容。

它是这样工作的:您通过在其 Manifest 中定义一些 IntentFilter 来注册您想要启动的 Activity,然后您从原始 Activity 启动此 Intent

但是,如果您想要一个自定义 Intent ,我会考虑这样做 StackOverflow Thread (你没有说你想要哪种 Intent )

关于android - 如何注册一个 Intent 被其他应用程序发现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31335526/

相关文章:

android - AsyncTask 取消自身仍然调用 onPostExecute()

java - 调用 C# Web 服务时出现意外响应代码 500

android - 检测应用程序是否从华为应用市场安装

Android深度 sleep 和唤醒锁

android - getActivity().findViewById(R.layout.contacts_list_view) 返回 null

java - 由于内部错误 : PASSWORD_VERIFIER challenge encountered not at the start of authentication flow,身份验证失败

带有视频和文字的 Android ACTION_SEND_MULTIPLE

Android 谷歌地图应用程序 - Mainactivity.java 错误

android - 在 eclipse-Android 4.2-Unable to build : the file dx. jar was not loaded from the SDK folder

android - 防止 RecyclerView 中的直接邻居被分离和回收