android - 自定义共享 Intent 的 UI

标签 android

enter image description here

我在我的 Android 应用程序中有一个共享 Intent ,我想知道如何自定义它的 UI。 我在 Stackoverflow 上浏览了几个相关问题,其中大多数都在谈论显示要显示的特定应用程序集。我不想要那个。我希望操作系统能够判断它在共享 Intent View 中显示的应用程序,但我确实希望看起来像某种方式。就像我附在这个问题上的图片一样。 此外,有没有办法让它成为自定义 UI 而不必自己处理点击事件? 提前谢谢你。

最佳答案

欢迎您创建自己的“选择器”,使用 PackageManagerqueryIntentActivities() 来确定要显示的候选 Activity 。当用户选择一个时,您可以创建一个显式的 Intent,基于您原始的隐式 Intent(例如,ACTION_SEND)和 ComponentName 用户选择的 Activity 。然后您可以完全控制选择器的外观和处理任何事件。

例如,此 Activity (来自 this sample project)使用此技术实现了主屏幕样式的启动器:

/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
*/

package com.commonsware.android.launchalot;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;

public class Launchalot extends ListActivity {
  AppAdapter adapter=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    PackageManager pm=getPackageManager();
    Intent main=new Intent(Intent.ACTION_MAIN, null);

    main.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);

    Collections.sort(launchables,
                     new ResolveInfo.DisplayNameComparator(pm)); 

    adapter=new AppAdapter(pm, launchables);
    setListAdapter(adapter);
  }

  @Override
  protected void onListItemClick(ListView l, View v,
                                 int position, long id) {
    ResolveInfo launchable=adapter.getItem(position);
    ActivityInfo activity=launchable.activityInfo;
    ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                         activity.name);
    Intent i=new Intent(Intent.ACTION_MAIN);

    i.addCategory(Intent.CATEGORY_LAUNCHER);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    i.setComponent(name);

    startActivity(i);    
  }

  class AppAdapter extends ArrayAdapter<ResolveInfo> {
    private PackageManager pm=null;

    AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
      super(Launchalot.this, R.layout.row, apps);
      this.pm=pm;
    }

    @Override
    public View getView(int position, View convertView,
                          ViewGroup parent) {
      if (convertView==null) {
        convertView=newView(parent);
      }

      bindView(position, convertView);

      return(convertView);
    }

    private View newView(ViewGroup parent) {
      return(getLayoutInflater().inflate(R.layout.row, parent, false));
    }

    private void bindView(int position, View row) {
      TextView label=(TextView)row.findViewById(R.id.label);

      label.setText(getItem(position).loadLabel(pm));

      ImageView icon=(ImageView)row.findViewById(R.id.icon);

      icon.setImageDrawable(getItem(position).loadIcon(pm));
    }
  }
}

在您的情况下,您将使用您的ACTION_SEND Intent,而不是ACTION_MAIN/CATEGORY_HOME,但是同样的原则也适用。

关于android - 自定义共享 Intent 的 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475454/

相关文章:

android:来自市场 Activity 的响应

android - 在 recyclerview 中的 notifyitemchanged 期间保留链式 react

java - Android Studio Activity - 已关闭

java - 如果在 android 中的 SQLite 事务期间,第二个线程尝试在同一个表上进行事务,会发生什么情况?

c# - Xamarin.forms 混合 ContentPage 和 Android Activity

android - 在所有 Activity 中使用相同的微调器

Android - Binder

android - 嵌套的 RecyclerView onClickListener 不工作

android - Firebase 查询 : equalTo on a list

android - 在 web View 中加载本地 HTML 文件时出现访问被拒绝错误