注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

是缘是情是童真

如果你用笑脸面对 你会看见别人的笑脸

 
 
 

日志

 
 

如何获取手机中已安装的软件程序  

2011-07-05 09:36:48|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
public class ShowAppActivity extends Activity implements Runnable ,OnItemClickListener {
private static final int SEARCH_APP = 0;
private static final int DELETE_APP = 1;
GridView gv;
ListView lv;
private List<PackageInfo> packageInfos;
private List<PackageInfo> userPackageInfos;
private List<PackageInfo> showPackageInfos;
private ProgressDialog pd;
ImageButton ib_change_category;
ImageButton ib_change_view;
private boolean allApplication = true;
private boolean isListView = false;
private Handler mHandler = new Handler(){

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == SEARCH_APP) {
showPackageInfos = packageInfos;
gv.setAdapter(new GridViewAdapter(ShowAppActivity.this,showPackageInfos));
lv.setAdapter(new ListViewAdapter(ShowAppActivity.this,showPackageInfos));
pd.dismiss();
setProgressBarIndeterminateVisibility(false);
}
if(msg.what == DELETE_APP) {
System.out.println("Delete App Success!!");
}
}
};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.show_app_grid);
        setProgressBarIndeterminateVisibility(true);
        
        AnimationSet set = new AnimationSet(false);
        Animation animation = new AlphaAnimation(0,1);
        animation.setDuration(500);
        set.addAnimation(animation);
        
        animation = new TranslateAnimation(1, 13, 10, 50);
        animation.setDuration(300);
        set.addAnimation(animation);
        
        animation = new RotateAnimation(30,10);
        animation.setDuration(300);
        set.addAnimation(animation);
        
        animation = new ScaleAnimation(5,0,2,0);
        animation.setDuration(300);
        set.addAnimation(animation);
        
        LayoutAnimationController controller = new LayoutAnimationController(set, 1);
        
        
        gv = (GridView) this.findViewById(R.id.gv_apps);
        gv.setLayoutAnimation(controller);
        
        lv = (ListView)this.findViewById(R.id.lv_apps);
        lv.setLayoutAnimation(controller);
        lv.setCacheColorHint(0);
        gv.setOnItemClickListener(this);
        lv.setOnItemClickListener(this);
        
        ib_change_category = (ImageButton) this.findViewById(R.id.ib_change_category);
        ib_change_view = (ImageButton) this.findViewById(R.id.ib_change_view);
        ib_change_view.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(isListView) {
//Toast.makeText(ShowAppActivity.this, "网格显示", Toast.LENGTH_SHORT).show();
MyToast.myToastShow(ShowAppActivity.this, R.drawable.grids, "网格显示", Toast.LENGTH_SHORT);
ib_change_view.setImageResource(R.drawable.grids);
lv.setVisibility(View.GONE);
gv.setVisibility(View.VISIBLE);
//AlphaAnimation 控制渐变透明的动画效果
//ScaleAnimation 控制尺寸伸缩的动画效果
//TranslateAnimation  控制画面平移的动画效果
//RotateAnimation  控制画面角度变化的动画效果
//LayoutAnimation  渲染ViewGroup中每个View显示时候的动画效果
AnimationSet  set = new AnimationSet(false);
Animation animation = new RotateAnimation(60, 0);
animation.setInterpolator(ShowAppActivity.this, android.R.anim.overshoot_interpolator);
animation.setDuration(1000);
set.addAnimation(animation);
animation = new AlphaAnimation(0, 1);
animation.setDuration(500);
set.addAnimation(animation);
gv.startAnimation(set);
//Animation animation = AnimationUtils.loadAnimation(ShowAppActivity.this, R.anim.set1);
//gv.startAnimation(set);
gv.startLayoutAnimation();
isListView  = false;
} else {
//Toast.makeText(ShowAppActivity.this, "列表显示", Toast.LENGTH_SHORT).show();
MyToast.myToastShow(ShowAppActivity.this, R.drawable.list, "列表显示", Toast.LENGTH_SHORT);
ib_change_view.setImageResource(R.drawable.list);
gv.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
AnimationSet set = new AnimationSet(false);

Animation animation = new TranslateAnimation(200, 1, 200, 1);
animation.setDuration(500);
animation.setInterpolator(ShowAppActivity.this, android.R.anim.bounce_interpolator);
set.addAnimation(animation);
animation = new ScaleAnimation(0, 1, 0, 1);
animation.setDuration(500);
set.addAnimation(animation);
//Animation animation = AnimationUtils.loadAnimation(ShowAppActivity.this, R.anim.set2);
lv.startAnimation(set);
isListView = true;
}
}
       
        });
        ib_change_category.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
if(allApplication) {
ib_change_category.setImageResource(R.drawable.user);
//gv.setAdapter(new GridViewAdapter(ShowAppActivity.this,userPackageInfos));
showPackageInfos = userPackageInfos;
allApplication = false;
//Toast.makeText(ShowAppActivity.this, "用户安装的程序列表", Toast.LENGTH_SHORT).show();
MyToast.myToastShow(ShowAppActivity.this, R.drawable.user, "用户安装的程序列表", Toast.LENGTH_SHORT);
} else {
ib_change_category.setImageResource(R.drawable.all);
//gv.setAdapter(new GridViewAdapter(ShowAppActivity.this,packageInfos));
showPackageInfos = packageInfos;
allApplication = true;
//Toast.makeText(ShowAppActivity.this, "所有程序列表", Toast.LENGTH_SHORT).show();
MyToast.myToastShow(ShowAppActivity.this, R.drawable.all, "所有程序列表", Toast.LENGTH_SHORT);
}
gv.setAdapter(new GridViewAdapter(ShowAppActivity.this,showPackageInfos));
lv.setAdapter(new ListViewAdapter(ShowAppActivity.this,showPackageInfos));
}});
        pd = ProgressDialog.show(this, "请稍候...", "正在搜索你所安装的应用程序...",true,false);
        Thread t = new Thread(this);
        t.start();
        
    }
    
    
    
    class GridViewAdapter extends BaseAdapter {
   
    LayoutInflater inflater;
    List<PackageInfo> pkInfos;
   
    public GridViewAdapter(Context context,List<PackageInfo> packageInfos) {
    inflater = LayoutInflater.from(context);
    this.pkInfos = packageInfos;
    }

@Override
public int getCount() {
// TODO Auto-generated method stub
return pkInfos.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return pkInfos.get(arg0);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.gv_item, null);
TextView tv = (TextView) view.findViewById(R.id.gv_item_appname);
ImageView iv = (ImageView) view.findViewById(R.id.gv_item_icon);
//tv.setText(packageInfos.get(position).packageName);
tv.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager()));
iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager()));
return view;
}
   
    }

    class ListViewAdapter extends BaseAdapter {
    LayoutInflater inflater;
    List<PackageInfo> pkInfos;
   
    public ListViewAdapter(Context context,List<PackageInfo> packageInfos) {
    inflater = LayoutInflater.from(context);
    this.pkInfos = packageInfos;
    }

@Override
public int getCount() {
// TODO Auto-generated method stub
return pkInfos.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return pkInfos.get(arg0);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.lv_item, null);
TextView tv_appname = (TextView) view.findViewById(R.id.lv_item_appname);
TextView tv_packagename = (TextView) view.findViewById(R.id.lv_item_packageame);
ImageView iv = (ImageView) view.findViewById(R.id.lv_icon);
//tv.setText(packageInfos.get(position).packageName);
tv_appname.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager()));
tv_packagename.setText(pkInfos.get(position).packageName);
iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager()));
return view;
}
    }
@Override
public void run() {
packageInfos = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES);
userPackageInfos = new ArrayList<PackageInfo>();
for(int i=0;i<packageInfos.size();i++) {
PackageInfo temp = packageInfos.get(i);
ApplicationInfo appInfo = temp.applicationInfo;
boolean flag = false;
if((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
// Updated system app
flag = true;
} else if((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
// Non-system app
flag = true;
}
if(flag) {
userPackageInfos.add(temp);
}
}
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mHandler.sendEmptyMessage(SEARCH_APP);
try {
Thread.currentThread().sleep(5000);
mHandler.sendEmptyMessage(DELETE_APP);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
final PackageInfo tempPkInfo = showPackageInfos.get(position);
//new Dialog to choose a choice
//创建Dialog的构造器
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选项");
builder.setItems(R.array.choice,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
String packageName = tempPkInfo.packageName;
ActivityInfo activityInfo = tempPkInfo.activities[0];
if(activityInfo == null) {
Toast.makeText(ShowAppActivity.this, "没有任何activity", Toast.LENGTH_SHORT).show();
return;
}
String activityName = activityInfo.name;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName,activityName));
startActivity(intent);
break;
case 1:
showAppDetail(tempPkInfo);
break;
case 2:
Uri packageUri = Uri.parse("package:" + tempPkInfo.packageName);
Intent deleteIntent = new Intent();
deleteIntent.setAction(Intent.ACTION_DELETE);
deleteIntent.setData(packageUri);
startActivityForResult(deleteIntent, 0);
break;
}
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
packageInfos = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES);
userPackageInfos = new ArrayList<PackageInfo>();
for(int i=0;i<packageInfos.size();i++) {
PackageInfo temp = packageInfos.get(i);
ApplicationInfo appInfo = temp.applicationInfo;
boolean flag = false;
if((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
// Updated system app
flag = true;
} else if((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
// Non-system app
flag = true;
}
if(flag) {
userPackageInfos.add(temp);
}
}
if(allApplication) {
showPackageInfos = packageInfos;
} else {
showPackageInfos = userPackageInfos;
}
gv.setAdapter(new GridViewAdapter(ShowAppActivity.this,showPackageInfos));
lv.setAdapter(new ListViewAdapter(ShowAppActivity.this,showPackageInfos));
}
@Override
protected void onResume() {
super.onResume();
}
private void showAppDetail(PackageInfo packageInfo) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("详细信息");
StringBuffer message = new StringBuffer();
message.append("程序名称:" + packageInfo.applicationInfo.loadLabel(getPackageManager()));
message.append("\n 包名:" + packageInfo.packageName);
message.append("\n 版本号:" + packageInfo.versionCode);
message.append("\n 版本名:" + packageInfo.versionName);
builder.setMessage(message.toString());
builder.setIcon(packageInfo.applicationInfo.loadIcon(getPackageManager()));
builder.setPositiveButton("确定", null);
builder.create().show();
}
    
}
  评论这张
 
阅读(446)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018