两个Android手机之间的通信彼此分开

时间:2015-08-13 11:41:53

标签: android communication

我正在开发两个Android应用程序,让我们称它们为应用程序A和应用程序B. 我将在一部手机上部署应用程序A,在另一部手机上部署应用程序B.

两个应用程序都使用gps进行位置跟踪。

应用程序B上的

我将设置手机的当前位置。当应用程序B的当前位置发生变化时,我想激活或向其他手机上部署的应用程序A发送任何通知。想要跟踪应用程序A中的应用程序B(其他手机)的当前位置。

两部手机之间的距离可能是公里。请告诉我如何继续。

package com.password.authentication;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;


public class Registration implements ActionListener,MouseListener {

    private JFrame registrationFrame;
    private JLabel userNameLabel;
    private JTextField userNameText;
    private JLabel firstNameLabel;
    private JTextField firstNameText;
    private JLabel lastNameLabel;
    private JTextField lastNameText;
    private JLabel emailID;
    private JTextField emailIDText;
    private JLabel password;
    private JLabel imagePassword;
    private JButton reset;
    private JButton register;
    private int xCordinate;
    private int yCordinate;
    private int imageCount;
    private int labelWidth;
    private int textFieldWidth;
    private int height;
    private List<JTextField> allTextFiel;
    private List<String> allPoints=new ArrayList<String>();

    File[] files={new File("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg"),
             new File("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg"),
             new File("C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg")};

    public Registration()
    {
        imageCount=0;
        xCordinate=10;
        yCordinate=10;
        labelWidth=100;
        textFieldWidth=180;
        height=30;
        allTextFiel=new ArrayList<JTextField>();
        registrationFrame=new JFrame("Registration");
        registrationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        registrationFrame.setLayout(null);

        userNameLabel=new JLabel("UserName");
        userNameLabel.setBounds(xCordinate, yCordinate, labelWidth, height);
        registrationFrame.add(userNameLabel);

        userNameText=new JTextField();
        userNameText.setBounds(userNameLabel.getX()+userNameLabel.getWidth()+10, yCordinate, textFieldWidth, height);
        registrationFrame.add(userNameText);
        allTextFiel.add(userNameText);

        yCordinate=userNameLabel.getHeight()+20;

        firstNameLabel=new JLabel("First Name");
        firstNameLabel.setBounds(xCordinate, yCordinate, labelWidth, height);
        registrationFrame.add(firstNameLabel);

        firstNameText=new JTextField();
        firstNameText.setBounds(firstNameLabel.getX()+firstNameLabel.getWidth()+10, yCordinate, textFieldWidth, height);
        registrationFrame.add(firstNameText);
        allTextFiel.add(firstNameText);
        yCordinate=yCordinate+firstNameLabel.getHeight()+20;


        lastNameLabel=new JLabel("Last Name");
        lastNameLabel.setBounds(xCordinate, yCordinate, labelWidth, height);
        registrationFrame.add(lastNameLabel);
        lastNameText=new JTextField();
        lastNameText.setBounds(lastNameLabel.getX()+lastNameLabel.getWidth()+10, yCordinate, textFieldWidth,height);
        registrationFrame.add(lastNameText);
        allTextFiel.add(lastNameText);

        yCordinate=yCordinate+lastNameLabel.getHeight()+20;

        emailID=new JLabel("EmailID");
        emailID.setBounds(xCordinate, yCordinate, labelWidth, height);
        registrationFrame.add(emailID);

        emailIDText=new JTextField();
        emailIDText.setBounds(emailID.getX()+emailID.getWidth()+10, yCordinate, textFieldWidth, height);
        registrationFrame.add(emailIDText);

        allTextFiel.add(emailIDText);
        yCordinate=yCordinate+emailID.getHeight()+20;

        password=new JLabel("password");
        password.setBounds(xCordinate, yCordinate, labelWidth,height);
        registrationFrame.add(password);



         BufferedImage img;
            try {


                img = ImageIO.read(files[imageCount]);
                ImageIcon icon = new ImageIcon(img);
                imagePassword=new JLabel(icon);
                imagePassword.setBounds(password.getX()+password.getWidth()+10, yCordinate, 250, 200);
                registrationFrame.add(imagePassword);
                imagePassword.addMouseListener(this);

            }
             catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            yCordinate=yCordinate+imagePassword.getHeight()+20;
            register=new JButton("Register");
            register.setBounds(imagePassword.getX(), yCordinate, 100, height);
            registrationFrame.add(register);
            register.addActionListener(this);

            reset=new JButton("reset");
            reset.setBounds(register.getX()+register.getWidth()+10, yCordinate, 100,30);
            registrationFrame.add(reset);
            reset.addActionListener(this);

            registrationFrame.setSize(600,600);
            registrationFrame.setLocationByPlatform(true);
            registrationFrame.setVisible(true);
    }





