簡體   English   中英

在Android Manifest中為網絡設置權限時崩潰

[英]Crash when setting permissions in Android Manifest for networks

我一直在遵循一個教程 ,在該教程中 ,我試圖使用JavaOSC庫來添加本機Android應用程序發送OSC消息的功能

以下是我用於設置OSC線程的代碼:

    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
                }
            }
        }
    }
  };

我最終遇到了一些網絡錯誤,研究表明,我可能需要在Android.manifest文件中添加以下權限行:

<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" />

但是,在添加了這些行並在Simulator(Nexus 7)上運行了該應用程序之后,我仍然收到錯誤消息,提示“該應用程序已停止”。

這實際上是我的第一個Android項目,因此我確定這里可能遺漏了一些明顯的內容(例如在發生崩潰時在哪里查找日志)。

編輯:我在API 27上。我從LogCat看到的唯一日志如下:

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

從Android Marshmallow(API 23)及更高版本開始,您可能需要實現Runtime Permission 這是一個例子,

   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
        }

您可以在onCreate內編寫此代碼,這是在運行時獲取WRITE_EXTERNAL_STORAGE權限的示例。
我認為您應該修改此行

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

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

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM