如何关闭活动并再次通过单击通知再次显示相同的活动

时间:2012-06-27 12:15:26

标签: android

我正在使用FTP上传视频。我也正在显示通知。如果我点击通知,它将带我进入应用程序屏幕。所以相同的屏幕将运行两次。所以我需要两次点击后退按钮。我想避免这种情况。请帮帮我......

public class loadVideo extends AsyncTask<Void, Void, Void> {
        String status = null;
        FTPClient ftp = new FTPClient();
        InputStream in = null;
        int progress = 2;
        String dbftpserver, dbftpusername, dbftpPassword, dbftpPort,
                dbftpDirPath = null;
        String hostName, username, password = null;
        NotificationManager notificationManager = (NotificationManager) getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);

        int id = 10;

        @Override
        protected Void doInBackground(Void... arg0) {
            UUID uniqueKey = UUID.randomUUID();
            fname = uniqueKey.toString();
            Log.e("UNIQUE NAME", fname);
            String strquery1 = "SELECT FTP_Server,FTP_Username,FTP_Password,FTP_PORT,FTP_Directory_Path FROM hwd_settings WHERE ID = 1 ";
            Cursor mCursor = (MainscreenActivity.JEEMAHWDroidDB).rawQuery(
                    strquery1, null);
            if (mCursor.getCount() != 0) {
                mCursor.moveToLast();
                for (int i = mCursor.getCount() - 1; i >= 0; i--) {
                    dbftpserver = mCursor.getString(0);
                    dbftpusername = mCursor.getString(1);
                    dbftpPassword = mCursor.getString(2);
                    dbftpPort = mCursor.getString(3);
                    dbftpDirPath = mCursor.getString(4);
                }
            }
            hostName = dbftpserver;
            username = dbftpusername;
            password = dbftpPassword;
            try {
                ftp.connect(hostName);
                ftp.login(username, password);
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                ftp.enterLocalPassiveMode();
                ftp.changeWorkingDirectory(dbftpDirPath + "uploads");
                int reply = ftp.getReplyCode();
                System.out.println("Received Reply from FTP Connection:"
                        + reply);
                if (FTPReply.isPositiveCompletion(reply)) {
                    System.out.println("Connected Success");                    
                    doftpVideo();
                } else {
                    Log.e("FTP", "CONNECTION ERROR");
                    finish();
                    Toast.makeText(getApplicationContext(),
                            "Unable to connect FTP", Toast.LENGTH_LONG).show();
                }
                return null;
            } catch (Exception i) {
                i.printStackTrace();
                String temp = i.toString();
                if (temp.equalsIgnoreCase("java.net.UnknownHostException: "
                        + hostName)) {
                    showAlert();
                }

            }
            return null;
        }

        private void showAlert() {
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    NewVideoActivity.this);
            alert.setMessage("Unable to connect FTP");
            alert.setPositiveButton(getResources().getString(R.string.ok),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            dialog.cancel();
                        }
                    }).show();
        }

        public void doftpVideo() {
            Intent intent1 = new Intent(NewVideoActivity.this,
                    MainscreenActivity.class);
            final PendingIntent pendingIntent = PendingIntent.getActivity(
                    getApplicationContext(), 0, intent1, 0);
            Notification notification = new Notification();
            final String location = selectedPath;
            final SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor prefsEditor = prefs.edit();
            try {
                int icon = R.drawable.video_upload; // icon from resources
                CharSequence tickerText = "Starting Video Upload"; // ticker-text
                long when = System.currentTimeMillis(); // notification time
                Context context = getApplicationContext(); // application
                                                            // Context
                CharSequence contentTitle = "JEEMAHWDroid"; // expanded message
                                                            // title
                CharSequence contentText = "Uploading " + strTitle; // expanded
                                                                    // message
                                                                    // text
                notification = new Notification(icon, tickerText, when);
                notification.flags = notification.flags
                        | Notification.FLAG_ONLY_ALERT_ONCE
                        | Notification.FLAG_NO_CLEAR;
                notification.setLatestEventInfo(context, contentTitle,
                        contentText, pendingIntent);
                notificationManager.notify(id, notification);
                File f1 = new File(location);
                in = new FileInputStream(f1);
                ftp.storeFile(fname + "." + extension, in);
                System.out.println("SUCCESS");
                System.out.println("Video Title:" + strTitle
                        + " is uploaded successfully");
                System.out.println("Video Name:" + fname
                        + " is uploaded successfully");
                status = "completed";
                prefsEditor.putString("status", status);
                prefsEditor.commit();
                String tempStatus = prefs.getString("status", null);
                System.out.println("tempSTATUS-->>>" + tempStatus);
                strUserName = prefs.getString(
                        getResources().getString(R.string.username), null);
                strToken = prefs.getString(
                        getResources().getString(R.string.token), null);
                url = prefs.getString("VALUE", null);
                doftpImage();
                doCreateRecord();
            } catch (Exception i) {
                i.printStackTrace();
            }
            return;
        }

2 个答案:

答案 0 :(得分:0)

尝试在AndroidManifest.xml中为MainscreenActivity设置android:launchMode="singleInstance"

答案 1 :(得分:0)

FLAG_ACTIVITY_SINGLE_TOP标记设置为intent1