Android 中Luncher 负责加载系统中安装的app,文件夹, 控件 并且将需要在桌面显示出来的app,文件夹,组件展现出来。
其中要显示的组件信息都存在系统的provider当中。
简单的luncher 实现:
创建一个android 工程,在Androidmanifest.xml文件的 MainActivity 中 Intent-filter标签中添加以下代码,红色字体部分:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.runinnerappdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
编译运行后,按home键时,就会弹出一个窗口,会让你选择,Home的内容:
这时就可以选择我们自己写的那个工程,开机的时候也一样会提示该窗口。
以下代码实现的功能是在ListView 中显示可运行程序的包名,点击则运行该程序
工程代码:
MainActivity:
package com.example.runinnerappdemo;
import java.util.List;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity implements OnItemClickListener{
ListView listView;
MyBaseAdapter adapter;
List<ResolveInfo> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=(ListView)findViewById(R.id.listView1);
adapter=new MyBaseAdapter(this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
addApplication();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 加载可启动的app
*/
private void addApplication(){
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
list= getPackageManager().queryIntentActivities(mainIntent, 0);
for (ResolveInfo resolveInfo : list) {
adapter.addItem(resolveInfo.activityInfo.name);
}
adapter.notifyDataSetChanged();
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
ResolveInfo info=list.get(arg2);
Intent intent =new Intent();
intent.setComponent(new ComponentName(info.activityInfo.packageName, info.activityInfo.name));
startActivity(intent);
}
}
MybaseAdapter代码:
package com.example.runinnerappdemo;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
private Holder holder;
private LayoutInflater mInflater;
private ArrayList<String> n_list;
public MyBaseAdapter(Context context) {
mInflater = LayoutInflater.from(context);
n_list = new ArrayList<String>();
}
public void addItem(String item) {
n_list.add(item);
}
public void clearItems() {
n_list.clear();
notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return n_list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return n_list.get(position);
}
@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
if (convertView == null) {
holder = new Holder();
convertView = mInflater.inflate(R.layout.item, null);
holder.textView1 = (TextView) convertView
.findViewById(R.id.textView1);
convertView.setTag(holder);
}
Holder holder = (Holder) convertView.getTag();
holder.textView1.setText(n_list.get(position));
return convertView;
}
/**
*
* @author admin
*
*/
class Holder {
TextView textView1;
}
}