android修改默认桌面

package com.lxl.lock;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class MainActivity extends Activity {  
    private GridView mGridView;  
    private List<ResolveInfo> mApps;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);  
        
        loadApps();  
        mGridView = (GridView) findViewById(R.id.gridView1);  
        mGridView.setAdapter(new AppAdapter(this, mApps));  
          
          
        mGridView.setOnItemClickListener(new 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);  
    }  
  
}  

//适配器
package com.lxl.lock;

import java.util.List;

import android.content.Context;
import android.content.pm.ResolveInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

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) {  
        ImageView iv;  
        if (convertView == null) {  
            iv = new ImageView(context);  
            iv.setScaleType(ScaleType.FIT_CENTER);  
            iv.setLayoutParams(new GridView.LayoutParams(50,50));  
        } else {  
            iv = (ImageView) convertView;  
        }  
        ResolveInfo info = mApps.get(position);  
        iv.setImageDrawable(info.activityInfo.loadIcon(context.getPackageManager()));  
        return iv;  
    }  
  
}  

mainfest文件。。

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <uses-permission android:name="android.permission.SET_WALLPAPER" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.lxl.lock.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

                <!-- 增加以下两行 -->
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

设置流程:

找到设置-》显示-》默认桌面-》选择自定义的那个


然后点击home,发现弹出的应该就是自定义的那个界面了。

你可能感兴趣的:(android修改默认桌面)