【问题标题】:Refresh MainActivity on returning from preference setting screen从首选项设置屏幕返回时刷新 MainActivity
【发布时间】:2015-12-11 15:44:02
【问题描述】:

我有 Preference screen 。我想立即执行在偏好屏幕中更改为 MainActivity 的列表视图的设置

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,mainactivity 的列表视图就会更新为已安装的应用程序。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 你能帮我理解我对 dis 的新代码吗...帮助表示赞赏.thanx
    猜你喜欢
    • 2015-07-13
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    相关资源
    最近更新 更多