gpt4 book ai didi

java - 从首选项设置屏幕返回时刷新 MainActivity

转载 作者:行者123 更新时间:2023-11-30 01:42:39 26 4
gpt4 key购买 nike

我有首选项屏幕。我想立即执行在偏好屏幕中更改的设置到 MainActivity 的 ListView

首选项.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Sys apps">
<CheckBoxPreference
android:title="Sytem Apps"
android:key="system"
android:defaultValue="true"
android:summary="Click To View Only Installed Apps"
/>
</PreferenceCategory>
</PreferenceScreen>

MainActivity.class

public class MainActivity extends ListActivity {
PackageManager packageManager;
List<ApplicationInfo> applist;
Listadapter listadapter;
String packageName="";
boolean isUnInstallClicked;
int mPosition;
boolean installed;
ApplicationInfo info;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

packageManager = getPackageManager();

new LoadApplications().execute();

}

@Override
protected void onResume() {
super.onResume();

if(isUnInstallClicked && !appInstalledOrNot(packageName)){
applist.remove(listadapter.getItem(mPosition));
listadapter.notifyDataSetChanged();
}

}


private boolean appInstalledOrNot(String uri){
PackageManager pm=getPackageManager();
boolean app_installed;
try{
pm.getPackageInfo(uri,PackageManager.GET_ACTIVITIES);
app_installed=true;
}catch (PackageManager.NameNotFoundException e){
app_installed=false;
}
return app_installed;
}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
ArrayList<ApplicationInfo> applist = new ArrayList<>();
SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
installed=preferences.getBoolean("system", true);
for (ApplicationInfo info : list) {
try {
if(installed==true) {
if ((info.flags & ApplicationInfo.FLAG_SYSTEM)!=0) {
applist.add(info);
listadapter.notifyDataSetChanged();
}
}
else if(installed==false){
if ((info.flags & ApplicationInfo.FLAG_SYSTEM)==0) {
applist.add(info);
listadapter.notifyDataSetChanged();
}

}
} 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));

listadapter = new Listadapter(MainActivity.this, R.layout.list_item, applist);

return null;
}

@Override
protected void onPostExecute(Void aVoid) {
setListAdapter(listadapter);
progress.dismiss();
super.onPostExecute(aVoid);

}

@Override
protected void onPreExecute() {
progress = ProgressDialog.show(MainActivity.this, null, "loading apps info,,,");
super.onPreExecute();
}

}

@Override
protected void onPause() {
super.onPause();

}
}

我希望当“system==false”时,只要我点击返回并返回 mainactivity,mainactivity 的 ListView 就会更新安装的应用程序。

最佳答案

onStart() 而不是 onCreate() 中加载应用程序列表。 (如果您不喜欢,还有其他一些选择,但这个可能是最简单的。)

关于java - 从首选项设置屏幕返回时刷新 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com