Java 使用片段显示用户安装的应用程序
在我正在创建的android应用程序中,我从扩展FragmentPagerAdapter的TabsPageAdapter类中获得了一个可滚动的选项卡。但是,在我的应用程序中,我试图将手机上所有用户安装的应用程序显示在选项卡的一部分中。然而,能够返回所有扩展碎片活动的类 我试着遵循这一点,但它不起作用 遗憾的是,经过大量研究,我发现我只能将片段放在不同的选项卡中,并且在任何时候都只能显示一个活动,而选项卡本身只能返回片段 我真的需要将所有用户安装的应用程序放在一个片段中,我想在这种情况下,但我不确定如何实现这一点…我有一个适配器类和所有东西 下面是我的InstalledAppActivity类,它扩展FragmentActivity以返回所有用户下载的应用程序Java 使用片段显示用户安装的应用程序,java,android,android-fragments,Java,Android,Android Fragments,在我正在创建的android应用程序中,我从扩展FragmentPagerAdapter的TabsPageAdapter类中获得了一个可滚动的选项卡。但是,在我的应用程序中,我试图将手机上所有用户安装的应用程序显示在选项卡的一部分中。然而,能够返回所有扩展碎片活动的类 我试着遵循这一点,但它不起作用 遗憾的是,经过大量研究,我发现我只能将片段放在不同的选项卡中,并且在任何时候都只能显示一个活动,而选项卡本身只能返回片段 我真的需要将所有用户安装的应用程序放在一个片段中,我想在这种情况下,但我不
package com.spicycurryman.getdisciplined10.app;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.javatechig.listapps.ApplicationAdapter;
import java.util.ArrayList;
import java.util.List;
public class InstalledAppActivity extends FragmentActivity {
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private ApplicationAdapter listadaptor = null;
ListView InstalledAppList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.installed_apps);
InstalledAppList = (ListView) findViewById(R.id.Installed_List);
packageManager = getPackageManager();
new LoadApplications().execute();
InstalledAppList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ApplicationInfo app = applist.get(i);
try {
Intent intent = packageManager
.getLaunchIntentForPackage(app.packageName);
if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(InstalledAppActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(InstalledAppActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
packageManager = getPackageManager();
new LoadApplications().execute();
return inflater.inflate(R.layout.installed_apps, container, false);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.block, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
boolean result = true;
switch (item.getItemId()) {
case R.id.main_text: {
displayAboutDialog();
break;
}
default: {
result = super.onOptionsItemSelected(item);
break;
}
}
return result;
}
private void displayAboutDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage(getString(R.string.slogan));
builder.setPositiveButton("Know More", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://javatechig.com"));
startActivity(browserIntent);
dialog.cancel();
}
});
builder.setNegativeButton("No Thanks!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
}
private AdapterView.OnItemClickListener OnItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
protected void onListItemClick(ListView InstalledAppList, View v, int position, long id) {
ApplicationInfo app = applist.get(position);
try {
Intent intent = packageManager
.getLaunchIntentForPackage(app.packageName);
if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(InstalledAppActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(InstalledAppActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
};
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
for (ApplicationInfo info : list) {
try {
if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
applist.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return applist;
}
private class LoadApplications extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(InstalledAppActivity.this,
R.layout.snippet_list_row, applist);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(Void result) {
//setListAdapter(listadaptor);
InstalledAppList.setAdapter(listadaptor);
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}
package com.spicycurryman.getstructed10.app;
导入android.app.AlertDialog;
导入android.app.ProgressDialog;
导入android.content.ActivityNotFoundException;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.pm.ApplicationInfo;
导入android.content.pm.PackageManager;
导入android.net.Uri;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuInflater;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ListView;
导入android.widget.Toast;
导入com.javatechig.listapps.ApplicationAdapter;
导入java.util.ArrayList;
导入java.util.List;
公共类InstalledAppActivity扩展了FragmentActivity{
私有PackageManager PackageManager=null;
私有列表applist=null;
私有应用程序适配器ListAdapter=null;
ListView安装应用程序;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.installed_apps);
InstalledApplication=(ListView)findViewById(R.id.Installed\u列表);
packageManager=getPackageManager();
新建LoadApplications().execute();
InstalledApplication.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
ApplicationInfo-app=applist.get(i);
试一试{
Intent=packageManager
.getLaunchIntentForPackage(app.packageName);
if(null!=intent){
星触觉(意向);
}
}捕获(ActivityNotFounde异常){
Toast.makeText(InstalledAppActivity.this,e.getMessage(),
Toast.LENGTH_LONG).show();
}捕获(例外e){
Toast.makeText(InstalledAppActivity.this,e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});
}
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
super.onCreate(savedInstanceState);
packageManager=getPackageManager();
新建LoadApplications().execute();
返回充气机。充气(R.layout.installed_apps,container,false);
}
公共布尔onCreateOptions菜单(菜单){
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单栏,菜单);
返回true;
}
公共布尔值onOptionsItemSelected(菜单项项){
布尔结果=真;
开关(item.getItemId()){
案例R.id.main_文本:{
显示AboutDialog();
打破
}
默认值:{
结果=super.onOptionsItemSelected(项目);
打破
}
}
返回结果;
}
私有void显示AboutDialog(){
final AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(getString(R.string.app_name));
setMessage(getString(R.string.lamposon));
setPositiveButton(“了解更多”,新建DialogInterface.OnClickListener()){
public void onClick(DialogInterface对话框,int-id){
Intent browserIntent=新意图(Intent.ACTION\u视图,Uri.parse(“http://javatechig.com"));
startActivity(浏览器内容);
dialog.cancel();
}
});
setNegativeButton(“不,谢谢!”,新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
dialog.cancel();
}
});
builder.show();
}
私有AdapterView.OnItemClickListener OnItemClickListener=新AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
}
受保护的void onListItemClick(ListView安装的应用程序,视图v,int位置,长id){
ApplicationInfo-app=applist.get(位置);
试一试{
Intent=packageManager
.getLaunchIntentForPackage(app.packageName);
if(null!=intent
fragmentclass fragment = new fragmentclass(list);
package com.spicycurryman.getdisciplined10.app;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.ibc.android.demo.appslist.app.ApkAdapter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class InstalledAppActivity extends Fragment
implements OnItemClickListener {
//hi
PackageManager packageManager;
ListView apkList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.user_installed, container, false);
packageManager = getActivity().getPackageManager();
/*To filter out System apps*/
apkList = (ListView) rootView.findViewById(R.id.applist);
new LoadApplications(getActivity().getApplicationContext()).execute();
return rootView;
}
/**
* Return whether the given PackageInfo represents a system package or not.
* User-installed packages (Market or otherwise) should not be denoted as
* system packages.
*
* @param pkgInfo
* @return boolean
*/
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
: false;
}
private boolean isSystemPackage1(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) ? false
: true;
}
// Don't need in Fragment
/*@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.block, menu);
// super.onCreateOptionsMenu(menu,inflater);
}*/
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//ApkAdapter apkAdapter=(ApkAdapter)apkList.getAdapter();
}
private class LoadApplications extends AsyncTask<Void, Void, Void> {
Context mContext;
private ProgressDialog pDialog;
List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();
public LoadApplications(Context context){
Context mContext = context;
}
@Override
protected Void doInBackground(Void... params) {
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
/* List<ApplicationInfo> list = mContext.getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
for(int n = 0;n<list.size();n++){
if ((list.get(n).flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP))
}*/
for(PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
boolean c = isSystemPackage1(pi);
if(!b || !c ) {
packageList1.add(pi);
}
}
//sort by application name
final PackageItemInfo.DisplayNameComparator comparator = new PackageItemInfo.DisplayNameComparator(packageManager);
Collections.sort(packageList1, new Comparator<PackageInfo>() {
@Override
public int compare(PackageInfo lhs, PackageInfo rhs) {
return comparator.compare(lhs.applicationInfo, rhs.applicationInfo);
}
});
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(InstalledAppActivity.this.getActivity());
pDialog.setMessage("Loading your apps...");
pDialog.show();
}
@Override
protected void onPostExecute(Void result) {
apkList.setAdapter(new ApkAdapter(getActivity(), packageList1, packageManager));
if (pDialog.isShowing()){
pDialog.dismiss();
}
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}