今天学习下 写一个 非常非常简陋的 launcher ,下面内容:
一、大概内容
1.该过程中使用 GridView 存放每一个 app
2.每个 app 包括 图标和 app名称,点击后 打开该 app
3.因此该过程中使用到 自定义 的 layout 和 adapter
4.其中主要的是获取 app 信息
二、MainActivity 内容
public class MainActivity extends AppCompatActivity {
private GridView gridView = null;
private String TAG = "MainActivity";
private List mApps = null;
private AppsAdapter appsAdapter = null;
private WindowManager wm = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView)findViewById(R.id.apps_list);
//获取 app 信息,信息放在 list中
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
//得到 手机屏幕的大小,在改代码中 没有使用
wm = this.getWindowManager();
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width = (int) (dm.widthPixels / dm.density);
int height =(int) (dm.heightPixels / dm.density);
Log.e(TAG,"width: " + String.valueOf(width) + "hight: " + String.valueOf(height));
//自定义的 adapter
appsAdapter = new AppsAdapter(mApps,this,R.layout.cust_gridview);
gridView.setAdapter(appsAdapter);
//设置 每个app的点击事件
gridView.setOnItemClickListener(listener);
}
private AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
ResolveInfo info = mApps.get(position);
//得到app的包名 和 名称
String pkg = info.activityInfo.packageName;
String cls = info.activityInfo.name;
ComponentName componet = new ComponentName(pkg, cls);
//启动app
Intent i = new Intent();
i.setComponent(componet);
startActivity(i);
}
};
}
三、 adapter 的内容
public class AppsAdapter extends BaseAdapter{
private List mApps = null;
private Context context = null;
private int resourceId = 0;
public AppsAdapter(List mApps,Context context,int resourceId) {
this.mApps = mApps;
this.context = context;
this.resourceId = resourceId;
}
@Override
public int getCount() {
return mApps.size();
}
@Override
public Object getItem(int position) {
return mApps.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(resourceId,parent,false);
ImageView i = (ImageView)v.findViewById(R.id.image);
TextView textView = (TextView)v.findViewById(R.id.text);
ResolveInfo info = mApps.get(position);
//设置 APP image 和 名称
i.setImageDrawable(info.activityInfo.loadIcon(context.getPackageManager()));
textView.setText(context.getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo));
return v;
}
}
四、对应的 自定义 布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
LinearLayout>
五、activity_main 内容
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<GridView
android:id="@+id/apps_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
GridView>
LinearLayout>
六、设置全屏 styles.xml
-- Base application theme. -->
七、作为 launcher 必须要设置的
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
八、参考
1.https://blog.csdn.net/a1510624979/article/details/53115298
2.https://www.jb51.net/article/38823.htm
3.https://blog.csdn.net/flyyhz/article/details/78475602
4.https://www.cnblogs.com/playing/archive/2011/04/13/2014705.html
以上为学习的内容