In android, how does one set the desired app startup icon to a pop-up box?

Source: Internet
Author: User

In android, how does one set the desired app startup icon to a pop-up box?

First look



This is the effect of clicking on our own apk.


Below is the layout File <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + YWN0aXZpdHlfbWFpbi54bWzW97K8vtbOxLz + PGJyPgo8L3A + Signature = "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"/>




I use a GridView as a container


Below is the layout file for a single 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"/>

The following is AndroidManifest. xml, which is different from the common apk.



Package = "com. wind. lancherdemo"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">


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">












Below is the source file


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 "}; // you can add the apk package name to be filtered.
@ 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); // you can search for a specific tutorial on the Internet if you do not know how to set the GridView adapter.
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); // This is where we filter out the apk we want based on all the apk we have installed. This is to delete an application we need, our program is still normal
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 ));
}





// Click here to enter the specific application.
@ 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 );
}
// Here we rewrite our GridView Adapter
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 is used in this place to increase the app Running Speed instead of refactoring our convertView and searching for ImageView and TextView every time.
@ 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;
}
}


}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.