您的位置:首页 > 其它

获取手机安装的全部应用的示例教程

2013-07-17 14:43 537 查看
android用sharepreference保存输入框中的内容

http://www.eoeandroid.com/thread-199222-1-1.html

Android之ScrollView嵌套ListView

http://www.eoeandroid.com/thread-198859-1-1.html

android根据View的不同状态更换不同的背景

http://www.eoeandroid.com/thread-198029-1-1.html

package com.isoftstone.apk_demo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class APK_DemoActivity extends Activity implements android.view.View.OnClickListener{

/** Called when the activity is first created. */

public Intent mIntent;
static ProgressDialog pd;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);

}
@Override

public void onClick(View v) {
// TODO Auto-generated method stub

Intent intent=new Intent(this,main.class);

Dialog();
startActivity(intent);
}

public void Dialog(){

pd = new ProgressDialog(APK_DemoActivity.this);
            pd.setMessage("亲! 正在扫描中哦....");
pd.show();

}
}


package com.isoftstone.apk_demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class main extends Activity implements  android.view.View.OnClickListener{

/** Called when the activity is first created. */

public Intent mIntent;
private ProgressDialog pd;
int i=0;

public List<ResolveInfo> installApps = null;// 本机已经安装程序列表
private ListView lv_content;// 内容显示区域
public static final int installWhat = 1;// 安装程序列表
public static final int appPermissionWhat = 5;// 某个程序的权限页面处理
Context context;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main0);

Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);

try {
handleInstall();
}

catch (Exception e) {

// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 处理本机安装程序
*
* @throws Exception
*/

private void handleInstall() throws Exception {

// 1.设置操作按钮可见(扫描已安装程序)
// 3.得到本机安装应用程序

if (installApps == null) {

mIntent = new Intent(Intent.ACTION_MAIN, null);
mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
installApps = getPackageManager().queryIntentActivities(mIntent, 0);

}
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();// 将本机安装程序收集

for (ResolveInfo info : installApps) {

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", info.loadLabel(this.getPackageManager()).toString());

map.put("packageName", info.activityInfo.packageName);
map.put("img", info.loadIcon(this.getPackageManager()));
list.add(map);

}
setTitle("共安装" + installApps.size() + "个程序");// 设置标题

// 3.将程序显示到页面上
lv_content = (ListView) this.findViewById(R.id.listView1);

SearchAdapter adapter = new SearchAdapter(this, list,
R.layout.list_content_main, new String[] { "name", "packageName", "img" }, new int[] {

R.id.activityInfo_name, R.id.activityInfo_packageName,
R.id.activityInfo_img });// 自定义适配器

lv_content.setAdapter(adapter);
}
@Override

public void onClick(View v) {
// TODO Auto-generated method stub

finish();
APK_DemoActivity.pd.cancel();

}
}


package com.isoftstone.apk_demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
* 自定义Adapter
*
* @author machao
* @mail zeusmc.163.com
*
*/

public class SearchAdapter extends SimpleAdapter {

private Map<Integer, View> viewMap = new HashMap<Integer, View>();

private int[] mTo;
private String[] mFrom;
private ViewBinder mViewBinder;
private List<? extends Map<String, ?>> mData;
private int mResource;
private LayoutInflater mInflater;

public SearchAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) {

super(context, data, resource, from, to);

mData = data;
mResource = resource;
mFrom = from;
mTo = to;

mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView(int position, View convertView, ViewGroup parent) {

return createViewFromResource(position, convertView, parent, mResource);
}

private View createViewFromResource(int position, View convertView,ViewGroup parent, int resource) {

View rowView = this.viewMap.get(position);

if (rowView == null) {

rowView = mInflater.inflate(resource, null);

final int[] to = mTo;
final int count = to.length;
final View[] holder = new View[count];

for (int i = 0; i < count; i++) {

holder[i] = rowView.findViewById(to[i]);
}

rowView.setTag(holder);
bindView(position, rowView);
viewMap.put(position, rowView);

}
return rowView;
}

@SuppressWarnings("unchecked")

private void bindView(int position, View view) {

final Map dataSet = mData.get(position);
if (dataSet == null) {

return;

}

final ViewBinder binder = mViewBinder;
final View[] holder = (View[]) view.getTag();
final String[] from = mFrom;
final int[] to = mTo;
final int count = to.length;

for (int i = 0; i < count; i++) {

final View v = holder[i];

if (v != null) {

final Object data = dataSet.get(from[i]);

String urlText = null;

if (data == null) {

urlText = "";

} else {

urlText = data.toString();

}
boolean bound = false;

if (binder != null) {

bound = binder.setViewValue(v, data, urlText);

}
if (!bound) {

if (v instanceof CheckBox) {

((CheckBox) v).setText(data.toString());

} else if (v instanceof Checkable) {

if (data instanceof Boolean) {

((Checkable) v).setChecked((Boolean) data);
} else {

throw new IllegalStateException(v.getClass()

.getName()

+ " should be bound to a Boolean, not a "

+ data.getClass());
}

} else if (v instanceof TextView) {

setViewText((TextView) v, urlText);

} else if (v instanceof CheckBox) {

} else if (v instanceof ImageView) {

if (data instanceof Integer) {

setViewImage((ImageView) v, (Integer) data);
}

if (data instanceof Drawable) {
setViewImage((ImageView) v, (Drawable) data);

} else {
if (urlText != null && data != null) {

setViewImage((ImageView) v, urlText);
} else {

TextView tv = ((TextView) view .findViewById(R.id.activityInfo_name));

tv.setTextColor(Color.RED);
tv.setTextSize(18);
view.setClickable(false);
view.setFocusable(false);
view.setFocusableInTouchMode(false);

}

}

} else {

throw new IllegalStateException(

v.getClass().getName()

+ " is not a "

+ " view that can be bounds by this SimpleAdapter");

}
}
}
}
}

public void setViewImage(ImageView v, int value) {
v.setImageResource(value);

}

public void setViewImage(final ImageView v, Drawable drawable) {

Bitmap bitmap = Bitmap .createBitmap(

drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight(),

drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable .getIntrinsicHeight());

drawable.draw(canvas);
((ImageView) v).setImageBitmap(bitmap);

}

}


效果图:







下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