关闭

Launcher的简单实现,

143人阅读 评论(0) 收藏 举报
分类:

简单应用launcher,把手机界面显示在自己的APP中,


这是清单文件中应该是的样子:

<activity
            android:name=".Launcher"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.HOME" />  
                <category android:name="android.intent.category.DEFAULT" /> 
            </intent-filter>
        </activity>

然后呢,为了盛放手机里面的已经下载的APP,所以当前项目的主界面显示用Gridview来展示:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

</RelativeLayout>

然后主界面代码:

 

package com.example.actionbardemo;

import java.util.Collections;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * actionBar使用需Android版本4.0以上,
 * 及Launcher的简单用法,将桌面放入自己的APP中。
 * @author xue
 *
 */
public class Launcher extends Activity implements OnItemClickListener{

	private PackageManager mPackageManager;
	private GridView mGridView;
	 private Context mContext;
	private List<ResolveInfo> mAllApps; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ActionBar bar = getActionBar();
		bar.setDisplayHomeAsUpEnabled(true);
//		bar.setHomeButtonEnabled(true);
		
		setupViews();
	}
	
	public void setupViews() {  
        mContext = Launcher.this;
        mPackageManager = getPackageManager();  
        mGridView = (GridView) findViewById(R.id.grid);  
        bindAllApps(); 
        // GridView中放入数据  
        mGridView.setAdapter(new GridItemAdapter(mContext, mAllApps));  
        mGridView.setNumColumns(4);  
        mGridView.setOnItemClickListener(this);  
    }  

	public void bindAllApps() {  
        // 应用中总有一个activity申明成这两个属性,也就是应用的入口  
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
        // 包含上面两个属性的全部查出来,并且排序  
        //简单来说就是,查询出手机上所有下载了的APP,是一个集合
        mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);  
        Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(  
                mPackageManager));  
    }  
	
	
    @Override  
    /**
     * 点击条目,跳转到该APP中去
     * 跳转实现用的是intent,查询到该条目的包名和主activity名称。
     */
    public void onItemClick(AdapterView<?> parent, View view, int position,  
            long id) {  
        ResolveInfo res = mAllApps.get(position);  
        // 该应用的包名和主Activity  
        String pkg = res.activityInfo.packageName;  
        String cls = res.activityInfo.name;  
        ComponentName componet = new ComponentName(pkg, cls);  
        Intent intent = new Intent();  
        intent.setComponent(componet);  
        startActivity(intent);  
    }  
    private class GridItemAdapter extends BaseAdapter {  
        private Context context;  
        private List<ResolveInfo> resInfo;  
        public GridItemAdapter(Context c, List<ResolveInfo> res) {  
            context = c;  
            resInfo = res;  
        }  
        @Override  
        public int getCount() {  
            return resInfo.size();  
        }  
        @Override  
        public Object getItem(int position) {  
            return null;  
        }  
        @Override  
        public long getItemId(int position) {  
            return 0;  
        }  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            convertView = LayoutInflater.from(context).inflate(  
                    R.layout.application_layout, null);  
            ImageView imageView = (ImageView) convertView  
                    .findViewById(R.id.imageview);  
            TextView textView = (TextView) convertView  
                    .findViewById(R.id.textview);  
            ResolveInfo res = resInfo.get(position);  
            imageView.setImageDrawable(res.loadIcon(mPackageManager));  
            textView.setText(res.loadLabel(mPackageManager).toString());  
            return convertView;  
        }  
    }
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		
		menu.add("Hello!");
		menu.add("World!");
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19937次
    • 积分:477
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:6篇
    • 译文:0篇
    • 评论:4条
    最新评论