Java安卓学习总结(二十八)

第二十四章 深入学习intent和任务

有关隐式intent寻找可启动应用

可启动应用是点击屏幕或启动器界面上的图标就能打开的应用,可以通过包含MAIN和LAUNCHER类别的intent过滤器从PackManager中获取所有可启动主activity。

Intent startupIntent= new Intent(Intent.ACTION_MAIN);
startupIntent.addCategory(Intent.CATEGORY_LAUNCHER);

PackageManager packageManager=getActivity().getPackageManager();
List<ResolveInfo>activities=packageManager.queryIntentActivities(startupIntent,0);

List<ResolveInfo>中存放了所有可启动应用的主activity,ResolveInfo.loadLabel可以获取activity的标签(一般是应用名),ResolveInfo.loadIcon可以获取activity的drawable图标,这在挑战练习中会用到。

有关使用ActivityInfo启动应用

使用

public Intent setClassName(String packageName, String className)

方法来设置需要启动的应用时需要包名和类名,可以直接从ResolveInfo中获得这些信息:

ActivityInfo activityInfo=mResolveInfo.activityInfo;

Intent i=new Intent(Intent.ACTION_MAIN).setClassName(activityInfo.applicationInfo.packageName,activityInfo.name).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(i);

这里还增加了Intent.ACTION_MAIN的信息,是为了指明应用启动行为,对部分应用有效。

之前创建显式intent时使用接受context和class的构造方法会自动创建组件名:

public Intent setClassName(Context context, Class<?> className)

有关将应用作为设备主屏幕

在Manifest.xml中的intent主过滤器中添加:

<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>

有关挑战练习 应用图标

原本viewHolder使用的时android资源中的原本就有的一个视图资源android.R.layout.simple_list_item_1,其下就只有一个textview:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

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.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />

如果想要显示一个图标加上一个文本,这个视图是不够用的,而android.R.layout.simple_list_item_2只是简单地变成了两个textVIew,所以需要手动写一个带有ImageView的视图文件。由于ResolveInfo.loadIcon可以获取activity的drawable图标,而ImageView恰好有setImageDrawable方法,所以不需要进行额外的操作。

bind()方法如下:

public void bindActivity(ResolveInfo resolveInfo)
{
mResolveInfo=resolveInfo;
PackageManager packageManager=getActivity().getPackageManager();
String appName=mResolveInfo.loadLabel(packageManager).toString();

mNameTextView.setText(appName);
mIconView.setImageDrawable(mResolveInfo.loadIcon(packageManager));
}

视图资源如下:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
<ImageView
android:id="@+id/activity_icon_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="8dp"/>

<TextView
android:id="@+id/activity_name_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="viewStart"
android:textSize="24dp"
android:layout_margin="8dp"/>

</LinearLayout>

效果图:

发表评论