效果是这样的:
就是获取此机上已经安装的应用的图片和应用名字。
很简单,直接上代码:
AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.pullupapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity:
public class MainActivity extends Activity {
private GridView mGridView;
private List<ResolveInfo> mApps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hideBottomUIMenu();
loadApps();
mGridView = (GridView) findViewById(R.id.apps_list);
mGridView.setAdapter(new AppAdapter(this, mApps));
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ResolveInfo info = mApps.get(position);
// 应用的包名
String pkg = info.activityInfo.packageName;
// 应用的主Activity
String cls = info.activityInfo.name;
ComponentName componentName = new ComponentName(pkg, cls);
Intent intent = new Intent();
intent.setComponent(componentName);
startActivity(intent);
}
});
}
/**
* 加载app
*/
private void loadApps() {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(intent, 0);
}
/**
* 隐藏虚拟按键,并且全屏
*/
protected void hideBottomUIMenu() {
//隐藏虚拟按键,并且全屏
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}
}
AppAdapter:
public class AppAdapter extends BaseAdapter {
List<ResolveInfo> mApps;
Context context;
public AppAdapter(Context context, List<ResolveInfo> apps) {
super();
this.context = context;
this.mApps = apps;
}
@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) {
ResolveInfo info = mApps.get(position);
PackageManager manager = context.getPackageManager();
ViewHold viewHold = null;
if (convertView == null){
convertView = View.inflate(context,R.layout.item,null);
viewHold = new ViewHold();
viewHold.tv_name = convertView.findViewById(R.id.textView);
viewHold.iv_icon = convertView.findViewById(R.id.imageView);
convertView.setTag(viewHold);
}else {
viewHold = (ViewHold)convertView.getTag();
}
viewHold.iv_icon.setImageDrawable(info.activityInfo.loadIcon(context.getPackageManager()));
viewHold.tv_name.setText(info.loadLabel(manager).toString());
return convertView;
}
private static class ViewHold {
private TextView tv_name;
private ImageView iv_icon;
}
}
下边是布局文件:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<GridView android:layout_width="match_parent"
android:id="@+id/apps_list"
android:numColumns="4"
android:layout_height="wrap_content">
</GridView>
</RelativeLayout>
item.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="70dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
就这样。
写这个的主要原因是要写一个应用来拉起别的应用,使得自己想要的应用能够一直活着,所以执行拉起操作的这个应用需要一直活着,那么就自己写一个launcher应用来实现这个效果吧。