简体   繁体   中英

Crash when setting permissions in Android Manifest for networks

I've been following a tutorial where I'm trying to use the JavaOSC library to add functionality for a native Android app to send OSC messages .

Below is the code I have for setting up an OSC thread:

    private String myIP = "192.168.0.3";
    private int myPort = 1234;

    private OSCPortOut oscPortOut;

    // This thread will contain all the code that pertains to OSC
  private Thread oscThread = new Thread() {
    @Override
    public void run() {
        try {
            // Connect to some IP address and port
            oscPortOut = new OSCPortOut(InetAddress.getByName(myIP), myPort);
            Log.v(TAG, "CREATED PORT");
        } catch(UnknownHostException e) {
            Log.v(TAG, "error is", e);
            // Error handling when your IP isn't found
            return;
        } catch(Exception e) {
            // Error handling for any other errors
            Log.v(TAG, "error is", e);
            return;
        }


      /* The second part of the run() method loops infinitely and sends messages every 500
       * milliseconds.
       */
        while (true) {
            if (oscPortOut != null) {
                // Creating the message
                Log.v(TAG, "CREATED MESSAGE");
                Object[] thingsToSend = new Object[3];
                thingsToSend[0] = "Hello World";
                thingsToSend[1] = 12345;
                thingsToSend[2] = 1.2345;

          /* The version of JavaOSC from the Maven Repository is slightly different from the one
           * from the download link on the main website at the time of writing this tutorial.
           *
           * The Maven Repository version (used here), takes a Collection, which is why we need
           * Arrays.asList(thingsToSend).
           *
           * If you're using the downloadable version for some reason, you should switch the
           * commented and uncommented lines for message below
           */
                OSCMessage message = new OSCMessage(myIP, Arrays.asList(thingsToSend));
                // OSCMessage message = new OSCMessage(myIP, thingsToSend);


          /* NOTE: Since this version of JavaOSC uses Collections, we can actually use ArrayLists,
           * or any other class that implements the Collection interface. The following code is
           * valid for this version.
           *
           * The benefit of using an ArrayList is that you don't have to know how much information
           * you are sending ahead of time. You can add things to the end of an ArrayList, but not
           * to an Array.
           *
           * If you want to use this code with the downloadable version, you should switch the
           * commented and uncommented lines for message2
           */
                ArrayList<Object> moreThingsToSend = new ArrayList<Object>();
                moreThingsToSend.add("Hello World2");
                moreThingsToSend.add(123456);
                moreThingsToSend.add(12.345);

                OSCMessage message2 = new OSCMessage(myIP, moreThingsToSend);
                //OSCMessage message2 = new OSCMessage(myIP, moreThingsToSend.toArray());

                try {
                    // Send the messages
                    oscPortOut.send(message);
                    oscPortOut.send(message2);
                    Log.v(TAG, "SENDING");

                    // Pause for half a second
                    sleep(500);
                } catch (Exception e) {
                    // Error handling for some error
                }
            }
        }
    }
  };

I ended up getting some network errors, where research showed that I may need to add the following permission lines to the Android.manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

However, after adding those lines and running the app on the Simulator (a Nexus 7), but I keep getting errors saying that "The app has stopped".

This is actually my first Android project, so I'm sure I may be missing something obvious here (such as where to find logs in the case of this crash).

EDIT: I'm on API 27. The only log I see from LogCat is the following:

12-13 17:07:54.299 2981-3026/com.deviantdev.pdsampleproject D/EGL_emulation: eglMakeCurrent: 0xa9f842a0: ver 2 0 (tinfo 0xa9f83300)
12-13 17:07:55.344 2981-2981/com.deviantdev.pdsampleproject I/Choreographer: Skipped 103 frames!  The application may be doing too much work on its main thread.
12-13 17:07:55.362 2981-3026/com.deviantdev.pdsampleproject D/EGL_emulation: eglMakeCurrent: 0xa9f842a0: ver 2 0 (tinfo 0xa9f83300)

                                                                             [ 12-13 17:07:55.416  2981: 2981 D/         ]
                                                                             PlayerBase::stop() from IPlayer
12-13 17:07:55.416 2981-2981/com.deviantdev.pdsampleproject D/AudioTrack: stop() called with 90720 frames delivered
12-13 17:07:55.432 2981-2981/com.deviantdev.pdsampleproject I/opensl_stream: Input buffer size estimate: 0
12-13 17:07:55.432 2981-2981/com.deviantdev.pdsampleproject I/opensl_stream: Output buffer size estimate: 0
12-13 17:07:55.432 2981-2981/com.deviantdev.pdsampleproject I/opensl_stream: Lowest margin: 11968

From Android Marshmallow(API 23) and above , you may need to implement Runtime Permission . Here is an example ,

   if (Build.VERSION.SDK_INT >= 23) {
            String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (!hasPermissions(mContext, PERMISSIONS)) {
                ActivityCompat.requestPermissions((MainActivity) mContext, PERMISSIONS, REQUEST );
            } else {
                //do something
            }
        } else {
            //do something
        }

You can write this code inside onCreate and this is example to get WRITE_EXTERNAL_STORAGE permission at run time .
I think you should modify this line

String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE};  

to

String[] PERMISSIONS = {android.Manifest.permission.INTERNET,android.Manifest.permission.ACCESS_NETWORK_STATE,android.Manifest.permission.ACCESS_WIFI_STATE};  

Hope it's helpful .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM