• 桌面应用基本创建流程


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <--主界面布局--> <GridView
    android:gravity="center_horizontal"
    android:stretchMode="columnWidth"
    android:columnWidth="100dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:layout_width="match_parent"
    android:id="@+id/apps_list"
    android:numColumns="3"
    android:layout_height="match_parent">
    </GridView>

    </LinearLayout>
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------
     <--清单文件中的配置-->
    <application
    android:name="com.way.launcher.SceneLauncherApplication"
    android:icon="@drawable/ic_launcher_home"
    android:label="@string/application_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <activity
    android:name="com.way.launcher.SceneLauncherActivity"
    android:configChanges="orientation|keyboardHidden"
    android:launchMode="singleTask"
    android:screenOrientation="nosensor" >
    <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>
    <activity
    android:name="com.way.launcher.SceneChooserActivity"
    android:configChanges="orientation|keyboardHidden"
    android:launchMode="singleTask"
    android:screenOrientation="nosensor" />
    </application>
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------
    public class MyHome extends Activity {
    private List<ResolveInfo> mApps;
    GridView mGrid;
    private AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    ResolveInfo info = mApps.get(position);
    //设置点击item监听
    String pkg = info.activityInfo.packageName;//该应用的包名
    String cls = info.activityInfo.name; //应用的主activity
    ComponentName componet = new ComponentName(pkg, cls);//设置跳转
    Intent i = new Intent();//发送意图
    i.setComponent(componet);
    startActivity(i);//启动被点击的activity
    }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    loadApps();
    setContentView(R.layout.activity_main);
    mGrid = (GridView) findViewById(R.id.apps_list);
    mGrid.setAdapter(new AppsAdapter());//由于adapter是内部类,所以直接将数据传入adapter里面了
    mGrid.setOnItemClickListener(listener);
    }
    private void loadApps() {//查询所有安装的应用
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
    }

    public class AppsAdapter extends BaseAdapter {
    public AppsAdapter() {}
    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
    imageView = new ImageView(MyHome.this);
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
    } else {
    imageView = (ImageView) convertView;
    }
    ResolveInfo info = mApps.get(position);
    imageView.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
    return imageView;
    }
    public final int getCount() {
    return mApps.size();
    }

    public final Object getItem(int position) {
    return mApps.get(position);
    }

    public final long getItemId(int position) {
    return position;
    }}

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {return false;}//屏蔽返回键
    return super.onKeyDown(keyCode, event);
    }
    }
  • 相关阅读:
    使用ClassLoader加载配置文件
    Io流和Properties集合的联合应用
    文件拷贝案例
    倒计时
    静态代码块
    数组的四种排序(冒泡排序,选择排序,插入排序,快速排序)
    通过map集合统计每个字符出现的次数
    随机输入几个数字,删除重复数字(但要保留一个),留下不重复的数字
    流程图学习-1-基础符号
    Java-List的对象的校验不起作用的解决方案
  • 原文地址:https://www.cnblogs.com/chenxuyuan/p/5084379.html
走看看 - 开发者的网上家园