0

I created an Messaging App where I can send messages and I want to add a feature where I can send my Location to a friend.

This is the message.java:


    Login login;

    String friend;
    String username;
    String password;
    ArrayAdapter<String> adapter;
    ArrayList<String> myList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messager);

        Button br = (Button) findViewById(R.id.send);

        br.setOnClickListener(this);
        SharedPreferences prefl = getSharedPreferences("freunde", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefl.edit();

        final ListView messagelist = (ListView) findViewById(R.id.Messagelist);

        adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                myList);

        messagelist.setAdapter(adapter);
    }


    @Override
    public void onClick(View v) {

        SharedPreferences prefl = getSharedPreferences("freunde", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefl.edit();




        EditText mess = (EditText) findViewById(R.id.nachricht);



        try {
            String test = sendMessage(prefl.getString("Username", null), prefl.getString("Password", null), prefl.getString("Friend", null), mess.getText().toString());
            if (test.contains("1")) {


                myList.add(mess.getText().toString());
                adapter.notifyDataSetChanged();


                Toast.makeText(Messager.this, "klappt", Toast.LENGTH_LONG).show();
            }

            else if(test.contains("0")){
                Toast.makeText(Messager.this, "klappt nicht", Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }



    }


    public String sendMessage(final String userFrom, final String pw, final String userTo, final String message) throws IOException {
        final CountDownLatch latch = new CountDownLatch(1);
        final String[] registerAnswer = new String[1];

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                try {
                    url = new URL("http://palaver.se.paluno.uni-due.de/api/message/send");

                    String par = "{\"Username\":\"" + userFrom + "\",\"Password\":\"" + pw + "\",\"Recipient\":\"" + userTo + "\",\"Mimetype\":\"" + "text/plain" + "\",\"Data\":\"" + message + "\"}";
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Content-Type", "application/json");
                    conn.setDoOutput(true);

                    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
                    writer.write(par);
                    writer.flush();
                    //  Log.v("", "" + conn.getResponseCode());

                    Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                    String ausgabe = "";
                    for (int c; (c = in.read()) >= 0; )
                        ausgabe += (char) c;
                    registerAnswer[0] = ausgabe;
                    // System.out.println("registerAnswer: (INNER) " + registerAnswer[0]);
                    latch.countDown();

                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                } catch (ProtocolException e1) {
                    e1.printStackTrace();
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

        });
        t.start();
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return registerAnswer[0];
    }
}

Now I want to get a class where I can send my Location with GPS on click the Location Button. I want send it like with Google Maps that the map can shown on the Chat field.

The other questions is, can I ask to activate the GPS otherwise that he needs to be activated manually?

P.S: I added a Location Button to the activity-messager.xml

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Tatok68
  • 3
  • 1
  • as far as I know you can ask him to activate GPS but you can't activate it yourself via code. So he can get the message and press the activate button. Auto-activate is only there for law-enforcement, and healthcare professionals. – ZF007 Jun 25 '19 at 10:10
  • Have a look at [this question](https://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) , should be able to help you get the current location provided you have that permission – isstiaung Jun 25 '19 at 10:11

0 Answers0