欢迎转载,转载请标明出处:www.jianshu.com/u/a5ad093cffe8
packagecom.example.gray_dog3.applist;
importandroid.content.pm.ApplicationInfo;
importandroid.content.pm.PackageInfo;
importandroid.content.pm.PackageManager;
importandroid.util.Log;
importjava.util.ArrayList;
importjava.util.List;
/**
* 扫描本地安装的应用,工具类
*/
public class ApkTool {
staticStringTAG="ApkTool";
public staticListmLocalInstallApps=null;
public staticListscanLocalInstallAppList(PackageManager packageManager) {
List myAppInfos =newArrayList();
try{
List packageInfos = packageManager.getInstalledPackages(0);
for(inti =0;i < packageInfos.size();i++) {
PackageInfo packageInfo = packageInfos.get(i);
//过滤掉系统app
if((ApplicationInfo.FLAG_SYSTEM& packageInfo.applicationInfo.flags) !=0) {
continue;
}
MyAppInfo myAppInfo =newMyAppInfo();
myAppInfo.setPackageName(packageInfo.packageName);
myAppInfo.setAppName(packageInfo.applicationInfo.loadLabel(packageManager).toString());
if(packageInfo.applicationInfo.loadIcon(packageManager) ==null) {
continue;
}
myAppInfo.setImage(packageInfo.applicationInfo.loadIcon(packageManager));
myAppInfos.add(myAppInfo);
}
}catch(Exception e) {
Log.e(TAG,"===============获取应用包信息失败");
}
returnmyAppInfos;
}
}
packagecom.example.gray_dog3.applist;
importandroid.graphics.drawable.Drawable;
public class MyAppInfo {
privateDrawableimage;
privateStringpackageName;
privateStringAppName;
publicMyAppInfo(Drawable image,String appName) {
this.image= image;
this.packageName= appName;
}
publicMyAppInfo() {
}
publicDrawablegetImage() {
returnimage;
}
public voidsetImage(Drawable image) {
this.image= image;
}
publicStringgetPackageName() {
returnpackageName;
}
public voidsetPackageName(String appName) {
this.packageName= appName;
}
// @Override
// public String toString() {
// return "MyAppInfo{" +
// "image=" + image +
// ", appName='" + appName + '\'' +
publicStringgetAppName() {
returnAppName;
}
public voidsetAppName(String appName) {
AppName= appName;
}
// '}';
// }
}
3.使用方法(调用)
packagecom.example.gray_dog3.applist;
importandroid.content.Intent;
importandroid.net.Uri;
importandroid.os.Handler;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.AdapterView;
importandroid.widget.BaseAdapter;
importandroid.widget.ImageView;
importandroid.widget.ListView;
importandroid.widget.TextView;
importandroid.widget.Toast;
importcom.google.android.gms.appindexing.Action;
importcom.google.android.gms.appindexing.AppIndex;
importcom.google.android.gms.appindexing.Thing;
importcom.google.android.gms.common.api.GoogleApiClient;
importjava.util.ArrayList;
importjava.util.List;
public classMainActivityextendsAppCompatActivityimplementsAdapterView.OnItemClickListener {
privateListViewlv_app_list;
privateAppAdaptermAppAdapter;
publicHandlermHandler=newHandler();
privateListappInfos;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
privateGoogleApiClientclient;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_app_list= (ListView) findViewById(R.id.lv_app_list);
mAppAdapter=newAppAdapter();
lv_app_list.setAdapter(mAppAdapter);
lv_app_list.setOnItemClickListener(this);
initAppList();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client=newGoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
private voidinitAppList() {
newThread() {
@Override
public voidrun() {
super.run();
//扫描得到APP列表
appInfos= ApkTool.scanLocalInstallAppList(MainActivity.this.getPackageManager());
mHandler.post(newRunnable() {
@Override
public voidrun() {
mAppAdapter.setData(appInfos);
Log.e("Mainactivity",appInfos.get(0).toString());
Toast.makeText(MainActivity.this,appInfos.get(0).toString(),Toast.LENGTH_SHORT).show();
}
});
}
}.start();
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
publicActiongetIndexApiAction() {
Thing object =newThing.Builder()
.setName("Main Page")//TODO: Define a title for the content shown.
//TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return newAction.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public voidonStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client,getIndexApiAction());
}
@Override
public voidonStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client,getIndexApiAction());
client.disconnect();
}
@Override
public voidonItemClick(AdapterView parent,View view, intposition, longid) {
Toast.makeText(this,appInfos.get(position).getPackageName(),Toast.LENGTH_SHORT).show();
runstallApp(appInfos.get(position).getPackageName());
}
classAppAdapterextendsBaseAdapter {
ListmyAppInfos=newArrayList();
public voidsetData(List myAppInfos) {
this.myAppInfos= myAppInfos;
notifyDataSetChanged();
}
publicListgetData() {
returnmyAppInfos;
}
@Override
public intgetCount() {
if(myAppInfos!=null&&myAppInfos.size() >0) {
returnmyAppInfos.size();
}
return0;
}
@Override
publicObjectgetItem(intposition) {
if(myAppInfos!=null&&myAppInfos.size() >0) {
returnmyAppInfos.get(position);
}
return null;
}
@Override
public longgetItemId(intposition) {
return0;
}
@Override
publicViewgetView(intposition,View convertView,ViewGroup parent) {
ViewHolder mViewHolder;
MyAppInfo myAppInfo =myAppInfos.get(position);
if(convertView ==null) {
mViewHolder =newViewHolder();
convertView = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_app_info, null);
mViewHolder.iv_app_icon= (ImageView) convertView.findViewById(R.id.iv_app_icon);
mViewHolder.tx_app_name= (TextView) convertView.findViewById(R.id.tv_app_name);
convertView.setTag(mViewHolder);
}else{
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.iv_app_icon.setImageDrawable(myAppInfo.getImage());
mViewHolder.tx_app_name.setText(myAppInfo.getAppName());
returnconvertView;
}
classViewHolder {
ImageViewiv_app_icon;
TextViewtx_app_name;
}
}
/**
* 打开已安装的APP
*
*@parampackageName
*/
private voidrunstallApp(String packageName) {
// Uri packageURI = Uri.parse("package:" + packageName);
try{
Intent intent =this.getPackageManager().getLaunchIntentForPackage(packageName);
this.startActivity(intent);
}catch(Exception e) {
Toast.makeText(this,"未发现对应APP",Toast.LENGTH_SHORT).show();
}
}
/**
* 卸载手机上的安装应用
*
*@parampackageName传入项目APP的包名
*/
private voiduninstallApp(String packageName) {
try{
Uri packageURI = Uri.parse("package:"+ packageName);
Intent uninstallIntent =newIntent(Intent.ACTION_DELETE,packageURI);
this.startActivity(uninstallIntent);
}catch(Exception E) {
Toast.makeText(this,"未发现对应APP",Toast.LENGTH_SHORT).show();
}
// setIntentAndFinish(true, true);
}
}
扫码关注公众号回复(app列表):获取demo下载链接
qrcode_for_gh_1c53482fb215_430(1).jpg感觉好的可以打赏:
网友评论