Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android中怎麼把自己需要的app啟動圖標集中到一個彈出框中

android中怎麼把自己需要的app啟動圖標集中到一個彈出框中

編輯:關於Android編程

先看效果圖

\


這個是我們自己的apk點擊之後的效果


下邊是布局文件<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+YWN0aXZpdHlfbWFpbi54bWzW97K8vtbOxLz+PGJyPgo8L3A+CjxwPjxMaW5lYXJMYXlvdXQgeG1sbnM6YW5kcm9pZD0="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"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="15dp"
android:text="@string/app_name"/>


android:id="@+id/allapps"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>




我用一個GridView 做容器


下邊是單個item布局文件

application_layout.xml



android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>

android:id="@+id/app_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"/>

android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="center"/>

下邊是AndroidManifest.xml,這個裡邊有幾個和普通apk不同的地方



package="com.wind.lancherdemo"
android:versionCode="1"
android:versionName="1.0" >


android:minSdkVersion="17"
android:targetSdkVersion="17" />


android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:name="com.wind.lancherdemo.MainActivity"
android:theme="@android:style/Theme.Dialog"
android:label="@string/app_name" >












下邊是源文件


package com.wind.lancherdemo;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity implements OnItemClickListener{
private GridView mGridView;
private Context mContext;
private PackageManager mPackageManager;
private List mAllApps;
private List mShowApps = new ArrayList();
private static final String[] mShowAppPkgNames = {"com.android.contacts","com.android.mms","com.android.browser"}; //這個地方可以添加我們需要過濾的apk包名
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

setupViews();
}






private void setupViews() {
mContext = MainActivity.this;
mPackageManager = getPackageManager();
mGridView = (GridView)findViewById(R.id.allapps);
bindAllApps();

mGridView.setAdapter(new GridItemAdapter(mContext, mShowApps)); //這個地方時設置GridView的適配器,不懂的可以去網上搜下具體教程
mGridView.setNumColumns(3);
mGridView.setOnItemClickListener(this);
}






private void bindAllApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);//這個地方就是我們根據我們安裝的所有apk過濾出我們想要的apk,這樣做的目的是你刪除了某個我們需要的應用,我們的程序依然正常
for (ResolveInfo app_item : mAllApps) {
String pkg = app_item.activityInfo.packageName;
for (int i = 0; i < mShowAppPkgNames.length; i++) {
if(mShowAppPkgNames[i].equals(pkg)) {
mShowApps.add(app_item);
}
}
}
Collections.sort(mShowApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
}





//這個是根據我們的點擊進入到具體的應用
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
ResolveInfo res = mShowApps.get(position);
String pkg = res.activityInfo.packageName;
String cls = res.activityInfo.name;

ComponentName component = new ComponentName(pkg, cls);

Intent i = new Intent();
i.setComponent(component);
startActivity(i);
}
//這個地方是我們重寫我們的GridView適配器
private class GridItemAdapter extends BaseAdapter{
private Context context;
private List resInfo;



public GridItemAdapter(Context context, List resInfo) {
this.context = context;
this.resInfo = resInfo;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return resInfo.size();
}


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


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

//這個地方用ViewHolder的目的是不用我們每次重構我們的convertView 及尋找ImageView和TextView,可以提高app運行速度
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;

if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.application_layout, null);
holder = new ViewHolder();
holder.mAppIcon = (ImageView)convertView.findViewById(R.id.app_icon);
holder.mAppTitle = (TextView)convertView.findViewById(R.id.app_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}

ResolveInfo res = resInfo.get(position);
holder.mAppIcon.setImageDrawable(res.loadIcon(mPackageManager));
holder.mAppTitle.setText(res.loadLabel(mPackageManager).toString());

return convertView;
}

private class ViewHolder {
ImageView mAppIcon;
TextView mAppTitle;
}
}


}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved