Hallo,
Ich hatte heute mal die Idee, mir einen Media-Remote-Control für mein Handy zu programmieren.
Da ich (relativ) neu in Android-Programmierung bin, würde ich euch gerne um Hilfe bitten
:
Bis jetzt habe ich:
- (funktionierenden) Server auf meinem Computer (C#)
- Grundgerüst (Controls, etc.) für meine App (Java^^)
Mein Problem ist jetzt, dass ich nicht weiß, wie ich eine TCP-Verbindung zwischen Android und PC herstelle, da im HauptThread das anscheinend nicht gemacht werden kann (android.os.NetworkOnMainThreadException).
PS: Es sollen nur Titel/Interpret empfangen werden und 0-6 (bytes) für Play/Pause/Vol+/Vol-/etc. gesendet werden. (!)Das ganze soll/kann nur über Internes Netzwerk laufen, muss aber nicht.
App:
MainActivity.java
activity-main.xml
Vielen Dank im Voraus!,
Valerian
Ich hatte heute mal die Idee, mir einen Media-Remote-Control für mein Handy zu programmieren.
Da ich (relativ) neu in Android-Programmierung bin, würde ich euch gerne um Hilfe bitten
![:)](https://www.vb-paradise.de/wcf/images/smilies/smile.png)
Bis jetzt habe ich:
- (funktionierenden) Server auf meinem Computer (C#)
- Grundgerüst (Controls, etc.) für meine App (Java^^)
Mein Problem ist jetzt, dass ich nicht weiß, wie ich eine TCP-Verbindung zwischen Android und PC herstelle, da im HauptThread das anscheinend nicht gemacht werden kann (android.os.NetworkOnMainThreadException).
PS: Es sollen nur Titel/Interpret empfangen werden und 0-6 (bytes) für Play/Pause/Vol+/Vol-/etc. gesendet werden. (!)Das ganze soll/kann nur über Internes Netzwerk laufen, muss aber nicht.
App:
Java-Quellcode
- package com.flammalpha.mediaclient;
- import java.io.*;
- import java.net.*;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.*;
- import android.widget.*;
- public class MainActivity extends Activity {
- Socket client;
- PrintWriter out;
- BufferedReader in;
- ServerSocket server;
- public void sendPrev(View view)
- {
- Send(2);
- }
- public void sendPlayPause(View view)
- {
- Send(0);
- }
- public void sendStop(View view)
- {
- Send(3);
- }
- public void sendNext(View view)
- {
- Send(1);
- }
- public void sendVolDown(View view)
- {
- Send(5);
- }
- public void sendMute(View view)
- {
- Send(6);
- }
- public void sendVolUp(View view)
- {
- Send(4);
- }
- public void Send(int val)
- {
- try
- {
- out.println(val);
- out.flush();
- }
- catch (Exception ex)
- {
- ((TextView)findViewById(R.id.tvStatus)).setText("Unable to send");
- }
- }
- public void clickConnect(View view)
- {
- execute(Connect());
- }
- public void Connect(){
- try
- {
- client = new Socket("192.168.178.16", 4664);
- out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
- in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- ((TextView)findViewById(R.id.tvStatus)).setText("Connected to: 192.168.178.16");
- }
- catch (UnknownHostException e)
- {
- ((TextView)findViewById(R.id.tvStatus)).setText("Unknown host: 192.168.178.16");
- }
- catch (IOException e)
- {
- ((TextView)findViewById(R.id.tvStatus)).setText("No I/O");
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- }
XML-Quellcode
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:scrollbars="none"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/tvInterpret"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:scrollbarAlwaysDrawVerticalTrack="false"
- android:scrollbars="none"
- android:text="Interpret"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- <TextView
- android:id="@+id/tvTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_below="@+id/tvInterpret"
- android:text="Title"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- <LinearLayout
- android:id="@+id/linearLayout1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/PlayLayout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <Button
- android:id="@+id/btnPrev"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="sendPrev"
- android:text="<<" />
- <Button
- android:id="@+id/btnPlayPause"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="sendPlayPause"
- android:text="|| ►" />
- <Button
- android:id="@+id/btnStop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="sendStop"
- android:text="■" />
- <Button
- android:id="@+id/btnNext"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="sendNext"
- android:text=">>" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/VolumeLayout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <Button
- android:id="@+id/btnVolDown"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="sendVolDown"
- android:text="-" />
- <Button
- android:id="@+id/btnMute"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="sendMute"
- android:text="Mute" />
- <Button
- android:id="@+id/btnVolUp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="sendVolUp"
- android:text="+" />
- <Button
- android:id="@+id/btnConnect"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="clickConnect"
- android:text="->" />
- </LinearLayout>
- </LinearLayout>
- <TextView
- android:id="@+id/tvStatus"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@+id/linearLayout1"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:text="Status"
- android:textAppearance="?android:attr/textAppearanceSmall" />
- </RelativeLayout>
Vielen Dank im Voraus!,
Valerian