android开发(16) 遍历所有的已经安装的应用程序,启动其他的应用程序
得到一个需求:想再我们的应用程序里调用其他的应用程序。
做出尝试后,我的方法时这样:
我们先把 即将调用的应用程序成为 目标应用程序
1.获得目标应用的 包名( packageName)
2.使用 getPackageManager().getLaunchIntentForPackage(packageName); 获得 一个 “启动intent",该方法会返回一个”可以启动该应用程序的intent“
3.启动 该intent. startActivity(intent);
那么其实,我们仅仅是 需要一个参数: packageName 。
开始贴代码。
---------------------
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" >
</ListView>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="60dp">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
List<ApplicationInfo> packages;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packages = getPackageManager().getInstalledApplications(0);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new MyAdapter());
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
ApplicationInfo appInfo = packages.get(arg2);
Intent intent = getPackageManager().getLaunchIntentForPackage(
appInfo.packageName);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
List<Map<String, Object>> list;
class MyAdapter extends BaseAdapter {
public void Query() {
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return packages.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View v = getLayoutInflater().inflate(R.layout.item, null);
ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
TextView textview1 = (TextView) v.findViewById(R.id.textView1);
TextView textview2 = (TextView) v.findViewById(R.id.textView2);
// 获得包名
ApplicationInfo app = (ApplicationInfo) packages.get(arg0);
String packageName = app.packageName;
textview2.setText(packageName);
// 获得图标
Drawable dr = getPackageManager().getApplicationIcon(app);
iv.setImageDrawable(dr);
String label = "";
try {
label = getPackageManager().getApplicationLabel(app).toString();
textview1.setText(label);
} catch (Exception e) {
Log.i("Exception", e.toString());
}
return v;
}
}
}
最后 提供代码下载