Android Custom Application drawer Launcher

Source: Internet
Author: User

Android Custom Application drawer Launcher

You can sort the application by application name and installation time, and view the specific project code on my GitHub. The link is shown at the end of the article ,:

Core code:

Package com. zms. launcher; import java. util. arrayList; import java. util. hashMap; import java. util. list; import android. app. activity; import android. app. alertDialog; import android. content. activityNotFoundException; import android. content. broadcastReceiver; import android. content. context; import android. content. dialogInterface; import android. content. intent; import android. content. intentFilter; import android. content. pm. packageManager; import android. content. pm. packageManager. nameNotFoundException; import android. content. pm. resolveInfo; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. pixelFormat; import android. graphics. drawable. drawable; import android.net. uri; import android. OS. bundle; import android. util. log; import android. view. contextMenu; import android. view. contextMenu. contextMenuInfo; import android. view. keyEvent; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. adapterContextMenuInfo; import android. widget. adapterView. onItemClickListener; import android. widget. editText; import android. widget. gridView; import android. widget. toast; public class Main extends Activity {static DbOperation db = null; static List appInfo = null; PackageManager packageManager = null; int orderFlag = 0; // indicates the sorting method, 0-default sorting, 1-name sorting, 2-time sorting static boolean isEqual = false; List
  
   
Apps = null; List
   
    
Limit 2 = null; private static final String TAG = ScrollLayoutTest; private ScrollLayout mScrollLayout; private static final float APP_PAGE_SIZE = 36366f; private Context mContext; int upCount; @ Override protected void onCreate (Bundle release) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); mContext = this; setContentView (R. layout. main); mScrollLayout = (ScrollLayout) findViewById (R. id. scrollLayoutTest); initViews () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// TODO Auto-generated method stub menu. add (0, 1, 1, ● [current] by default); menu. add (0, 2, 1, ○ name sorting); menu. add (0, 3, 1, ○ time sorting); menu. add (0, 4, 1, about applications); menu. add (0, 5, 1, update desktop); return super. onCreateOptionsMenu (menu) ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case 1: // default sorting [current] break; case 2: // name sorting Intent intent1 = new Intent (this, bytes byname. class); startActivity (intent1); break; case 3: // sort Intent intent2 = new Intent (this, bytes bytime. class); startActivity (intent2); break; case 4: // About Intent intent3 = new Intent (this, About. class); startActivity (intent3); break; case 5: // update the desktop onCreate (null); break; default:} return super. onOptionsItemSelected (item);} public void initViews () {packageManager = getPackageManager (); final Intent mainIntent = new Intent (Intent. ACTION_MAIN, null); mainIntent. addCategory (Intent. CATEGORY_LAUNCHER); List
    
     
> Items = new ArrayList
     
      
> (); // Get all apps = packageManager. queryIntentActivities (mainIntent, 0); for (ResolveInfo pi: apps) {HashMap
      
        Map = new HashMap
       
         (); PackageManager manager = this. getPackageManager (); map. put (appIcon, pi. loadIcon (packageManager); map. put (appName, pi. loadLabel (packageManager ). toString (); map. put (appPack, pi. activityInfo. packageName); try {map. put (appTime, Long. toString (manager. getPackageInfo (pi. activityInfo. packageName, 0 ). firstInstallTime);} catch (NameNotFoundException e) {e. printStackTrace ();} items. add (map);} // ToDB db = new DbOperation (this); long countlong = db. getCount (); // obtain the number of records in the database. int countint = (int) countlong; upCount = items. size (); // ++ + upCount if (countint = items. size () {isEqual = true; // if the number of records matches the current items. if the size () is equal, no application information is inserted. Only the insertion time is for (int m = 0; m <items. size (); m ++) {// insert time: String appPack2 = (String) items. get (m ). get (appPack); try {PackageManager manager = this. getPackageManager (); String appTime2 = Long. toString (manager. getPackageInfo (appPack2, 0 ). firstInstallTime); db. insertTime (appTime2, appPack2);} catch (NameNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} else {// If not, the table data is cleared first, and then the application information is inserted, and the insertion time is db. deleteTable (); for (int I = 0; I <items. size (); I ++) {String appName = (String) items. get (I ). get (appName); String appPack = (String) items. get (I ). get (appPack); String appTime = (String) items. get (I ). get (Time); App appInfo = new App (appName, appPack, appTime); DbOperation db = new DbOperation (this); db. insertApp (appInfo); // insert time: String appPack2 = (String) items. get (I ). get (appPack); try {PackageManager manager = this. getPackageManager (); String appTime2 = Long. toString (manager. getPackageInfo (appPack2, 0 ). firstInstallTime); db. insertTime (appTime2, appPack2);} catch (NameNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}// read database information db = new DbOperation (this); if (orderFlag = 0) {appInfo = db. query (appTable);} else if (orderFlag = 1) {appInfo = db. queryByName (appTable);} else if (orderFlag = 2) {appInfo = db. queryByTime (appTable);} final int PageCount = (int) Math. ceil (apps. size ()/APP_PAGE_SIZE); Log. e (TAG, size: + apps. size () + page: + PageCount); for (int I = 0; I <PageCount; I ++) {GridView appPage = new GridView (this); this. registerForContextMenu (appPage); appPage. setAdapter (new AppAdapter (this, appInfo, I, apps, items); appPage. setNumColumns (6); appPage. setOnItemClickListener (listener); mScrollLayout. addView (appPage) ;}} public OnItemClickListener listener = new OnItemClickListener () {public void onItemClick (AdapterView
        Parent, View view, int position, long id) {GridView listView = (GridView) parent; App appInfo = (App) (listView. getItemAtPosition (position); String packName = appInfo. getPack (); Intent mainIntent = mContext. getPackageManager (). getLaunchIntentForPackage (packName); mainIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); try {mContext. startActivity (mainIntent);} catch (ActivityNotFoundException noFou Nd) {Toast. makeText (mContext, this application is not found !, Toast. LENGTH_SHORT ). show () ;}}; public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {menu. setHeaderTitle (operation); menu. add (0, 1, Menu. NONE, renamed); menu. add (0, 2, Menu. NONE, unmount);} public boolean onContextItemSelected (MenuItem item) {// obtain the information of the currently selected item AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item. getMenuInfo (); final int a = menuInfo. position; switc H (item. getItemId () {case 1: final EditText e = new EditText (this); new AlertDialog. builder (this ). setTitle (renamed )//. setIcon (R. drawable. logo2 ). setView (e ). setPositiveButton (OK, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int I) {int b1 = mScrollLayout. getCurScreen (); int num1 = b1 * 36 + a; String appName = appInfo. get (num1 ). getName (); System. ou T. println (appName); String str = e. getText (). toString (); System. out. println (str); db. changeName (str, appName); // update the onCreate (null) ;}} interface );}}). setNegativeButton (cancel, null ). show (); break; case 2: int b1 = mScrollLayout. getCurScreen (); int num1 = b1 * 36 + a; String appPack = appInfo. get (num1 ). getPack (); Uri uri = Uri. parse (package: + appPack); System. out. println (appPack); Intent intent = new Intent (Int Ent. ACTION_DELETE, uri); intent. setData (uri); // set the retrieved URI db. deleteApp (appPack); startActivity (intent); // break; default: // update + delay try {Thread. currentThread (). sleep (300); onCreate (null);} catch (InterruptedException e22) {e22.printStackTrace ();} onCreate (null); return super. onContextItemSelected (item);} return true ;}@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {// TODO Auto-generated method stub if (keyCode = KeyEvent. KEYCODE_BACK) {finish (); return true;} return super. onKeyDown (keyCode, event);} class AppIntentReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {onCreate (null) ;}} private void registerIntentReceivers () {IntentFilter filter = new IntentFilter (Intent. ACTION_PACKAGE_ADDED); filter. addAction (I Ntent. ACTION_PACKAGE_REMOVED); filter. addAction (Intent. ACTION_PACKAGE_CHANGED); filter. addDataScheme (package); registerReceiver (new AppIntentReceiver (), filter);} public static Bitmap drawableToBitmap (Drawable drawable) {// obtain the length and width of drawable int w = drawable. getIntrinsicWidth (); int h = drawable. getIntrinsicHeight (); // retrieves the drawable color format Bitmap. config config = drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. config. ARGB_8888: Bitmap. config. RGB_565; // create the corresponding bitmap Bitmap bitmap = Bitmap. createBitmap (w, h, config); // create the Canvas canvas Canvas = new Canvas (bitmap); drawable. setBounds (0, 0, w, h); // draw the drawable content into the canvas. draw (canvas); return bitmap;} @ Override protected void onDestroy () {// TODO Auto-generated method stub android. OS. process. killProcess (android. OS. process. myPid (); super. onDestroy (); // this. finish ();}}
       
      
     
    
   
  

 

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.