Android實現軟體列表的點擊啟動另外一個程式功能【附demo源碼下載】_Android

來源:互聯網
上載者:User

本文執行個體講述了Android實現軟體列表的點擊啟動另外一個程式功能。分享給大家供大家參考,具體如下:

目前面世的許多軟體中有這麼一個功能:裝置中安裝了哪些軟體,他們會以一個軟體列表清單的形式向使用者展示出來。

今天我們就來實現這一功能:

運行環境: motorola defy+ 系統2.3.6

主要 API : PackageInfo,PackageManager,LayoutInflater,ApplicationInfo

PackageManger類,它的主要職責是管理應用程式包。 通過它,我們就可以擷取應用程式資訊

通過PackageManager擷取應用程式相關資訊,再通過listView顯示出相應資訊。

直接上主要代碼了

public class AppListView extends LinearLayout implements OnItemClickListener{  private final static String TAG = "AppListView";  private ListView mListView;  private TextView mTvTitle;  private List<AppInfo> mAppList;  private Context mContext;  private LayoutInflater mInflater;  private PackageManager mPacManager;  public AppListView(Context context, AttributeSet attrs) {    super(context, attrs);    init(context);  }  public AppListView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);    init(context);  }  public AppListView(Context context)  {    super(context);    init(context);  }  private void init(Context c)  {    mContext = c;    mInflater = (LayoutInflater)c.getSystemService         (Context.LAYOUT_INFLATER_SERVICE);    this.addView(mInflater.inflate(R.layout.activity_main,null,false));    mListView = (ListView)this.findViewById(R.id.listView);    mTvTitle  = (TextView)this.findViewById(R.id.title);    loadAppData();    mListView.setAdapter(new MyAdapter(c));    mListView.setOnItemClickListener(this);    Log.d(TAG, "一共"+mAppList.size());  }  // 載入應用軟體資料 軟體名稱,包名,對應的表徵圖等等  private void loadAppData()  {    if(mAppList != null){      mAppList.clear();    }else{      mAppList = new ArrayList<AppInfo>();    }    mPacManager = mContext.getPackageManager();    List<PackageInfo> packages = mPacManager.getInstalledPackages(0);    for(int i=0; i<packages.size(); i++){      PackageInfo pi = packages.get(i);      AppInfo ai = new AppInfo();      ai.packageName = pi.packageName;      ai.appName = pi.applicationInfo.loadLabel(mPacManager).toString();      ai.appIcon = pi.applicationInfo.loadIcon(mPacManager);      mAppList.add(ai);    }    mTvTitle.setText("本機所安裝的軟體總數:"+packages.size()+"個");  }  // 為ListView自訂配接器  class MyAdapter extends BaseAdapter  {    public MyAdapter(Context c)    {      mContext = c;    }    @Override    public int getCount() {      return mAppList == null?0:mAppList.size();    }    @Override    public Object getItem(int arg0) {      return mAppList == null?null:mAppList.get(arg0);    }    @Override    public long getItemId(int arg0) {      return arg0;    }    @Override    public View getView(int arg0, View arg1, ViewGroup arg2) {      View view;      if(arg1 == null){        view = mInflater.inflate(R.layout.app_list_item, null);      }else{        view = arg1;      }      AppInfo ai = (AppInfo)getItem(arg0);      ImageView appIcon  = (ImageView)view.findViewById(R.id.appIcon);      TextView appName  = (TextView)view.findViewById(R.id.appName);      TextView appPackage = (TextView)view.findViewById(R.id.appPackage);      appIcon.setImageDrawable(ai.appIcon);      appName.setText(ai.appName);      appPackage.setText(ai.packageName);      return view;    }  }  // 處理ListView的item的點擊操作,我這裡是啟動該應用程式  @Override  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {    Toast.makeText(mContext, arg2+"", Toast.LENGTH_SHORT).show();    // 擷取本次item的包名    String packName = mAppList.get(arg2).packageName;    // 啟動此程式    Intent intent = mPacManager.getLaunchIntentForPackage(packName);    mContext.startActivity(intent);  }  // 軟體載體  public class AppInfo {    // 軟體名稱    public String appName="";    // 軟體包名    public String packageName="";    // 軟體表徵圖    public Drawable appIcon=null;  }}

public abstract Intent getLaunchIntentForPackage (String packageName)

這個方法通過包名返回一個Intent , 然後通過StartActivity(Intent)啟動應用程式

完整執行個體代碼點擊此處本站下載

下面是程式啟動並執行效果圖:

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android控制項用法總結》、《Android視圖View技巧總結》、《Android檔案操作技巧匯總》、《Android操作SQLite資料庫技巧總結》、《Android操作json格式資料技巧總結》、《Android資料庫操作技巧總結》、《Android編程之activity操作技巧總結》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》及《Android資源操作技巧匯總》

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.