    @Override
    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==register)
    {
        int totalImagesSelected=imageCount;
        if(totalImagesSelected==3)

        {
            for(String XYPosition:allPoints)
            {
                System.out.println("point position are "+XYPosition);
            }
            JOptionPane.showMessageDialog(registrationFrame, "Registered SuccessFully" );
            registrationFrame.dispose();


        }
        else
        {
            JOptionPane.showMessageDialog(registrationFrame, "Please select region from all images" );
        }

    }
    if(e.getSource()==reset)
    {
        for (JTextField field : allTextFiel) {
            field.setText("");
          }

    }

    }





    @Override
    public void mouseClicked(MouseEvent e) {

        int selectedXcordinate=e.getX();
        int selectedYcordinate=e.getY();
        String XYPosition=selectedXcordinate+","+selectedYcordinate;
        allPoints.add(XYPosition);
        imageCount=imageCount+1;
        int x=imagePassword.getX();
        int y=imagePassword.getY();
        int imagewidth=imagePassword.getWidth();
        int imageheight=imagePassword.getHeight();

        if(imageCount<files.length)
        {
        registrationFrame.remove(imagePassword);
        BufferedImage img;
        try {


            img = ImageIO.read(files[imageCount]);

            ImageIcon icon = new ImageIcon(img);
            imagePassword=new JLabel(icon);
            imagePassword.setBounds(x,y ,imagewidth, imageheight);
            registrationFrame.add(imagePassword);
            imagePassword.addMouseListener(this);
            registrationFrame.repaint();

        }
         catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }
    }





    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }





    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }





    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }





    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;

    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

    public class Welcome implements ActionListener {

         private JFrame frame;
         private JLabel welcome;
         private JButton login;
         private JButton admin;
         private JButton registration;
         private int xCordinate;
         private int yCordinate;

        public Welcome()
        {
                xCordinate=10;
                yCordinate=10;
                frame=new JFrame("Welcome");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(null);
                welcome=new JLabel("Welcome to Password Authentication Using Image");
                login=new JButton("Login");
                admin=new JButton("Admin Login");
                registration=new JButton("Registration");
                welcome.setBounds(xCordinate, yCordinate, 400, 30);
                frame.add(welcome);
                yCordinate=yCordinate+40;
                login.setBounds(xCordinate, yCordinate, 120, 30);
                frame.add(login);
                yCordinate=yCordinate+40;
                admin.setBounds(xCordinate, yCordinate, 120, 30);
                frame.add(admin);
                yCordinate=yCordinate+40;
                registration.setBounds(xCordinate, yCordinate, 120, 30);
                frame.add(registration);
                login.addActionListener(this);
                admin.addActionListener(this);
                registration.addActionListener(this);

                frame.setSize(500,400);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);

        }


        @Override
        public void actionPerformed(ActionEvent e) {

        if(e.getSource()==login)
        {
            Login login=new Login();
        }
        if(e.getSource()==admin)
        {
            AdminLogin admin=new AdminLogin();
            //UploadImages uploadImage=new UploadImages();
        }
        if(e.getSource()==registration)
        {
            Registration registration=new Registration();
        }

        }
        public static void main(String[] args )
        {
            Welcome welcomeScreen=new Welcome();

        }

    }

    package com.password.authentication;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;

    public class Login implements ActionListener,MouseListener {
        private JFrame loginFrame;
        private JLabel heading;
        private JLabel userNameLabel;
        private JTextField userName;
        private JLabel PasswordLabel;
        private JLabel image;
        private JButton submitt;
        private JButton reset;
        private int xCordinate;
        private int yCordinate;
        private int height;
        private int imageCount;
        private List<JTextField> allTextField;
        File[] files={new File("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg"),
                 new File("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg"),
                 new File("C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg")};

        public int getxCordinate() {
            return xCordinate;
        }


        public void setxCordinate(int xCordinate) {
            this.xCordinate = xCordinate;
        }


        public int getyCordinate() {
            return yCordinate;
        }


        public void setyCordinate(int yCordinate) {
            this.yCordinate = yCordinate;
        }


        public Login()
        {   
            imageCount=0;
            height=30;
            xCordinate=10;
            yCordinate=10;
            allTextField=new ArrayList<JTextField>();
            loginFrame=new JFrame("Login");
            loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            loginFrame.setLayout(null);
            heading=new JLabel("Enter Login Credentials");
            heading.setBounds(xCordinate, yCordinate, 180, 30);
            loginFrame.add(heading);

            yCordinate=yCordinate+height+10;

            userNameLabel=new JLabel("UserName");
            userNameLabel.setBounds(xCordinate, yCordinate, 100, height);
            loginFrame.add(userNameLabel);


            userName=new JTextField();
            userName.setBounds(userNameLabel.getX()+userNameLabel.getWidth()+10, yCordinate, 180, height);
            loginFrame.add(userName);
            allTextField.add(userName);

            yCordinate=yCordinate+height+10;

            PasswordLabel=new JLabel("Password");
            PasswordLabel.setBounds(xCordinate, yCordinate, 100, height);
            loginFrame.add(PasswordLabel);


             BufferedImage img;
            try {


                img = ImageIO.read(new File("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg"));

                ImageIcon icon = new ImageIcon(img);
                image=new JLabel(icon);
                image.setBounds(PasswordLabel.getX()+PasswordLabel.getWidth()+10, yCordinate, 250, 200);

                loginFrame.add(image);
                image.addMouseListener(this);

            }
             catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            yCordinate=yCordinate+image.getHeight()+10;
            submitt=new JButton("Submit");
            submitt.setBounds(image.getX(), yCordinate, 120, height);
            loginFrame.add(submitt);

            reset=new JButton("Reset");
            reset.setBounds(submitt.getX()+submitt.getWidth()+10, yCordinate, 120, height);
            loginFrame.add(reset);

            loginFrame.setSize(500,400);
            loginFrame.setLocationByPlatform(true);
            loginFrame.setVisible(true);
        }


        @Override
        public void actionPerformed(ActionEvent e) {

            if(e.getSource()==reset)
            {
                for (JTextField field : allTextField) {
                    field.setText("");
                  }

            }

        }


        @Override
        public void mouseClicked(MouseEvent e) {

            imageCount=imageCount+1;
            int x=image.getX();
            int y=image.getY();
            int imagewidth=image.getWidth();
            int imageheight=image.getHeight();

            if(imageCount<files.length)
            {
                loginFrame.remove(image);
            BufferedImage img;
            try {


                img = ImageIO.read(files[imageCount]);

                ImageIcon icon = new ImageIcon(img);
                image=new JLabel(icon);
                image.setBounds(x,y ,imagewidth, imageheight);
                loginFrame.add(image);
                image.addMouseListener(this);
                loginFrame.repaint();

            }
             catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            }



        }


        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }


        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }


        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }


        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

    }

