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

时间:2015-12-11 15:44:03

标签: java android android-listview sharedpreferences android-preferences

我有Preference屏幕。我想立即执行在首选项屏幕中更改为MainActivity的listview

的设置

Preference.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的listview会在我单击并返回mainactivity后立即使用已安装的应用程序进行更新。

1 个答案:

答案 0 :(得分:1)

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