Android桌面应用

关于Android桌面开发:

为什么很多很多公司都要开发桌面呢,很简单,开发了桌面就了解了用户手机上的一切用户的操作:使用每个软件的频率,关于每个软件的相关信息。这个价值是巨大的。

Android桌面应用_第1张图片


就是自己实现一个桌面的应用:

原理:1.通过包管理器得到各个程序相关信息

            2.用布局显示出来

            3.禁用掉返回键


public class MainActivity extends Activity {
	private List<String> packnames;
	private GridView  gv;
	PackageManager pm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		gv=(GridView) findViewById(R.id.gv);
		//包管理器
	  <strong>  pm=getPackageManager();
		Intent intent=new Intent();
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");</strong>
		
		<span style="font-size: 13.3333339691162px; font-family: Arial, Helvetica, sans-serif;"><strong>//查询出来所有的手机上具有启动能力的activity</strong></span>

<strong>		List<ResolveInfo> infos=pm.queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS);
		packnames=new ArrayList<String>();</strong>
		for(ResolveInfo info:infos){
			String packname=info.activityInfo.packageName;
			packnames.add(packname);
		}
		
		gv.setAdapter(new MyAdapter());
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				<strong>String packname=packnames.get(position);
				Intent intent=pm.getLaunchIntentForPackage(packname);
				startActivity(intent);</strong>
			}
		});
	}
	
	<strong>//禁用返回键
	@Override
	public void onBackPressed() {
		 
	} </strong>
	private class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return packnames.size();
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view=View.inflate(getApplicationContext(), R.layout.item, null);
			TextView tv=(TextView) view.findViewById(R.id.tv);
			ImageView iv=(ImageView) view.findViewById(R.id.iv);
			String packname=packnames.get(position);
			try {
				 <strong>tv.setText(pm.getPackageInfo(packname, 0).applicationInfo.loadLabel(pm));
			    iv.setImageDrawable(pm.getPackageInfo(packname, 0).applicationInfo.loadIcon(pm));</strong>
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		        tv.setTextColor(Color.BLACK);
			return view;
		}

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

}


布局文件如下:

<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"
    tools:context="com.example.Destop.MainActivity" >

    <GridView
        android:numColumns="4"
        android:id="@+id/gv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/iv"
        android:layout_width="50dip"
        android:layout_height="50dip"
        />
    <TextView 
        android:singleLine="true"
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        />
</LinearLayout>


你可能感兴趣的:(Android桌面应用)