설 치 된 응용 프로그램 가 져 오기

6972 단어 설치 하 다.
main.xml 에서:
    
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/gridview"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:numColumns="auto_fit"  
        android:verticalSpacing="15dp"  
        android:horizontalSpacing="12dp"  
        android:columnWidth="95dp"  
        android:stretchMode="columnWidth"  
        android:gravity="center"  
        />  

 그리고 item.xml 에서 item 레이아웃 을 줍 니 다.
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      
    android:orientation="vertical"        
    android:layout_width="fill_parent"      
    android:layout_height="fill_parent"       
    android:gravity="center" >  
  
    <ImageView  
        android:id="@+id/apps_image"          
        android:layout_width="48dip"       
        android:layout_height="48dip"         
        android:icon="@drawable/ic_launcher" />      
  
    <TextView          
        android:id="@+id/apps_textview"       
        android:layout_width="wrap_content"       
        android:layout_height="wrap_content"          
        android:maxLines="1"          
        android:maxEms="5"        
        android:text="good" />  
  
</LinearLayout>  

 Activity 에 보이 기
public class UnloadingActivity extends Activity {
	//            
	List<AppsItemInfo> list;

	private GridView gridview;
	private PackageManager pManager;
	private String uninstallPackage; 
//	int state;
//	int mNum;
//	final static int PROGRESS_DIALOG = 0;
//	ProgressThread progressThread;
//	ProgressDialog progressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);

		//   gridview
		gridview = (GridView) findViewById(R.id.gridview);

		//     、   、  
		pManager = UnloadingActivity.this.getPackageManager();
		List<PackageInfo> appList = getAllApps(UnloadingActivity.this);

		list = new ArrayList<AppsItemInfo>();

		for (int i = 0; i < appList.size(); i++) {
			PackageInfo pinfo = appList.get(i);
			AppsItemInfo shareItem = new AppsItemInfo();
			//     
			shareItem.setIcon(pManager
					.getApplicationIcon(pinfo.applicationInfo));
			//         
			shareItem.setLabel(pManager.getApplicationLabel(
					pinfo.applicationInfo).toString());
			//          
			shareItem.setPackageName(pinfo.applicationInfo.packageName);

			list.add(shareItem);

		}

		//   gridview Adapter
		gridview.setAdapter(new baseAdapter());

		//        ,    
		 gridview.setOnItemClickListener(new ClickListener());
	
	}

	public static List<PackageInfo> getAllApps(Context context) {

		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		PackageManager pManager = context.getPackageManager();
		//          
		List<PackageInfo> packlist = pManager.getInstalledPackages(0);
		for (int i = 0; i < packlist.size(); i++) {
			PackageInfo pak = (PackageInfo) packlist.get(i);

			//                
			//             ,        ,           
			// if()     <=0        ,         
			if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
				//              
				apps.add(pak);
			}

		}
		return apps;
	}

	

	private class baseAdapter extends BaseAdapter {
		LayoutInflater inflater = LayoutInflater.from(UnloadingActivity.this);

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

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		public final class ViewHolder {
			private ImageView icon;
			private TextView label;
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder;
			if (convertView == null) {
				//   View   itemView R.layout.item  
				convertView = inflater.inflate(R.layout.item, null);
				holder = new ViewHolder();
				holder.icon = (ImageView) convertView
						.findViewById(R.id.apps_image);
				holder.label = (TextView) convertView
						.findViewById(R.id.apps_textview);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.icon.setImageDrawable(list.get(position).getIcon());
			holder.label.setText(list.get(position).getLabel().toString());

			return convertView;

		}

	}

	//             ,         
	 private class ClickListener implements OnItemClickListener {
	  
	 @Override
	 public void onItemClick(AdapterView<?> arg0, View arg1, int position,
	 long arg3) {
		  AppsItemInfo app = list.get(position);  
			 uninstallPackage = app.packageName;  
			 AppUtils.uninstallApk(UnloadingActivity.this, uninstallPackage);  

	 }
	  
	 }

	//       AppsItemInfo  ,             
	private class AppsItemInfo {

		private Drawable icon; //     
		private String label; //        
		private String packageName; //         

		public Drawable getIcon() {
			return icon;
		}

		public void setIcon(Drawable icon) {
			this.icon = icon;
		}

		public String getLabel() {
			return label;
		}

		public void setLabel(String label) {
			this.label = label;
		}

		public String getPackageName() {
			return packageName;
		}

		public void setPackageName(String packageName) {
			this.packageName = packageName;
		}

	}


}

좋은 웹페이지 즐겨찾기