简体   繁体   English

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

[英]how to close the activity and again show the same activity again by clicking the notification

I am uploading a video using FTP. 我正在使用FTP上传视频。 I am showing a notification also for that. 我也在为此显示通知。 If I click the notification it will take me to the application screen. 如果我单击通知,它将带我到应用程序屏幕。 So the same screen will run two times. 因此,同一屏幕将运行两次。 So I need to click back button two times. 因此,我需要单击两次后退按钮。 I want to avoid this. 我想避免这种情况。 Please help me... 请帮我...

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;
        }

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

设置FLAG_ACTIVITY_SINGLE_TOP标志intent1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过单击通知销毁活动并再次调用它 - Destroying activity and calling it again by clicking notification 通过单击通知打开应用程序时,FCM不会再次打开活动 - FCM not opening activity again when app opened by clicking notification 如何隐藏一个活动来调用另一个活动并在后台运行它并再次显示? - How to hide an ACTIVITY to call another ACTIVITY and run it on background and show it again? 活动加载一次又一次 - Activity Loading again and again 如何使抽屉滑动关闭而不是再次创建活动? - How to make drawer just slide close instead of creating the activity again? 如何关闭活动,因此无法从活动应用中再次打开? - How to close activity, so cannot be opened again from active apps? Android:如何强制关闭活动并从接收者重新开始? - Android: How to force close activity and start again from receier? 如何在不显示闪屏的情况下关闭活动? - How can I close activity without showing splash screen again? 如何在创建活动之前隐藏操作栏,然后再次显示它? - How to hide action bar before activity is created, and then show it again? 如何在我的活动中实现“不再显示”功能? - How can i implement the “do not show again” functionality on my activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM