一起学android之怎样获取手机程序列表以及程序相关信息并启动指定程序 (26)
效果图:
程序列表:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
启动程序,获取程序信息:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
代码例如以下:
创建一个AppInfo类来表示应用程序
<pre name="code" class="java">public class AppInfo {
public CharSequence title;// 程序名
public CharSequence packageName; // 程序包名
Intent intent;// 启动Intent
public Drawable icon;// 程序图标
/*
* 设置启动该程序的Intent
*/
final void setActivity(ComponentName className, int launchFlags) {
intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(className);
intent.setFlags(launchFlags);
}
}
创建程序列表的适配器:
/**
* 程序列表适配器
* @author bill
*
*/
public class ShowAppListAdapter extends BaseAdapter {
private ArrayList<AppInfo> appList;
private LayoutInflater inflater; public ShowAppListAdapter(Context context,ArrayList<AppInfo> appList,
PackageManager pm) {
this.appList = appList;
inflater = LayoutInflater.from(context);
} public int getCount() {
return appList.size();
} public Object getItem(int position) {
return appList.get(position);
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
final AppInfo info = appList.get(position);
ViewHolder holder = null;
if(null == convertView){
convertView = inflater.inflate(R.layout.app_list_item, null);
holder = new ViewHolder();
holder.lv_image = (ImageView) convertView.findViewById(R.id.lv_icon);
holder.lv_name = (TextView) convertView.findViewById(R.id.lv_item_appname);
holder.lv_packname = (TextView) convertView.findViewById(R.id.lv_item_packageame);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.lv_image.setImageDrawable(info.icon);
final CharSequence name = info.title;
final CharSequence packName = info.packageName;
holder.lv_name.setText(name);
holder.lv_packname.setText(packName);
return convertView;
}
private final static class ViewHolder{
ImageView lv_image;
TextView lv_name;
TextView lv_packname;
} }
public class MainActivity extends Activity {
/*
* 应用程序集合
*/
private ArrayList<AppInfo> appInfos;
private ListView lv_app;
/*
* 管理应用程序包,并通过它获取程序信息
*/
private PackageManager pm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_list);
pm = getPackageManager();
initView();
new Thread(runable).start();
}
private void initView(){
lv_app = (ListView) findViewById(R.id.app_list_view);
lv_app.setOnItemClickListener(new AppDetailLinster());
}
private final Runnable runable = new Runnable() {
public void run() {
loadApplications();
myHandler.obtainMessage().sendToTarget();
}
};
private Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
lv_app.setAdapter(new ShowAppListAdapter(MainActivity.this,
appInfos, pm));
}
};
/**
* 载入应用列表
*/
private void loadApplications() {
PackageManager manager = this.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = manager.queryIntentActivities(
mainIntent, 0);
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
if (apps != null) {
final int count = apps.size();
if (appInfos == null) {
appInfos = new ArrayList<AppInfo>(count);
}
appInfos.clear();
for (int i = 0; i < count; i++) {
AppInfo application = new AppInfo();
ResolveInfo info = apps.get(i);
application.title = info.loadLabel(manager);
application.setActivity(new ComponentName(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name), Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
application.icon = info.activityInfo.loadIcon(manager);
application.packageName = info.activityInfo.applicationInfo.packageName;
appInfos.add(application);
}
}
}
/**
* 列表监听类
* @author bill
*
*/
public final class AppDetailLinster implements OnItemClickListener {
AlertDialog dialog;
public void onItemClick(AdapterView<?> view, View arg1,
final int position, long arg3) {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("选项");
builder.setItems(R.array.choice, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
final AppInfo appInfo = appInfos.get(position);
switch (which) {
case 0: // 启动程序
try {
startApp(appInfo);
} catch (Exception e) {
}
break;
case 1: // 具体信息
try {
showAppDetail(appInfo);
} catch (Exception e) {
}
break;
}
dialog.dismiss();
}
private void showAppDetail(AppInfo appInfo)
throws Exception {
final String packName = appInfo.packageName.toString();
final PackageInfo packInfo = getAppPackinfo(packName);
final String versionName = packInfo.versionName;
final String[] apppremissions = packInfo.requestedPermissions;
final String appName = appInfo.title.toString();
Intent showDetailIntent = new Intent(MainActivity.this,
ShowAppDetailActivity.class);
Bundle bundle = new Bundle();
bundle.putString("packagename", packName);
bundle.putString("appversion", versionName);
bundle.putStringArray("apppremissions", apppremissions);
bundle.putString("appname", appName);
showDetailIntent.putExtras(bundle);
startActivity(showDetailIntent);
}
private void startApp(AppInfo appInfo)
throws Exception {
final String packName = appInfo.packageName.toString();
final String activityName = getActivityName(packName);
if (null == activityName) {
Toast.makeText(MainActivity.this, "程序无法启动",
Toast.LENGTH_SHORT);
return;
}
Intent intent = new Intent();
intent.setComponent(new ComponentName(packName,
activityName));
startActivity(intent);
}
});
dialog = builder.create();
dialog.show();
}
}
/**
* 获取程序信息
* @param packName
* @return
* @throws Exception
*/
public PackageInfo getAppPackinfo(String packName) throws Exception {
return pm.getPackageInfo(packName, PackageManager.GET_ACTIVITIES
| PackageManager.GET_PERMISSIONS);
}
/**
* 获取启动相关程序的Activity
* @param packName
* @return
* @throws Exception
*/
public String getActivityName(String packName) throws Exception {
final PackageInfo packInfo = pm.getPackageInfo(packName,
PackageManager.GET_ACTIVITIES);
final ActivityInfo[] activitys = packInfo.activities;
if (null == activitys || activitys.length <= 0) {
return null;
}
return activitys[0].name;
}
}
app_list.xml:
<?xml version="1.0" encoding="utf-8"? >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/black" > <ListView
android:id="@+id/app_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView> </RelativeLayout>
app_list_item.xml:
<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center_vertical"> <ImageView
android:id="@+id/lv_icon"
android:layout_width="48px"
android:layout_height="48px"
android:layout_marginTop="5px"
android:layout_marginBottom="5px"
></ImageView>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="48px"
android:paddingLeft="5px"
>
<TextView
android:id="@+id/lv_item_appname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="16px"
android:textStyle="bold"
android:textColor="#fff"
></TextView> <TextView
android:id="@+id/lv_item_packageame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#fff"
></TextView> </LinearLayout>
</LinearLayout>
/**
* 查看应用信息
* @author bill
*
*/
public class ShowAppDetailActivity extends Activity { private TextView tv_appname;
private TextView tv_appversion;
private TextView tv_packagename;
private TextView tv_permission;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.app_detial);
tv_appname = (TextView) findViewById(R.id.detail_app_name);
tv_appversion = (TextView) findViewById(R.id.detail_app_version);
tv_packagename = (TextView) findViewById(R.id.detail_app_packname);
tv_permission = (TextView) findViewById(R.id.detail_app_permissions);
Bundle bundle = this.getIntent().getExtras();
String packagename= bundle.getString("packagename");
String appversion = bundle.getString("appversion");
String appname = bundle.getString("appname");
String[] appPremissions = bundle.getStringArray("apppremissions");
StringBuilder sb = new StringBuilder();
for(String s : appPremissions){
sb.append(s);
sb.append("\n");
}
tv_appname.setText(appname);
tv_appversion.setText(appversion);
tv_packagename.setText(packagename);
tv_permission.setText(sb.toString());
}
}
app_detial.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"
>
<TableLayout
android:id="@+id/app_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="程序名字"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/detail_app_name"
/>
</TableRow> <TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="程序版本号"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/detail_app_version"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="程序包名"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/detail_app_packname"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="程序权限"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/detail_app_permissions"
/>
</TableRow>
</TableLayout> </LinearLayout>
最后别忘了配置AndroidManifest。
转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/44274387情绪控_
一起学android之怎样获取手机程序列表以及程序相关信息并启动指定程序 (26)的更多相关文章
- 【风马一族_Android】Android 从命令行界面获取手机信息
Android 从命令行界面获取手机信息 1: cmd 打开命令行界面 2:adb devices 获取与电脑相连的设备,例如:模拟器.真机(手机) (右击“标记”,选择设备名称,点击“Ctrl+ ...
- Android开发之获取手机SIM卡信息
TelephonyManager是一个管理手机通话状态.电话网络信息的服务类.该类提供了大量的getXxx(),方法获取电话网络的相关信息. TelephonyManager类概述: 可用于訪问有关设 ...
- 【转】android 安卓APP获取手机设备信息和手机号码的代码示例
http://blog.csdn.net/changemyself/article/details/7421476 下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓 ...
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下 PackageManager packageManager = this.getPackageManag ...
- Android初级教程获取手机系统联系人信息
在手机内部,对联系人信息存在对应的数据库.我们创建的而联系人信息都存在这张表中.如下是对数据库的截图,我已经对表和应该注意的地方做了红笔标注: 好了,现在可以根据数据库里面的数据来写代码了. 代码如下 ...
- android 安卓APP获取手机设备信息和手机号码的代码示例
下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码 准备条件:一部安卓手机.手机SIM卡确保插入手机里.eclipse ADT和android-sdk开发环境 第一步:新建一个andro ...
- Android学习笔记-获取手机内存,SD卡存储空间。
前面介绍到如何保存数据到手机内存或者SD卡,但是问题是,在保存以前,我们还需要对他们的空间(可用空间),进行判断,才可以进行后续操作,所以,本节我们就介绍如何获取手机内存以及Sd卡的空间. //这时获 ...
- 获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下: PackageManager packageManager = this.getPackageMana ...
- Android初级教程获取手机位置信息GPS与动态获取最佳方式
简单介绍一下gps定位的操作. 主要是靠locationmanger这个api完成的一些操作:通过获取这个实例,然后调用它的requestLocationUpdates方法进行注册.传入的参数分别有以 ...
随机推荐
- 移动平台3G手机网站前端开发布局技巧汇总
移动平台3G手机网站前端开发布局技巧汇总 作者:前端开发-武方博 发布:2011-05-10 09:11 分类:移动开发 阅读:120,618 views 7条评论 您或许正在 ...
- [转] 使用CSS3 will-change提高页面滚动、动画等渲染性能 ---张鑫旭
一.先来看一个例子 下面这个例子来自某外文,我这里简单转述下. 视差滚动现在不是挺流行的嘛,然后Chris Ruppel当其使用background-attachment: fixed实现背景图片不随 ...
- WordPress防暴力破解:安全插件和用.htpasswd保护WordPress控制面板
正在用Wordpress的博主们一定知道最近全球兴起的一波黑客锁定Wordpress暴力破解控制面板密码的风波了,据CloudFlare执行长Matthew Prince所说,所谓的暴力密码攻击是输入 ...
- Android 应用层知识纲要
Java基础 * 面向对象 * Java集合框架 * 异常处理 * Java反射, Spring框架,通过反射实现 * 泛型, 静态变成语言 * 文件操作 Android基础 * Activity * ...
- python【第八篇】socket网络编程
内容大纲 1.socke基础 两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 建 立网络通信连接至少要一对端口号(socket).socket本质是编程接口(API ...
- apt-cache madison package-name
apt-cache madison package-name 搜索软件有那些可用版本,
- operation 多线程
2.Cocoa Operation 优点:不需要关心线程管理,数据同步的事情.Cocoa Operation 相关的类是 NSOperation ,NSOperationQueue.NSOperati ...
- 2、MyBatis.NET学习笔记之CodeSmith使用
说明:本系列随笔会与CSDN同步发布,当然这里先发,因为这里可以用WLW.但刚才由于误操作,没有重新发上来.只好先在CSDN先发了.重往这里发时图片无法处理,索性直接粘过来吧! 使用框架后一些相关的配 ...
- c# appdomain
http://www.cnblogs.com/Terrylee/archive/2005/11/28/285809.html
- BZOJ 1263 整数划分
Description 从文件中读入一个正整数\(n\).要求将\(n\)写成若干个正整数之和,并且使这些正整数的乘积最大. 例如,\(n=13\),则当\(n\)表示为\(4+3+3+3\)(或\( ...