引用:http://mmqzlj.blog.51cto.com/2092359/752799
由于多数客户是手机盲,不知道如何快速安装apk软件到android手机上,故而公司提出开发一个利用无线在两台android手机上分享已安装软件的需求。
近段时间在网上搜索关于蓝牙分享软件的例子,未果!
发现android自带的examples里有个bluetoothChat的例子,里面关于蓝牙的调用例子看起来非常繁琐,先判断是否支持蓝牙,再判断蓝牙是否打开,再去搜索蓝牙设备,然后在配对,配对后发送信息。在两个手机上分别装了试试,发现根本无法发送信息。几天下来一无所获。最后无意中google了一下,看到一段极端简单的代码调用android自带的ACTION_SEND,居然可以了。
避免更多的人绕弯路,现把android手机之间分享已安装软件的代码贴出,供大家参考。
主要代码:
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.SimpleAdapter.ViewBinder;
public class MainActivity extends Activity implements OnClickListener{
public static final String TAG = "AppListActivity";
private ListView listView;
private List<Map<String,Object>> list;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v(TAG,"created");
super.onCreate(savedInstanceState);
this.setContentView(R.layout.app_list);
listView = (ListView)this.findViewById(R.id.listView1);
list = new ArrayList<Map<String,Object>>();
List<PackageInfo> appListInfo=this.getPackageManager().getInstalledPackages(0);
for (PackageInfo p : appListInfo) {
if(p.applicationInfo.sourceDir.startsWith("/system/app/")){
continue;
}
Map<String,Object> map = new HashMap<String,Object>();
Drawable icon = null;
String appName= "";
try{
appName = this.getPackageManager().getApplicationLabel(p.applicationInfo).toString();
icon = this.getPackageManager().getApplicationIcon(p.applicationInfo.packageName);
}catch(Exception e){
e.printStackTrace();
}
map.put("name", appName);
map.put("package", p.applicationInfo.packageName);
map.put("sourceDir", p.applicationInfo.sourceDir);
map.put("icon",icon);
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.app_list_item, new String[]{"name","icon"}, new int[]{R.id.tv_name,R.id.iv_icon});
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View view, Object data, String textRepresentation) {
//判断是否为我们要处理的对象
if(view instanceof ImageView && data instanceof Drawable){
ImageView iv = (ImageView) view;
iv.setImageDrawable((Drawable)data);
return true;
}else
return false;
}
});
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if(list.get(position).get("sourceDir")!=null){
File f = new File(list.get(position).get("sourceDir").toString());
//调用android分享窗口
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(intent);
}
return false;
}
});
}
/* (non-Javadoc)
* @see android.app.Activity#onDestroy()
*/
@Override
protected void onDestroy() {
Log.v(TAG, "destroy");
super.onDestroy();
}
@Override
public void onClick(View v) {
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.SimpleAdapter.ViewBinder;
public class MainActivity extends Activity implements OnClickListener{
public static final String TAG = "AppListActivity";
private ListView listView;
private List<Map<String,Object>> list;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v(TAG,"created");
super.onCreate(savedInstanceState);
this.setContentView(R.layout.app_list);
listView = (ListView)this.findViewById(R.id.listView1);
list = new ArrayList<Map<String,Object>>();
List<PackageInfo> appListInfo=this.getPackageManager().getInstalledPackages(0);
for (PackageInfo p : appListInfo) {
if(p.applicationInfo.sourceDir.startsWith("/system/app/")){
continue;
}
Map<String,Object> map = new HashMap<String,Object>();
Drawable icon = null;
String appName= "";
try{
appName = this.getPackageManager().getApplicationLabel(p.applicationInfo).toString();
icon = this.getPackageManager().getApplicationIcon(p.applicationInfo.packageName);
}catch(Exception e){
e.printStackTrace();
}
map.put("name", appName);
map.put("package", p.applicationInfo.packageName);
map.put("sourceDir", p.applicationInfo.sourceDir);
map.put("icon",icon);
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.app_list_item, new String[]{"name","icon"}, new int[]{R.id.tv_name,R.id.iv_icon});
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View view, Object data, String textRepresentation) {
//判断是否为我们要处理的对象
if(view instanceof ImageView && data instanceof Drawable){
ImageView iv = (ImageView) view;
iv.setImageDrawable((Drawable)data);
return true;
}else
return false;
}
});
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if(list.get(position).get("sourceDir")!=null){
File f = new File(list.get(position).get("sourceDir").toString());
//调用android分享窗口
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(intent);
}
return false;
}
});
}
/* (non-Javadoc)
* @see android.app.Activity#onDestroy()
*/
@Override
protected void onDestroy() {
Log.v(TAG, "destroy");
super.onDestroy();
}
@Override
public void onClick(View v) {
}
}
界面截图: