Android實(shí)現(xiàn)軟件列表的點(diǎn)擊啟動(dòng)另外一個(gè)程序功能【附demo源碼下載】
本文實(shí)例講述了Android實(shí)現(xiàn)軟件列表的點(diǎn)擊啟動(dòng)另外一個(gè)程序功能。分享給大家供大家參考,具體如下:
目前面世的許多軟件中有這么一個(gè)功能:設(shè)備中安裝了哪些軟件,他們會(huì)以一個(gè)軟件列表清單的形式向用戶展示出來。
今天我們就來實(shí)現(xiàn)這一功能:
運(yùn)行環(huán)境: motorola defy+ 系統(tǒng)2.3.6
主要 API : PackageInfo,PackageManager,LayoutInflater,ApplicationInfo
PackageManger類,它的主要職責(zé)是管理應(yīng)用程序包。 通過它,我們就可以獲取應(yīng)用程序信息
通過PackageManager獲取應(yīng)用程序相關(guān)信息,再通過listView顯示出相應(yīng)信息。
直接上主要代碼了
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());
}
// 加載應(yīng)用軟件數(shù)據(jù) 軟件名稱,包名,對應(yīng)的圖標(biāo)等等
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("本機(jī)所安裝的軟件總數(shù):"+packages.size()+"個(gè)");
}
// 為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的點(diǎn)擊操作,我這里是啟動(dòng)該應(yīng)用程序
@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;
// 啟動(dòng)此程序
Intent intent = mPacManager.getLaunchIntentForPackage(packName);
mContext.startActivity(intent);
}
// 軟件載體
public class AppInfo {
// 軟件名稱
public String appName="";
// 軟件包名
public String packageName="";
// 軟件圖標(biāo)
public Drawable appIcon=null;
}
}
public abstract Intent getLaunchIntentForPackage (String packageName)
這個(gè)方法通過包名返回一個(gè)Intent , 然后通過StartActivity(Intent)啟動(dòng)應(yīng)用程序
完整實(shí)例代碼點(diǎn)擊此處本站下載。
下面是程序運(yùn)行的效果圖:

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android編程實(shí)現(xiàn)應(yīng)用程序開機(jī)自啟動(dòng)的方法
- Android編程實(shí)現(xiàn)在一個(gè)程序中啟動(dòng)另一個(gè)程序的方法
- Android程序啟動(dòng)時(shí)出現(xiàn)黑屏問題的解決方法
- Android使用Intent啟動(dòng)其他非系統(tǒng)應(yīng)用程序的方法
- Android中使用am命令實(shí)現(xiàn)在命令行啟動(dòng)程序詳解
- Android中通過外部程序啟動(dòng)App的三種方法
- Android adb.exe程序啟動(dòng)不起來 具體解決方法
- Android開機(jī)自啟動(dòng)程序詳解
- Android判斷程序是否第一次啟動(dòng)
相關(guān)文章
Android開發(fā)雙向滑動(dòng)選擇器范圍SeekBar實(shí)現(xiàn)
這篇文章主要為大家介紹了Android開發(fā)雙向滑動(dòng)范圍選擇器SeekBar實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
退出Android程序時(shí)清除所有activity的實(shí)現(xiàn)方法
這篇文章主要介紹了退出Android程序時(shí)清除所有activity的實(shí)現(xiàn)方法,詳細(xì)分析了Android退出時(shí)清除activity的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04
Android基于hover組件實(shí)現(xiàn)監(jiān)控鼠標(biāo)移動(dòng)事件的方法
這篇文章主要介紹了Android基于hover組件實(shí)現(xiàn)監(jiān)控鼠標(biāo)移動(dòng)事件的方法,結(jié)合實(shí)例形式分析了hover組件監(jiān)控鼠標(biāo)光標(biāo)在view上變化的操作技巧,需要的朋友可以參考下2017-02-02
Android編程中Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)功能詳解
這篇文章主要介紹了Android編程中Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)功能,結(jié)合實(shí)例形式分析了Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)功能的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
Android自定義View實(shí)現(xiàn)彈性小球效果
前段時(shí)間看到一個(gè)功能,是一個(gè)小球沿著固定軌跡彈動(dòng)的效果,那么這篇文章小編給大家分享在Android中如何自定義View來實(shí)現(xiàn)彈性小球的效果,有需要的可以參考借鑒。2016-09-09
Android 限制edittext 整數(shù)和小數(shù)位數(shù) 過濾器(詳解)
下面小編就為大家?guī)硪黄狝ndroid 限制edittext 整數(shù)和小數(shù)位數(shù) 過濾器(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

