找到launcher上面的程序并进行名字的排序(中文跟英文)

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.android.settings;


import java.text.Collator;
import java.util.*;


import com.android.settings.applications.ApplicationsState.AppEntry;


import android.app.ListFragment;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;




public class NavigationPreference extends ListFragment {
    private static final String TAG = "NavigationPreference";
    private static final boolean DEBUG = false;
    private PackageManager pm;
    private LayoutInflater mInflater;
    private Comparator<ResolveInfo> comparator;
    private List<ResolveInfo> listRv;
    
    
    private class ApplicatonAdapter extends BaseAdapter
    {
   
    private ApplicatonAdapter()
    {
    listRv=findActivitiesForPackage(); //找到launcher上面的程序
   
    Collections.sort(listRv, new Comparator<ResolveInfo>() { //对apk的名字进行排序,包括中文英文
    private final Collator sCollator = Collator.getInstance();
@Override
public int compare(ResolveInfo lhs, ResolveInfo rhs) {
// TODO Auto-generated method stub
return sCollator.compare(lhs.loadLabel(pm), rhs.loadLabel(pm));
}
});
   
    mInflater=LayoutInflater.from(getActivity());
    };
   
    /**
         * Query the package manager for MAIN/LAUNCHER activities in the supplied package.
         */
        private  List<ResolveInfo> findActivitiesForPackage() {
            pm = getActivity().getPackageManager();


            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);


            final List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
            return apps != null ? apps : new ArrayList<ResolveInfo>();
        }


@Override
public int getCount() {
// TODO Auto-generated method stub
return listRv!=null?listRv.size():0;
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listRv.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
AppViewHolder holder;
if (convertView == null) {
               convertView = mInflater.inflate(R.layout.manage_applications_item, null);


               // Creates a ViewHolder and store references to the two children views
               // we want to bind data to.
               holder = new AppViewHolder();
               holder.appName = (TextView) convertView.findViewById(R.id.app_name);
               holder.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);
               convertView.setTag(holder);
           } else {
               // Get the ViewHolder back to get fast access to the TextView
               // and the ImageView.
               holder = (AppViewHolder) convertView.getTag();
           }
String lable=listRv.get(position).loadLabel(pm).toString();
holder.appName.setText(lable);
Drawable drawable=listRv.get(position).loadIcon(pm);
if(drawable!=null)
{
holder.appIcon.setImageDrawable(drawable);
}else
{
holder.appIcon.setImageResource(android.R.drawable.sym_def_app_icon);
}
return convertView;
}

class AppViewHolder 
{
TextView appName;
ImageView appIcon;
}
    }


    


    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ApplicatonAdapter adapter=new ApplicatonAdapter();
        setListAdapter(adapter);
    }




    @Override
    public void onResume() {
        super.onResume();
        getListView().requestFocus();
    }


    /**
     * Each listener needs to call {@link #updateLocale(Locale)} to actually change the locale.
     *
     * We don't call {@link #updateLocale(Locale)} automatically, as it halt the system for
     * a moment and some callers won't want it.
     */
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
       
    Settings.Secure.putString(getActivity().getContentResolver(),
Settings.Secure.DEFALUT_PACKAGENAME,listRv.get(position).activityInfo.applicationInfo.packageName);
    Settings.Secure.putString(getActivity().getContentResolver(),
Settings.Secure.DEFALUT_LABLE,listRv.get(position).loadLabel(pm).toString());
         getActivity().onBackPressed();
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值