package com.password.authentication;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class AdminLogin implements ActionListener {

        JFrame adminLoginFrame;
        JLabel welcomeMessage;
        JLabel userNameLabel;
        JLabel passwordLabel;
        JTextField usernameText;
        JTextField passwordText;
        JButton submit;
        JButton reset;
        int xCordinate;
        int yCordinate;
        int labelWidth;
        int height;
        int textWidth;

    public AdminLogin()
    {
        labelWidth=120;
        textWidth=180;
        height=30;
        xCordinate=30;
        yCordinate=30;
        adminLoginFrame=new JFrame(" Admin Login");
        adminLoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        adminLoginFrame.setLayout(null);
        welcomeMessage=new JLabel("Admin Login");
        welcomeMessage.setBounds(xCordinate, yCordinate, labelWidth, height);
        adminLoginFrame.add(welcomeMessage);
        yCordinate=yCordinate+welcomeMessage.getHeight()+10;
        userNameLabel=new JLabel("UserName");
        userNameLabel.setBounds(xCordinate, yCordinate, labelWidth, height);
        adminLoginFrame.add(userNameLabel);
        usernameText=new JTextField();
        usernameText.setBounds(xCordinate+userNameLabel.getWidth()+10, yCordinate, textWidth, height);
        adminLoginFrame.add(usernameText);
        yCordinate=yCordinate+usernameText.getHeight()+10;
        passwordLabel=new JLabel("Password");
        passwordLabel.setBounds(xCordinate, yCordinate, labelWidth, height);
        adminLoginFrame.add(passwordLabel);
        passwordText=new JTextField();
        passwordText.setBounds(xCordinate+passwordLabel.getWidth()+10, yCordinate, textWidth, height);
        adminLoginFrame.add(passwordText);
        yCordinate=yCordinate+passwordText.getHeight()+10;
        submit=new JButton("Submit");
        submit.setBounds(xCordinate+passwordLabel.getWidth()+10, yCordinate, 100, 30);
        adminLoginFrame.add(submit);
        submit.addActionListener(this);
        reset=new JButton("reset");
        reset.setBounds(xCordinate+passwordText.getX()+submit.getWidth(), yCordinate, 100, 30);
        adminLoginFrame.add(reset);
        adminLoginFrame.setSize(500,400);
        adminLoginFrame.setLocationByPlatform(true);
        adminLoginFrame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==submit)
        {
            new UnLockUser();
        }

    }


}


