Launcher的简单实现,
来源:互联网 发布:淘宝上怎么买到真蜂蜜 编辑:程序博客网 时间:2024/06/05 09:55
简单应用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; @Overrideprotected 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; } }@Overridepublic 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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stubreturn super.onOptionsItemSelected(item);}}
0 0
- Launcher的简单实现,
- GridView实现简单的应用启动器Launcher
- Android实现简单Launcher的代码示例
- Android launcher应用的简单实现
- 简单的Launcher
- Launcher的简单介绍
- Launcher简要分析:Launcher的功能实现
- Launcher简要分析:Launcher的功能实现
- Launcher的简单讲解一
- Launcher的简单讲解二
- Android记录8--简单自定义Launcher实现
- Android中的简单的launcher介绍
- 实现类似launcher的滑动桌面
- Android launcher动态Icon的实现方法
- Android launcher动态Icon的实现方法
- Android 实现双Launcher的无缝切换
- Android Launcher开发(四)Launcher概述及简单添加和删除应用程序的快捷方式
- Android Launcher开发(四)Launcher概述及简单添加和删除应用程序的快捷方式
- 图像的膨胀与腐蚀、细化
- bolb layer
- 每天学习openstack(9)
- 探索.git目录
- FALSE/TRUE与false/true的区别
- Launcher的简单实现,
- js对象属性的getter和setter
- ubuntu apt-get update 更新失败
- 64位win7安装python2.7.3和MysqlDB的解决方案
- OC数据类型
- php 计算包含中英文的字符串的字符数
- error C2143 语法错误 缺少“;”(在“”的前面)
- C++字符串详解
- C和指针读书笔记(第十五章)