我创建了一个消息传递应用程序,可以在其中发送消息,并且希望添加一项功能,可以将Location
发送给朋友。
这是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];
}
}
现在,我想上一堂课,点击Location
,即可使用GPS发送我的Location Button
。我希望像Google Maps一样发送它,使地图可以显示在Chat field
上。
其他问题是,我是否可以要求激活GPS,否则需要手动激活它?
P.S:我在activity-messager.xml中添加了一个位置按钮