public class Act_Share extends Activity {
private ShareCustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_share);
ImageView btn_back = (ImageView) findViewById(R.id.share_back);
btn_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
ListView shareList = (ListView) findViewById(R.id.act_share_list);
List<AppInfo> shareAppInfos = getShareAppList();
adapter = new ShareCustomAdapter(this, shareAppInfos);
shareList.setAdapter(adapter);
shareList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AppInfo appInfo = (AppInfo) adapter.getItem(position);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setComponent(new ComponentName(appInfo.packageName,
appInfo.appLauncherClassName));
shareIntent.setType("image/*");
File file = new File("/sdcard/com.igoatech.wxrepack/shareimg.png");
if (!file.exists()) {
Bitmap bm = FileUtil.drawableToBitmap(getResources()
.getDrawable(R.drawable.erweima));
FileUtil.saveBitmap(bm, file);
}
Uri imageUri = Uri.fromFile(file);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(shareIntent);
}
});
}
private List<AppInfo> getShareAppList() {
List<AppInfo> shareAppInfos = new ArrayList<AppInfo>();
PackageManager packageManager = getPackageManager();
List<ResolveInfo> resolveInfos = getShareApps(this);
if (null == resolveInfos) {
return null;
} else {
for (ResolveInfo resolveInfo : resolveInfos) {
AppInfo appInfo = new AppInfo();
appInfo.packageName = resolveInfo.activityInfo.packageName;
appInfo.appLauncherClassName = resolveInfo.activityInfo.name;
appInfo.appName = resolveInfo.loadLabel(packageManager)
.toString();
appInfo.appIcon = resolveInfo.loadIcon(packageManager);
shareAppInfos.add(appInfo);
}
}
return shareAppInfos;
}
public List<ResolveInfo> getShareApps(Context context) {
List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
Intent intent = new Intent(Intent.ACTION_SEND, null);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("text/plain");
PackageManager pManager = context.getPackageManager();
mApps = pManager.queryIntentActivities(intent,
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
return mApps;
}
}
XML:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/all_bg" >
<LinearLayout
android:id="@+id/share_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/main_bg"
android:gravity="center_vertical" >
<ImageView
android:id="@+id/share_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@drawable/fanhui" />
<TextView
style="@style/all_title"
android:text="@string/share" />
</LinearLayout>
<ListView
android:id="@+id/act_share_list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@id/share_title"
android:layout_marginTop="4dp"
android:background="#FFFFFF"
android:cacheColorHint="#00000000"
android:divider="@color/line"
android:dividerHeight="1.0dip"
android:fadingEdge="none"
android:footerDividersEnabled="false"
android:headerDividersEnabled="true" />
</RelativeLayout>
adapter:
public class ShareCustomAdapter extends BaseAdapter {
private Context context;
private List<AppInfo> list;
public ShareCustomAdapter(Context context, List<AppInfo> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View v, ViewGroup arg2) {
if (v == null) {
v = LayoutInflater.from(context).inflate(
R.layout.share_item, null);
}
AppInfo item = list.get(position);
ImageView iv = (ImageView) v.findViewById(R.id.share_item_icon);
TextView tv = (TextView) v.findViewById(R.id.share_item_name);
iv.setImageDrawable(item.appIcon);
tv.setText(item.appName);
v.setTag(item);
return v;
}
}
XML:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/share_item_icon"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="3.0dip"
android:scaleType="fitXY" />
<TextView
android:id="@+id/share_item_name"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享"
android:singleLine="true"
android:textSize="14sp"
android:textColor="@color/item_text"
android:layout_marginLeft="5dip" />
</LinearLayout>
public class AppInfo {
public String appName = "";
public String packageName = "";
public String versionName = "";
public int versionCode = 0;
public Drawable appIcon = null;
public String date = "";
public String size = "";
public String iconUrl = "";
public String downloadurl = "";
public String brief = "";
public int downloadPercent = 0;
public boolean isInWhite = false;
public PackageInfo packageInfo = null;
public String isInstalled = "false";
public String path = "";
public PackageInfo packageinfo = null;
public String appLauncherClassName = "";
public int number = 0;
public String filePath = "";
public int installedType = 1;
public int upgrade = 0;
public boolean isChecked = false;
public void print() {
Log.v("app", "Name:" + appName + " Package:" + packageName);
Log.v("app", "Name:" + appName + " versionName:" + versionName);
Log.v("app", "Name:" + appName + " versionCode:" + versionCode);
}
}
图片处理代码:
/**
* Drawableת��ΪBitmap
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;
}
/** ���淽�� */
public static void saveBitmap(Bitmap bm, File file) {
// Log.e(TAG, "����ͼƬ");
try {
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
// Log.i(TAG, "�Ѿ�����");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}