0

主な活動、

public class MainActivity extends ListActivity {
    int count;
    private PackageManager packageManager = null;
    private List<ApplicationInfo> applist = null;
    private ApplicationAdapter listadaptor = null;
    ListView list;
    private ArrayList results;
    String str = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=(ListView) findViewById(android.R.id.list);
        packageManager = getPackageManager();

        new LoadApplications().execute();
        Intent service = new Intent(MainActivity.this,MyService.class);
        startService(service);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        ApplicationInfo app = applist.get(position);

        try {
            Intent intent = packageManager
                    .getLaunchIntentForPackage(app.packageName);

            if (null != intent) {
                startActivity(intent);
            }
        } catch (ActivityNotFoundException e) {
            Toast.makeText(MainActivity.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }


    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
        ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
        int i=0;
        for (ApplicationInfo info : list) {

            try {

                if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {


                    applist.add(info);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            i++;
        }

        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));
            listadaptor = new ApplicationAdapter(MainActivity.this,R.layout.snippet_list_row, applist);
            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(Void result) {
            setListAdapter(listadaptor);
            progress.dismiss();
            super.onPostExecute(result);
        }

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

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }
}

アプリケーションアダプター、

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> implements CompoundButton.OnCheckedChangeListener {
    private List<ApplicationInfo> appsList = null;
    private Context context;
    private PackageManager packageManager;
    SparseBooleanArray mCheckStates;
    CheckBox chkSelect;
    SharedPreferences.Editor editor;
    ApplicationInfo applicationInfo;
    int count;
    String Pname;
    AppInfoHolder holder;

    public ApplicationAdapter(Context context, int textViewResourceId,
                              List<ApplicationInfo> appsList) {
        super(context, textViewResourceId, appsList);
        this.context = context;
        this.appsList = appsList;
        packageManager = context.getPackageManager();
        mCheckStates = new SparseBooleanArray();
        count=appsList.size();
    }

    @Override
    public int getCount() {
        return ((null != appsList) ? appsList.size() : 0);
    }

    @Override
    public ApplicationInfo getItem(int position) {
        return ((null != appsList) ? appsList.get(position) : null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SharedPreferences sharedPrefs = context.getSharedPreferences("shared",Context.MODE_PRIVATE);
        editor = sharedPrefs.edit();
        View view = convertView;
        holder=null;
        if (null == view) {
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.snippet_list_row, null);
            holder=new AppInfoHolder();
            holder.chkSelect = (CheckBox) view.findViewById(R.id.checkbox);
            holder.appName = (TextView) view.findViewById(R.id.app_name);
            holder.packageName = (TextView) view.findViewById(R.id.app_paackage);
            holder.iconview = (ImageView) view.findViewById(R.id.app_icon);

            view.setTag(holder);
        }else{
            holder=(AppInfoHolder)view.getTag();
        }

        applicationInfo = appsList.get(position);
        if (null != applicationInfo) {


            holder.appName.setText(applicationInfo.loadLabel(packageManager));
            holder.packageName.setText(applicationInfo.packageName);                holder.iconview.setImageDrawable(applicationInfo.loadIcon(packageManager));
        }

        Collections.sort(appsList, new ApplicationInfo.DisplayNameComparator(packageManager));
        holder.chkSelect.setTag(position);


        holder.chkSelect.setChecked(sharedPrefs.getBoolean("CheckValue" + position, false));
        holder.chkSelect.setOnCheckedChangeListener(this);

        return view;
    }
    public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);
    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));

    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {
    //save the checkbox values (true or false)
        editor.putBoolean("CheckValue" + buttonView.getTag(), isChecked);

        //total number of items in the list       
        editor.putInt("count",count);
        editor.commit();
    }

    static class AppInfoHolder{
        CheckBox chkSelect;
        TextView appName;
        TextView packageName;
        ImageView iconview;
    }
}

アプリケーション アダプター (onCheckedChanged) では、チェックボックスの値 (true または false) とリスト ビュー内の項目の総数 (インストールされているアプリの総数) を保存できます。同様に、すべてのアプリケーションのパッケージ名を保存したいと思います。私が保存するためにしたことは、以下の2つの方法のいずれかを使用することです.

  1. editor.putString("名前" + buttonView.getTag(), applicationInfo.packageName); //(これを onChecked に追加)
  2. editor.putString("名前" + 位置, holder.packageName.getText().toString()); //(これを getview に追加)

これら 2 つは、パッケージ名の一部のみを保存するか、何も保存しません。すべてのパッケージ名を保存するのを手伝ってください。


注: position と buttonView.getTag() の値は 0、1、2 などです。

4

1 に答える 1