Guest User

Untitled

a guest
Nov 2nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. package example_company.controller1;
  2.  
  3. import android.os.AsyncTask;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import com.jcraft.jsch.Channel;
  9. import com.jcraft.jsch.ChannelExec;
  10. import com.jcraft.jsch.JSch;
  11. import com.jcraft.jsch.JSchException;
  12. import com.jcraft.jsch.Session;
  13. import android.support.design.widget.CoordinatorLayout;
  14. import android.support.design.widget.Snackbar;
  15. import android.graphics.Color;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. }
  23.  
  24. public void onClick11 (View v) {
  25. new AsyncTask<Integer, Void, Void>(){
  26. @Override
  27. protected Void doInBackground(Integer... params) {
  28. try {
  29. executeSSHcommand();
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. return null;
  34. }
  35. }.execute(1);
  36. }
  37.  
  38. public void executeSSHcommand(){
  39. String user = "pi";
  40. String password = "raspberry";
  41. String host = "192.168.0.1";
  42. int port=22;
  43. try{
  44.  
  45. JSch jsch = new JSch();
  46. Session session = jsch.getSession(user, host, port);
  47. session.setPassword(password);
  48. session.setConfig("StrictHostKeyChecking", "no");
  49. session.setTimeout(10000);
  50. session.connect();
  51. ChannelExec channel = (ChannelExec)session.openChannel("exec");
  52. channel.setCommand("omxd n");
  53. channel.connect();
  54. channel.disconnect();
  55.  
  56. }
  57. catch(JSchException e){
  58.  
  59. }
  60. }
  61. }
  62.  
  63. <?xml version="1.0" encoding="utf-8"?>
  64. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  65. xmlns:tools="http://schemas.android.com/tools"
  66. android:id="@+id/activity_main"
  67. android:layout_width="match_parent"
  68. android:layout_height="match_parent"
  69. android:paddingBottom="@dimen/activity_vertical_margin"
  70. android:paddingLeft="@dimen/activity_horizontal_margin"
  71. android:paddingRight="@dimen/activity_horizontal_margin"
  72. android:paddingTop="@dimen/activity_vertical_margin"
  73. tools:context="example_company.controller1.MainActivity">
  74.  
  75. <TextView
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:text="Hello World!" />
  79.  
  80. <Button
  81. android:text="NEXT"
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:layout_centerVertical="true"
  85. android:layout_centerHorizontal="true"
  86. android:id="@+id/button"
  87. android:onClick="onCLick11" />
  88.  
  89. </RelativeLayout>
  90.  
  91. <?xml version="1.0" encoding="utf-8"?>
  92. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  93. package="example_company.controller1">
  94.  
  95. <uses-permission android:name="android.permission.INTERNET" />
  96.  
  97. <application
  98. android:allowBackup="true"
  99. android:icon="@mipmap/ic_launcher"
  100. android:label="@string/app_name"
  101. android:supportsRtl="true"
  102. android:theme="@style/AppTheme">
  103. <activity android:name=".MainActivity">
  104. <intent-filter>
  105. <action android:name="android.intent.action.MAIN" />
  106.  
  107. <category android:name="android.intent.category.LAUNCHER" />
  108. </intent-filter>
  109. </activity>
  110. </application>
  111.  
  112. </manifest>
Add Comment
Please, Sign In to add comment