簡單實現(xiàn)一個Launcher
1.javabean
public class AppNameIcon {
public StringappName;//應(yīng)用名
? ? public DrawableappIcon;//應(yīng)用圖標(biāo)
? ? public StringappClass;//類名
? ? public StringappPackage;//包名
? ? public IntentintentPackage;
? ? /*public AppNameIcon( Drawable appIcon,String appName) {
this.appName = appName;
this.appIcon = appIcon;
}*/
? ? public AppNameIcon(String appName, Drawable appIcon, String appClass, String appPackage,Intent intentPackage) {
this.appName = appName;
? ? ? ? this.appIcon = appIcon;
? ? ? ? this.appClass = appClass;
? ? ? ? this.appPackage = appPackage;
? ? ? ? this.intentPackage = intentPackage;
? ? }
public IntentgetIntentPackage() {
return intentPackage;
? ? }
public void setIntentPackage(Intent intentPackage) {
this.intentPackage = intentPackage;
? ? }
public StringgetAppClass() {
return appClass;
? ? }
public void setAppClass(String appClass) {
this.appClass = appClass;
? ? }
public StringgetAppPackage() {
return appPackage;
? ? }
public void setAppPackage(String appPackage) {
this.appPackage = appPackage;
? ? }
public StringgetAppName() {
return appName;
? ? }
public void setAppName(String appName) {
this.appName = appName;
? ? }
public DrawablegetAppIcon() {
return appIcon;
? ? }
public void setAppIcon(Drawable appIcon) {
this.appIcon = appIcon;
? ? }
}
2.獲取桌面應(yīng)用
public class AppNameAndIcon {
private ListmAppNameIconList;
? ? private AppNameIconmAppNameIcon;
? ? private PackageManagermPackageManager;
? ? public ListgetAppInfos(Context context) {
mPackageManager = context.getPackageManager();
? ? ? ? List packgeInfos =mPackageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
? ? ? ? mAppNameIconList =new ArrayList<>();
? ? ? ? //調(diào)用固定的數(shù)據(jù)
? ? ? ? /*DataDesktop desktop =new DataDesktop();
? ? ? ? List myAppNameIconList =? desktop.appInfos();
? ? ? ? for(int i =0; i < myAppNameIconList.size(); i++){
mAppNameIconList.add(myAppNameIconList.get(i));
? ? ? ? }*/
for (PackageInfo packgeInfo : packgeInfos) {
//去除系統(tǒng)應(yīng)用
? ? ? ? ? ? ApplicationInfo appInfo = packgeInfo.applicationInfo;
? ? ? ? ? ? if (!filterApp(appInfo)) {
continue;
? ? ? ? ? ? }
String packageName = packgeInfo.packageName;
? ? ? ? ? ? String className = packgeInfo.applicationInfo.className;
? ? ? ? ? ? String appName = packgeInfo.applicationInfo.loadLabel(mPackageManager).toString();
? ? ? ? ? ? Drawable drawable = packgeInfo.applicationInfo.loadIcon(mPackageManager);
? ? ? ? ? ? Intent intent =mPackageManager.getLaunchIntentForPackage(packageName);
? ? ? ? ? ? mAppNameIcon =new AppNameIcon(appName,drawable,className,packageName,intent);
? ? ? ? ? ? mAppNameIconList.add(mAppNameIcon);
? ? ? ? }
return mAppNameIconList;
? ? }
? ? //判斷某一個應(yīng)用程序是不是系統(tǒng)的應(yīng)用程序檬某,
? ? //如果是返回true,否則返回false啊央。
? ? public boolean filterApp(ApplicationInfo info) {
//有些系統(tǒng)應(yīng)用是可以更新的,如果用戶自己下載了一個系統(tǒng)的應(yīng)用來更新了原來的年堆,它還是系統(tǒng)應(yīng)用野来,這個就是判斷這種情況的
? ? ? ? if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) !=0) {
return true;
? ? ? ? ? ? //判斷是不是系統(tǒng)應(yīng)用
? ? ? ? }else if ((info.flags & ApplicationInfo.FLAG_SYSTEM) ==0) {
return true;
? ? ? ? }
return false;
? ? }
}
3.Adapter
public class AppNameIconAdapterextends RecyclerView.Adapter {
private Viewview;
? ? private ListmAppNameIconList;
? ? private AppNameIconmAppNameIcon;
? ? private ContextmContext;
? ? public ListgetmAppNameIconList() {
return mAppNameIconList;
? ? }
public void setmAppNameIconList(List mAppNameIconList) {
this.mAppNameIconList = mAppNameIconList;
? ? }
private LayoutInflatermInflater;
? ? private OnItemClickListenermOnItemClickListener;
? ? public AppNameIconAdapter(List appNameIconList,Context context) {
this.mAppNameIconList = appNameIconList;
? ? ? ? mAppNameIconList= fixListOrder(mAppNameIconList);
? ? ? ? this.mContext = context;
? ? ? ? mInflater = LayoutInflater.from(context);
? ? }
/**
? ? * 固定制定app的位置
? ? * @param mAppNameIconList
? ? * @return
? ? */
? ? private ListfixListOrder(List mAppNameIconList) {
List list=new ArrayList();
? ? ? ? for(int i=0;i
AppNameIcon appNameIcon=mAppNameIconList.get(i);
? ? ? ? ? ? switch (appNameIcon.getAppPackage()){
case "com.topwise.etone.demo":
list.add(appNameIcon);
? ? ? ? ? ? ? ? ? ? mAppNameIconList.remove(appNameIcon);
break;
? ? ? ? ? ? ? ? default:
i++;
break;
? ? ? ? ? ? }
}
list.addAll(mAppNameIconList);
? ? ? ? return? list;
? ? }
@NonNull
@Override
? ? public AppNameIconHolderonCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
view =mInflater.inflate(R.layout.desktop_app_item, parent, false);
//? ? ? ? view.setOnClickListener(this);
? ? ? ? return new AppNameIconHolder(view,mOnItemClickListener);
? ? }
@Override
? ? public void onBindViewHolder(@NonNull AppNameIconHolder holder, int position) {
/*if(position > 5){
}else {*/
? ? ? ? ? ? mAppNameIcon =mAppNameIconList.get(position);
? ? ? ? ? ? holder.appName.setText(mAppNameIcon.getAppName());
? ? ? ? ? ? holder.appIcon.setImageDrawable(mAppNameIcon.getAppIcon());
? ? ? ? ? ? holder.itemView.setTag(position);
? ? ? ? //}
? ? }
@Override
? ? public int getItemCount() {
return mAppNameIconList.size();
? ? }
//? ? @Override
//? ? public void onClick(View v) {
//? ? ? ? if(mOnItemClickListener != null){
//? ? ? ? ? ? mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
//? ? ? ? }
//? ? }
? ? public class AppNameIconHolderextends RecyclerView.ViewHolder{
public ImageViewappIcon;
? ? ? ? public TextViewappName;
? ? ? ? public AppNameIconHolder(View itemView, final AppNameIconAdapter.OnItemClickListener listener) {
super(itemView);
? ? ? ? ? ? this.appIcon = (ImageView)itemView.findViewById(R.id.movie_icon);
? ? ? ? ? ? this.appName = (TextView)itemView.findViewById(R.id.movie_name);
? ? ? ? ? ? itemView.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? ? ? public void onClick(View v) {
if(listener !=null){
listener.onItemClick(v, getPosition());
? ? ? ? ? ? ? ? ? ? }
}
});
? ? ? ? }
}
//接口回調(diào)
? ? public interface OnItemClickListener {
void onItemClick(View v, int position);
? ? }
//給接口設(shè)置set
? ? public void setOnItemClickListener(AppNameIconAdapter.OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
? ? }
}
4.Activity
4.1給activity配置默認launcher屬性
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.MONKEY" />
4.2系統(tǒng)activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
? ? supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
? ? setContentView(R.layout.activity_home);
? ? initView();
? ? getAppNameIcon();
}
//
//private void setS
//獲取應(yīng)用
private void getAppNameIcon(){
mAppNameAndIcon =new AppNameAndIcon();
? ? //LayoutManage manage = new LayoutManage(this,null,3,2);
//GridView gridView = new GridView(this, 3,3);
? ? GridLayoutManager layoutManager =new GridLayoutManager(this, 3);
? ? mRecyclerView.setLayoutManager(layoutManager);
? ? /*layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == 2 ? 1 : 3;
}
});*/
//mRecyclerView.setLayoutManager(layoutManager);
? ? mAppNameIconAdapter =new AppNameIconAdapter(mAppNameAndIcon.getAppInfos(this),HomeActivity.this);
? ? mRecyclerView.setHasFixedSize(true);
? ? mRecyclerView.setAdapter(mAppNameIconAdapter);
? ? mAppNameIconAdapter.setOnItemClickListener(this);
}
//初始化
private void initView(){
mToolbar = (Toolbar) findViewById(R.id.toolbar);
? ? setSupportActionBar(mToolbar);
? ? getSupportActionBar().setTitle(null);
? ? mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_app);
? ? mTitle = (TextView) findViewById(R.id.title);
? ? mTitle.setText(R.string.EtonePay_app);
}
@Override
public void onItemClick(View v, int position) {
????? Toast.makeText(this, "" + position, Toast.LENGTH_SHORT).show();
}