編輯:初級開發
獲取Launcher 啟動列表
即 列出所有Launcher程序 通過PackageManager 來獲取
[代碼 步驟]
1. 定義內部類 LauncherItem 用於定義Application相關屬性 比如:圖標 名稱 以及 ComponentName
Java代碼
public class LauncherItem {
Drawable icon;
String name;
ComponentName component;
LauncherItem(Drawable d, String s,ComponentName cn){
icon = d;
name = s;
component = cn;
}
};
public class LauncherItem {
Drawable icon;
String name;
ComponentName component;
LauncherItem(Drawable d, String s,ComponentName cn){
icon = d;
name = s;
component = cn;
}
};
2. 定義List
Java代碼
public void addLauncher(){
lvalue = new ArrayList
pkgMgt = this.getPackageManager();
//to query all launcher & load into List<>
Intent it = new Intent(Intent.ACTION_MAIN);
it.addCategory(Intent.CATEGORY_LAUNCHER);
List
for(int i=0;i
ActivityInfo ai = ra.get(i).activityInfo;
//String ainfo = ai.toString();
Drawable icon = ai.loadIcon(pkgMgt);
String label = ai.loadLabel(pkgMgt).toString();
ComponentName c = new ComponentName(ai.applicationInfo.packageName,ai.name);
LauncherItem item = new LauncherItem(icon,label,c);
lvalue.add(item);
}
}
public void addLauncher(){
lvalue = new ArrayList
pkgMgt = this.getPackageManager();
//to query all launcher & load into List<>
Intent it = new Intent(Intent.ACTION_MAIN);
it.addCategory(Intent.CATEGORY_LAUNCHER);
List
for(int i=0;i
ActivityInfo ai = ra.get(i).activityInfo;
//String ainfo = ai.toString();
Drawable icon = ai.loadIcon(pkgMgt);
String label = ai.loadLabel(pkgMgt).toString();
ComponentName c = new ComponentName(ai.applicationInfo.packageName,ai.name);
LauncherItem item = new LauncherItem(icon,label,c);
lvalue.add(item);
}
}
3. 定義LauncherAdapter 並指定各個item顯示樣式
Java代碼
public class LauncherAdapter extends BaseAdapter {
Activity activity;
public LauncherAdapter(Activity a){
activity = a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lvalue.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@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
return composeItem(position);
}
public VIEw composeItem(int position){
LinearLayout layout = new LinearLayout(activity);
layout.setOrIEntation(LinearLayout.HORIZONTAL);
ImageView iv = new ImageVIEw(activity);
iv.setImageDrawable(lvalue.get(position).icon);
layout.addVIEw(iv);
TextView tv = new TextVIEw(activity);
tv.setText(lvalue.get(position).name);
tv.setPadding(10, 5, 0, 0);
layout.addVIEw(tv);
return layout;
}
}
public class LauncherAdapter extends BaseAdapter {
Activity activity;
public LauncherAdapter(Activity a){
activity = a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lvalue.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@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
return composeItem(position);
}
public VIEw composeItem(int position){
LinearLayout layout = new LinearLayout(activity);
layout.setOrIEntation(LinearLayout.HORIZONTAL);
ImageView iv = new ImageVIEw(activity);
iv.setImageDrawable(lvalue.get(position).icon);
layout.addVIEw(iv);
TextView tv = new TextVIEw(activity);
tv.setText(lvalue.get(position).name);
tv.setPadding(10, 5, 0, 0);
layout.addVIEw(tv);
return layout;
}
}
4. 啟動某個item 當單擊時
Java代碼
adapter = new LauncherAdapter(this);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView arg0, VIEw arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent =new Intent(Intent.ACTION_VIEW);
intent.setComponent(lvalue.get(arg2).component);
startActivity(intent);
}
});
adapter = new LauncherAdapter(this);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView arg0, VIEw arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent =new Intent(Intent.ACTION_VIEW);
intent.setComponent(lvalue.get(arg2).component);
startActivity(intent);
}
});
5. emulator 結果結果
- 列出所有application
- 單擊Alarm Clock 的情形:
眾所周知,在寫 android 程序的時候,很容易出現 OOM ,而出現的時機大多數是由 Bitmap decode 引發的: &
外特性空間的Activity 我們先來看看,Android應用開發人員接觸的外特性空間中的Activity,對於AMS (activ
上課講義摘錄之13:android的IBinder介面及其安全性機制Activity物件與Service物件在不同的進程(Process)裡執行,各有不同的UID(Un
File 讀寫[功能]因為文件讀寫很平常 使用打算把這個功能寫出輔助類的形式 以便以後方便使用 就是:FileIOHelper[代碼]1. 定義指定的File 以及其上