日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android获取LAUNCHER,Android 获取Launcher 启动列表

發布時間:2023/12/2 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android获取LAUNCHER,Android 获取Launcher 启动列表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

先留著有時間再看

獲取Launcher 啟動列表

即 列出所有Launcher程序 通過PackageManager 來獲取

[代碼 步驟]

1. 定義內部類 LauncherItem 用于定義Application相關屬性 比如:圖標 名稱 以及 ComponentName

Java代碼

1  public class LauncherItem {

2

3   Drawable icon;

4

5   String name;

6

7   ComponentName component;

8

9   LauncherItem(Drawable d, String s,ComponentName cn){

10

11   icon = d;

12

13   name = s;

14

15   component = cn;

16

17   }

18

19   };

20

21   public class LauncherItem {

22

23   Drawable icon;

24

25   String name;

26

27   ComponentName component;

28

29   LauncherItem(Drawable d, String s,ComponentName cn){

30

31   icon = d;

32

33   name = s;

34

35   component = cn;

36

37   }

38

39   };

40

2. 定義List lvalue 用于存放查詢結果

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 ra =pkgMgt.queryIntentActivities(it,0);

for(int i=0;i< p>

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 ra =pkgMgt.queryIntentActivities(it,0);

for(int i=0;i< p>

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获取LAUNCHER,Android 获取Launcher 启动列表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。