android中怎么把自己需要的app启动图标集中到一个弹出框中

先看效果图

android中怎么把自己需要的app启动图标集中到一个弹出框中


这个是我们自己的apk点击之后的效果


下边是布局文件

activity_main.xml主布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
<TextView 
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:layout_marginTop="15dp"
   android:text="@string/app_name"/>


    <GridView 
        android:id="@+id/allapps"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</LinearLayout>


我用一个GridView 做容器


下边是单个item布局文件

application_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" 
    >
    
<ImageView 
   android:id="@+id/app_icon"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginTop="15dp"/>

<TextView
   android:id="@+id/app_title"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="12sp"
   android:gravity="center"/>
</LinearLayout>

下边是AndroidManifest.xml,这个里边有几个和普通apk不同的地方


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wind.lancherdemo"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.wind.lancherdemo.MainActivity"
            android:theme="@android:style/Theme.Dialog"                  <!--这个更改app theme为Dialog-->
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>


下边是源文件


package com.wind.lancherdemo;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity implements OnItemClickListener{
private GridView mGridView;
private Context mContext;
private PackageManager mPackageManager;
private List<ResolveInfo> mAllApps;
private List<ResolveInfo> mShowApps = new ArrayList<ResolveInfo>();
private static final String[] mShowAppPkgNames = {"com.android.contacts","com.android.mms","com.android.browser"};  //这个地方可以添加我们需要过滤的apk包名
  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

setupViews();
}






private void setupViews() {
mContext = MainActivity.this;
mPackageManager = getPackageManager();
mGridView = (GridView)findViewById(R.id.allapps);
bindAllApps();

mGridView.setAdapter(new GridItemAdapter(mContext, mShowApps));  //这个地方时设置GridView的适配器,不懂的可以去网上搜下具体教程
mGridView.setNumColumns(3);
mGridView.setOnItemClickListener(this);
}






private void bindAllApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);//这个地方就是我们根据我们安装的所有apk过滤出我们想要的apk,这样做的目的是你删除了某个我们需要的应用,我们的程序依然正常
for (ResolveInfo app_item : mAllApps) {
String pkg = app_item.activityInfo.packageName;
for (int i = 0; i < mShowAppPkgNames.length; i++) {
if(mShowAppPkgNames[i].equals(pkg)) {
mShowApps.add(app_item);
}
}
}
Collections.sort(mShowApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
}





//这个是根据我们的点击进入到具体的应用
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ResolveInfo res = mShowApps.get(position);
String pkg = res.activityInfo.packageName;
String cls = res.activityInfo.name;

ComponentName component = new ComponentName(pkg, cls);

Intent i = new Intent();
i.setComponent(component);
startActivity(i);
}
//这个地方是我们重写我们的GridView适配器
private class GridItemAdapter extends BaseAdapter{
private Context context;
private List<ResolveInfo> resInfo;



public GridItemAdapter(Context context, List<ResolveInfo> resInfo) {
this.context = context;
this.resInfo = resInfo;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return resInfo.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return resInfo.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

//这个地方用ViewHolder的目的是不用我们每次重构我们的convertView 及寻找ImageView和TextView,可以提高app运行速度
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;

if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.application_layout, null);
holder = new ViewHolder();
holder.mAppIcon = (ImageView)convertView.findViewById(R.id.app_icon);
holder.mAppTitle = (TextView)convertView.findViewById(R.id.app_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}

ResolveInfo res = resInfo.get(position);
holder.mAppIcon.setImageDrawable(res.loadIcon(mPackageManager));
holder.mAppTitle.setText(res.loadLabel(mPackageManager).toString());

return convertView;
}

private class ViewHolder {
ImageView mAppIcon;
TextView mAppTitle;
}
}


}



android中怎么把自己需要的app启动图标集中到一个弹出框中

上一篇:【缓存】MESI 缓存一致性协议 Gif 动画


下一篇:【docker】小技巧:在宿主机器上直接查看docker容器的进程