Android 中Luncher 負責加載系統中安裝的app,文件夾, 控件 而且將須要在桌面顯示出來的app,文件夾,組件展示出來。java
其中要顯示的組件信息都存在系統的provider當中。android
簡單的luncher 實現:app
建立一個android 工程,在Androidmanifest.xml文件的 MainActivity 中 Intent-filter標籤中添加如下代碼,紅色字體部分:ide
<application字體
android:allowBackup="true"this
android:icon="@drawable/ic_launcher"spa
android:label="@string/app_name"code
android:theme="@style/AppTheme" >xml
<activityget
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; } }