android - 如何将应用程序的 packageName 的 ArrayList 发送到另一个 Activity ?

标签 android

我可以使用此代码发送包名称。但是如何使用其包名称启动新应用程序?

lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            //CheckedTextView item = (CheckedTextView) view;

            //if(item.isChecked()){
            packageName = list.get(position).activityInfo.packageName;
            Log.i("PACKAGE", packageName);
            //TODO: Pass this packageName to a method
            finalList.add(packageName);
            //}else if(!item.isChecked()){
            //}
        }
    });

最佳答案

我将向您展示如何send and receive implicit intents .这对于向一个或多个 Activity 、服务或广播接收器以及应用程序之间发送数据很有用。缺点是它是“公共(public)” Intent (如果您不使用 LocalBroadcastManager),因此不要发送任何敏感信息。

  1. 创建发件人使用的发送方法。
  2. 修改接收实体的 AndroidManifest.xml(在您的情况下是 Activity)
  3. 修改接收实体以处理传入的 Intent。

第一步 - 创建发送方使用的发送方法。:

public final static String ACTION_SEND_NAME = "com.example.intent.action.SEND_NAME";
public final static String ACTION_SEND_NAME_EXTRA = "name";

public static void sendPackageName(Context context, String name) {
    if (null == context || null == name) {
        throw new IllegalArgumentException("Argument(s) may not be null");
    }

    Intent intent = new Intent(ACTION_SEND_NAME);
    intent.putExtra(ACTION_SEND_NAME_EXTRA, name);

    //You might add extra these flags
    intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);

    //LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    //context.sendBroadcast(intent);
    //context.startService(intent);
    context.startActivity(intent);
}

第二步 - 修改 AndroidManifest.xml:

    <activity
        android:name="com.example.MyActivity"
        android:label="@string/app_name">
        <intent-filter>
            <!-- Other Intent filters -->
            <action android:name="com.example.intent.action.SEND_NAME" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

第三步 - 修改接收实体:

void onCreate (Bundle savedInstanceState) {

//...

// Get intent, action and MIME type
Intent intent = getIntent();
String appName;

if (ACTION_SEND_NAME.equals(intent.getAction())) {
    appName = intent.getStringExtra(Intent.ACTION_SEND_NAME_EXTRA); 
}

关于android - 如何将应用程序的 packageName 的 ArrayList 发送到另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17646020/

相关文章:

android - 为什么在小米设备中未激活“请勿打扰”?

android - Android (Cupcake) 是否已使用 Skyhook Wireless 进行本地化?

android - 汽车动画旋转的轮子

android - 未找到 Google Play 资源

c# - MVVMCross Release 构建不工作(LinkerPleaseInclude Listview)

android - Expandablelistview 子项上的 ImageButton

java - Material 设计与安卓工作室

android - Android 上的 XmlPullParser XML

android - 当我加载 ADT 时为什么会收到错误 "The Android SDK requires Android Developer Toolkit version XX.X.X or above?"

java - 点击标记时是否可以在 Google map 上显示覆盖项目?