概述
android中怎么把自己需要的app启动图标集中到一个弹出框中
先看效果图
这个是我们自己的apk点击之后的效果
下边是布局文件
activity_main.xml主布局文件
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;
}
}
}
最后
以上就是独特皮卡丘为你收集整理的android app自动弹出提示,android中如何把自己需要的app启动图标集中到一个弹出框中...的全部内容,希望文章能够帮你解决android app自动弹出提示,android中如何把自己需要的app启动图标集中到一个弹出框中...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复