简单实现一个Launcher

简单实现一个Launcher 1.javabean public class AppNameIcon {
public StringappName; //应用名
public DrawableappIcon; //应用图标
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.获取桌面应用 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<>();
//调用固定的数据
/*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) {
//去除系统应用
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;
}
//判断某一个应用程序是不是系统的应用程序,
//如果是返回true,否则返回false。
public boolean filterApp(ApplicationInfo info) {
//有些系统应用是可以更新的,如果用户自己下载了一个系统的应用来更新了原来的,它还是系统应用,这个就是判断这种情况的
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) !=0) {
return true;
//判断是不是系统应用
}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);
returnlist;
}
@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());
}
}
});
}
}
//接口回调
public interface OnItemClickListener {
void onItemClick(View v, int position);
}
//给接口设置set
public void setOnItemClickListener(AppNameIconAdapter.OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
}
4.Activity 4.1给activity配置默认launcher属性





4.2系统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
//获取应用
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();
【简单实现一个Launcher】}

    推荐阅读