package com.password.authentication;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class UnLockUser  implements ActionListener {

    public JFrame unLockUserFrame;
    private JLabel heading;
    private JLabel userName;
    private JLabel userID;
    int xCordinate;
    int yCordinate;

    public  List<String> lockedUser=new ArrayList<String>();
    public List<JCheckBox> selectedCheckBoxes=new ArrayList<JCheckBox>();

    public UnLockUser()
    {
            unLockUserFrame=new JFrame("UnLock Users");
            unLockUserFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            unLockUserFrame.setLayout(null);
            xCordinate=70;
            yCordinate=40;
            lockedUser.add("nagesh_112");
            lockedUser.add("piyush_223");
            heading=new JLabel("Locked Users");
            heading.setForeground(Color.RED);
            heading.setBounds(xCordinate, 30, 120, 30);
            unLockUserFrame.add(heading);
            yCordinate=yCordinate+30;
            userID=new JLabel("UserID");
            userID.setBounds(xCordinate+20, yCordinate, 80, 30);
            unLockUserFrame.add(userID);
            userName=new JLabel("UserName");
            userName.setBounds(xCordinate+userID.getWidth(), yCordinate, 100, 30);
            unLockUserFrame.add(userName);


        for(int i=0;i<lockedUser.size();i++)
        {
            String userDetails=lockedUser.get(i);
            String[] user=userDetails.split("_");
            String userID=user[1];
            JCheckBox unlockUser=new JCheckBox("  "+userID);
            yCordinate=yCordinate+30;
            unlockUser.setBounds(xCordinate, yCordinate, 60, 30);
            unLockUserFrame.add(unlockUser);
            JLabel userName=new JLabel(user[0]);
            userName.setBounds(xCordinate+unlockUser.getWidth()+10, yCordinate, 100, 30);
            unLockUserFrame.add(userName);
            selectedCheckBoxes.add(unlockUser);
        }
            yCordinate=yCordinate+30;
            JButton unLock=new JButton("UnLock");
            unLock.setBounds(xCordinate, yCordinate+20, 100, 30);
            unLock.addActionListener(this);
            unLockUserFrame.add(unLock);
            unLockUserFrame.setSize(500,400);
            unLockUserFrame.setLocationByPlatform(true);
            unLockUserFrame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

            for (JCheckBox checkBox : selectedCheckBoxes) {
                if (checkBox.isSelected())
                {
                    System.out.println("seleetd checkbox value is"+checkBox.getText());
                }
            }
    }

}

0 个答案:

没有答案