最近3个小时,我一直在为此挠头。我想处理AsyncTask中的应用程序列表,它是活动的子类,然后在recyclerview中显示结果。如果不是因为可能发生内存泄漏的事实,它可以很好地工作而不将AsyncTask设为静态。
我曾尝试将PackageManager packageManager = getApplicationContext().getPackageManager();
放在onPreExecute或活动的onCreate中,但是它仍然向我显示警告,提示不能从静态上下文中引用非静态方法...
这是工作代码,如果您有兴趣查看它的话,但这不是可行的方法(非静态AsyncTask!):
AppDrawer.java:
public class AppDrawer extends Activity {
private static final String TAG = "AppDrawer";
private AppListAdapter appListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_drawer);
appListAdapter = new AppListAdapter(this);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setAdapter(appListAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
new newThread().execute();
}
public void updateStuff() {
appListAdapter.notifyItemInserted(appListAdapter.getItemCount()-1);
}
public class newThread extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
PackageManager packageManager = getApplicationContext().getPackageManager();
AppListAdapter.appList = new ArrayList<>();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo app:apps) {
AppData newApp = new AppData();
newApp.setName(app.loadLabel(packageManager).toString());
newApp.setPackageName(app.activityInfo.packageName);
newApp.setIcon(app.activityInfo.loadIcon(packageManager));
AppListAdapter.appList.add(newApp);
}
return "Success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
updateStuff();
}
}
}
AppListAdapter.java:
public class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.ViewHolder> {
public static List<AppData> appList;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView appName;
ImageView appIcon;
ViewHolder(View itemView) {
super(itemView);
appName = itemView.findViewById(R.id.appNameTextView);
appIcon = itemView.findViewById(R.id.appIconImageView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Context context = view.getContext();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(appList.get(position).getPackageName());
context.startActivity(launchIntent);
}
}
public AppListAdapter(Context context) {
appList = new ArrayList<>();
}
@Override
public void onBindViewHolder(@NonNull AppListAdapter.ViewHolder viewHolder, int integer) {
String appName = appList.get(integer).getName();
String appPackageName = appList.get(integer).getPackageName();
Drawable appIcon = appList.get(integer).getIcon();
TextView textView = viewHolder.appName;
textView.setText(appName);
ImageView imageView = viewHolder.appIcon;
imageView.setImageDrawable(appIcon);
}
@Override
public int getItemCount() {
return appList.size();
}
@NonNull
@Override
public AppListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.app_item_view, parent, false);
return new ViewHolder(view);
}
}
因此,我又该如何从静态 AsyncTask获取应用列表?