1

我有首選項屏幕。我想立即執行優先屏幕改爲MainActivity的列表視圖從首選項設置屏幕返回時刷新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(); 

} 
} 

我想要的設置,當 「系統==假」,mainactivity的一旦我點擊並返回mainactivity,listview就會獲得已安裝應用程序的更新。

回答

1

將應用程序列表載入onStart()而不是onCreate()。 (還有一些其他的選擇,如果這不是你喜歡的,但這一個可能是最簡單的。)

+0

你可以幫我理解我的代碼我新去dis ...幫助表示讚賞.thanx – Rahul