private OnItemClickListener listener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { } }
private OnItemClickListener clickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { ResolveInfo info = apps.get(i); //该应用的包名 String pkg = info.activityInfo.packageName; //应用的主activity类 String cls = info.activityInfo.name; ComponentName componet = new ComponentName(pkg, cls); Intent intent = new Intent(); intent.setComponent(componet); startActivity(intent); } };
代码中的中文注释可能会引起Gradle报错
解决办法:http://blog.csdn.net/sljjyy/article/details/11976099
现在整个类代码如下:
package com.sljjyy.sao.launcher; import android.content.ComponentName; import android.content.Intent; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.AdapterView.OnItemClickListener; import java.util.List; public class MainActivity extends Activity { GridView appsGrid; private List<ResolveInfo> apps; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loadApps(); appsGrid = (GridView) findViewById(R.id.apps_list); appsGrid.setAdapter(new AppsAdapter()); appsGrid.setOnItemClickListener(clickListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private OnItemClickListener clickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { ResolveInfo info = apps.get(i); //该应用的包名 String pkg = info.activityInfo.packageName; //应用的主activity类 String cls = info.activityInfo.name; ComponentName componet = new ComponentName(pkg, cls); Intent intent = new Intent(); intent.setComponent(componet); startActivity(intent); } }; private void loadApps() { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); new ImageView(MainActivity.this); apps = getPackageManager().queryIntentActivities(mainIntent, 0); } public class AppsAdapter extends BaseAdapter { public AppsAdapter(){ } @Override public int getCount() { return apps.size(); } @Override public Object getItem(int i) { return apps.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ImageView iv; if(view == null){ iv = new ImageView(MainActivity.this); iv.setScaleType(ImageView.ScaleType.FIT_CENTER); iv.setLayoutParams(new GridView.LayoutParams(50, 50)); } else { iv = (ImageView) view; } ResolveInfo info = apps.get(i); iv.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); return iv; } } }
android手把手教你开发launcher(一)(AndroidStudio版)
android手把手教你开发launcher(二)——列出安装的应用程序
android手把手教你开发launcher(三)——启动安装的应用程序
android手把手教你开发launcher(四)——显示widget
android手把手教你开发launcher(五)——设置壁纸
转自:http://www.bangchui.org/read.php?tid=12208