视图寻呼机适配器中的广播接收器

时间:2013-11-30 07:06:43

标签: android

我有一个活动,里面有一个视图寻呼机。我想从广播中更新寻呼机的一页。所以,我在寻呼机适配器中创建了一个广播接收器。但它没有收到广播。它是否与manifest.xml中的声明有关(因为它不在某些活动中)?甚至可以这样做吗? 这是我在寻呼机适配器中的工作:

    public Object instantiateItem(View collection, int position) {
    try {
        searchBox = (EditText) activity.findViewById(R.id.searchBox);
        searchButton = (ImageView) activity.findViewById(R.id.searchButton);
        if (position == 0) {
            v1 = Inflater.inflate(R.layout.activity_search, null);
            listOfSearch = (GridView) v1.findViewById(R.id.list_search);
            searchImage = (ImageView) v1.findViewById(R.id.searchImage);
            prb = (ProgressBar) v1.findViewById(R.id.progress_bar);
            errorMessage = (TextView) v1.findViewById(R.id.errorMsg);
            searchImage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    searchBox.requestFocus();
                    imm.showSoftInput(searchBox,
                            InputMethodManager.SHOW_IMPLICIT);
                }
            });
            searchButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    imm.hideSoftInputFromWindow(searchBox.getWindowToken(),
                            0);
                    errorMessage.setText("");
                    searchImage.setVisibility(View.GONE);
                    searchButton.setOnClickListener(null);
                    listOfSearch.setAdapter(null);
                    prb.setVisibility(View.VISIBLE);
                    new getVideos().execute();
                }
            });
        }
        if (position == 1) {
            receiverl = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent.getExtras().getInt("running") == 1) {
                        int progress = intent.getExtras()
                                .getInt("progress");
                        DownloadView.setVisibility(View.VISIBLE);
                        dName.setText(intent.getExtras().getString("name"));
                        pBar.setProgress(intent.getExtras().getInt(
                                "progress"));
                        dPercentage.setText(progress + " %");
                        dQueue.setText("In queue: "
                                + intent.getExtras().getInt("queue"));
                        if (progress >= 100) {
                            apg.setInDownload(false);
                            DownloadView.setVisibility(View.GONE);
                            getSD();
                            vla = new ViewListAdapter(activity, list,
                                    list2, list3, send);
                            listOfDownloads.setAdapter(vla);
                        }
                    } else {
                        DownloadView.setVisibility(View.GONE);
                    }
                }
            };
            v1 = Inflater.inflate(R.layout.activity_downloads, null);
            DownloadView = (FrameLayout) v1.findViewById(R.id.downloadView);
            dName = (TextView) v1.findViewById(R.id.nam);
            pBar = (ProgressBar) v1.findViewById(R.id.pbb);
            dPercentage = (TextView) v1.findViewById(R.id.prcent);
            dQueue = (TextView) v1.findViewById(R.id.queue);
            listOfDownloads = (GridView) v1
                    .findViewById(R.id.list_downloads);
            errorText = (TextView) v1.findViewById(R.id.errorText);
            File file = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "Video/VidBoxLite");
            if (!file.exists()) {
                file.mkdirs();
            }
            getSD();
            if (list == null) {
                errorText.setText("No storage found.");
            } else if (list.length == 0) {
                errorText.setText("No videos found.");
            } else {
                vla = new ViewListAdapter(activity, list, list2, list3,
                        send);
                listOfDownloads.setAdapter(vla);
                vla.notifyDataSetChanged();
                listOfDownloads.setLongClickable(true);
                listOfDownloads
                        .setOnItemLongClickListener(new OnItemLongClickListener() {
                            public boolean onItemLongClick(
                                    AdapterView<?> parent, View v,
                                    int position, long id) {
                                forP = position;
                                LayoutInflater inflater = LayoutInflater
                                        .from(activity);
                                View layout = inflater.inflate(
                                        R.layout.downloaded_video_options,
                                        null);
                                ListView oka = (ListView) layout
                                        .findViewById(R.id.option);
                                ArrayAdapter<String> adp = new ArrayAdapter<String>(
                                        activity,
                                        android.R.layout.select_dialog_item);
                                adp.add("Delete");
                                adp.add("Share");
                                adp.add("Cancel");
                                oka.setAdapter(adp);
                                oka.setOnItemClickListener(new OnItemClickListener() {

                                    @Override
                                    public void onItemClick(
                                            AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                                        if (arg2 == 2)
                                            alertDD.dismiss();
                                        if (arg2 == 1) {
                                            Intent sendIntent = new Intent();
                                            sendIntent.setType("video/*");
                                            sendIntent
                                                    .setAction(Intent.ACTION_SEND);
                                            sendIntent.putExtra(
                                                    Intent.EXTRA_STREAM,
                                                    Uri.parse(list2[forP]));
                                            // startActivity(Intent.createChooser(sendIntent));
                                            activity.startActivity(Intent
                                                    .createChooser(
                                                            sendIntent,
                                                            "Share video via:"));
                                            alertDD.dismiss();
                                        }
                                        if (arg2 == 0) {
                                            alertDD.dismiss();
                                            File fle = new File(list2[forP]);
                                            if (fle.exists()) {
                                                fle.delete();
                                                getSD();
                                                if (list == null) {
                                                    errorText
                                                            .setText("No storage found.");
                                                } else if (list.length == 0) {
                                                    errorText
                                                            .setText("No videos found.");
                                                } else {
                                                    vla = new ViewListAdapter(
                                                            activity, list,
                                                            list2, list3,
                                                            send);
                                                    listOfDownloads
                                                            .setAdapter(vla);
                                                }
                                            }
                                        }
                                    }

                                });
                                AlertDialog.Builder alertD = new AlertDialog.Builder(
                                        activity);
                                alertD.setView(layout).setCancelable(false);
                                alertDD = alertD.create();
                                alertDD.show();
                                return true;
                            }
                        });
                listOfDownloads
                        .setOnItemClickListener(new OnItemClickListener() {
                            public void onItemClick(AdapterView<?> parent,
                                    View view, int position, long id) {
                                if (position < send) {
                                    Uri ttt = Uri.parse(Environment
                                            .getExternalStorageDirectory()
                                            + File.separator
                                            + "Video/VidBox/Lite/"
                                            + list[position]);
                                    Intent intent = new Intent(
                                            Intent.ACTION_VIEW, ttt);

                                    intent.setDataAndType(
                                            ttt,
                                            "video/"
                                                    + list[position]
                                                            .substring(
                                                                    list[position]
                                                                            .length() - 3,
                                                                    list[position]
                                                                            .length()));
                                    activity.startActivity(intent);
                                }
                            }
                        });
            }
        }
        ((ViewPager) collection).addView(v1, 0);
    } catch (Exception e) {
    }
    return v1;
}

1 个答案:

答案 0 :(得分:0)

您是否忘了注册接收器?您可以在清单文件中或通过调用
来执行此操作 activity.registerReceiver(接收机);方法。 你应该注意,如果你通过调用activity.registerReceiver(receiver)注册它; 当不再需要接收器时,你也必须取消注册它。