android开发获取手机里的所有应用,点击并响应

释放双眼,带上耳机,听听看~!

在此我写一个找到android手机里的所有已装应用程序(android手机自带的也行),点击某个应用时,将做出响应 。

先看一下效果图:

当点击《愤怒的小鸟》时,将打开《愤怒的小鸟》这个游戏

main.xml的源代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="15dp"
android:horizontalSpacing="12dp"
android:columnWidth="95dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>

接着是apps.xml,源代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<ImageView
android:id="@+id/apps_image"
android:layout_width="48dip"
android:layout_height="48dip"
android:icon="@drawable/icon" />
<TextView
android:id="@+id/apps_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxEms="5"
android:text="good" />
</LinearLayout>

接下来就是java源代码了,比较长,希望耐心看下去

package com.android.getApps;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class Apps extends Activity {
// 用来记录应用程序的信息
List<AppsItemInfo> list;
private GridView gridview;
private PackageManager pManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
// 取得gridview
gridview = (GridView) findViewById(R.id.gridview);
// 获取图片、应用名、包名
pManager = Apps.this.getPackageManager();
List<PackageInfo> appList = getAllApps(Apps.this);
list = new ArrayList<AppsItemInfo>();
for (int i = 0; i < appList.size(); i++) {
PackageInfo pinfo = appList.get(i);
AppsItemInfo shareItem = new AppsItemInfo();
// 设置图片
shareItem.setIcon(pManager
.getApplicationIcon(pinfo.applicationInfo));
// 设置应用程序名字
shareItem.setLabel(pManager.getApplicationLabel(
pinfo.applicationInfo).toString());
// 设置应用程序的包名
shareItem.setPackageName(pinfo.applicationInfo.packageName);
list.add(shareItem);
}
// 设置gridview的Adapter
gridview.setAdapter(new baseAdapter());
// 点击应用图标时,做出响应
gridview.setOnItemClickListener(new ClickListener());
}
public static List<PackageInfo> getAllApps(Context context) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
// 获取手机内所有应用
List<PackageInfo> packlist = pManager.getInstalledPackages(0);
for (int i = 0; i < packlist.size(); i++) {
PackageInfo pak = (PackageInfo) packlist.get(i);
// 判断是否为非系统预装的应用程序
// 这里还可以添加系统自带的,这里就先不添加了,如果有需要可以自己添加
// if()里的值如果<=0则为自己装的程序,否则为系统工程自带
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
// 添加自己已经安装的应用程序
apps.add(pak);
}
}
return apps;
}
private class baseAdapter extends BaseAdapter {
LayoutInflater inflater = LayoutInflater.from(Apps.this);
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
// 使用View的对象itemView与R.layout.item关联
convertView = inflater.inflate(R.layout.apps, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView
.findViewById(R.id.apps_image);
holder.label = (TextView) convertView
.findViewById(R.id.apps_textview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setImageDrawable(list.get(position).getIcon());
holder.label.setText(list.get(position).getLabel().toString());
return convertView;
}
}
private class ViewHolder{
private ImageView icon;
private TextView label;
}
// 当用户点击应用程序图标时,将对这个类做出响应
private class ClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//			// 将应用所选的应用程序信息共享到Application中
//			MyApp appState = ((MyApp) getApplicationContext());
//			// 获取当前所在选项卡
//			String tab_id = appState.getTab_id();
//			// 设置所选应用程序信息
//			appState.set_AppInfo(tab_id, list.get(arg2).getLabel(), list.get(
//					arg2).getIcon(), list.get(arg2).getPackageName());
Intent intent = new Intent();
intent = Apps.this.getPackageManager().getLaunchIntentForPackage(list.get(arg2).getPackageName());
startActivity(intent);
// 销毁当前Activity
//			finish();
}
}
// 自定义一个 AppsItemInfo 类,用来存储应用程序的相关信息
private class AppsItemInfo {
private Drawable icon; // 存放图片
private String label; // 存放应用程序名
private String packageName; // 存放应用程序包名
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
}
}

人已赞赏
Android文章

解决Android Studio的卡顿问题

2020-3-4 13:44:08

Android文章

Android自定义曲线图

2020-3-4 16:55:30

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索