![Pan Zelený :mrgreen:](./images/smilies/icon_mrgreen.gif)
Když tu mame tu sekci java, neumí někdo v Android studiu?
Při brouzdaní po netu jsem minulý týden našel toto.package com.example.myapplicationssh;
import android.os.AsyncTask;
import android.os.Bundle;
import java.io.ByteArrayOutputStream;
import java.util.Properties;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import android.app.Activity;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static String executeRemoteCommand(String username,String password,String hostname,int port)
throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, port);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelssh = (ChannelExec)
session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelssh.setOutputStream(baos);
// Execute command
//channelssh.setCommand("lsusb > /home/pi/test1.txt");
channelssh.setCommand("sudo reboot");
channelssh.connect();
try{Thread.sleep(10000);}catch(Exception ee){};
channelssh.disconnect();
return baos.toString();
}
public void zmacklTlacitko(View view) {
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
try {
executeRemoteCommand("pi", "xxxx","192.168.1.5", 22);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
}
}
Kód: Vybrat vše
sed -n 5p
Kód: Vybrat vše
~pi/pic
Kód: Vybrat vše
package com.example.myapplicationssh;
import android.os.AsyncTask;
import android.os.Bundle;
import java.io.ByteArrayOutputStream;
import java.text.BreakIterator;
import java.util.Properties;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
//private String aaaa;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static String executeRemoteCommand(String username,String password,String hostname,int port)
throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, port);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
ChannelExec channelssh = (ChannelExec)
session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelssh.setOutputStream(baos);
// channelssh.setCommand("lsusb > /home/pi/test1.txt");
// channelssh.setCommand("sudo reboot");
channelssh.setCommand("cat /home/pi/teplota");
channelssh.connect();
try{Thread.sleep(10000);}catch(Exception ee){};
channelssh.disconnect();
return baos.toString();
}
public void zmacklTlacitko(View view) {
final TextView Label1 = (TextView) findViewById(R.id.Label1); //Získejte ID pro textiew
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
try {
executeRemoteCommand("pi", "xxxxxx","192.168.1.5", 22);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
Label1.setText("After Clicking");
}
}
Je mi uplně jedno jestli asynchronně nebo synchronně.Upozorňují, že android studio jsem viděl týden před 2 roky a týden teď a Javu neumím, takže mně berte s rezervou
Kód: Vybrat vše
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
try {
executeRemoteCommand("pi", "xxxxxx","192.168.1.5", 22);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
Kód: Vybrat vše
try {
executeRemoteCommand("pi", "xxxxxx","192.168.1.5", 22);
} catch (Exception e) {
e.printStackTrace();
}
Kód: Vybrat vše
protected Void doInBackground(Integer... params) {
try {
String temp = executeRemoteCommand("pi", "xxxxxx","192.168.1.5", 22);
Label1.setText(temp);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Trochu si odporuješ. Jestliže v baos i ve Variable, cokoliv to v AndroidStudiu je, vidíš
Nechápu. Nevidím důvod. Button je UI widget s nápisem a umí se mačkat, pokud je povolený.
Tady ^ máš nápovědu - pošli si to přes nějaký message bus mezi vlákny.
Kód: Vybrat vše
public class DisplayResult extends AsyncTask<...> {
private Label destinationLabel;
public DisplayResult(Label destinationLabel) {
this.destinationLabel = destinationLabel;
}
private String result;
doInBackground(){
result = jakákoliv akce vracející zrovna string
}
onPostExecute(){
Activity.runOnUiThread(() -> destinationLabel.setText(result));
}
}