【问题标题】:Crash when setting permissions in Android Manifest for networks在 Android 清单中为网络设置权限时崩溃
【发布时间】:2017-12-13 18:21:29
【问题描述】:

我一直在关注tutorial,我正在尝试使用JavaOSC 库为原生Android 应用添加发送OSC messages 的功能。

以下是我用于设置 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" />

但是,添加这些行并在模拟器(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

【问题讨论】:

  • 转到 LogCat 并检查与您的崩溃相关的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/…
  • 你的 API 版本是多少?
  • @zey,我在 API 27
  • @CommonsWare 我刚刚更新了问题 - ty!
  • 这是一个警告 (W/),而不是错误。它似乎来自 Google Play 服务 (com.google.android.gms)。而且,它不是 Java 堆栈跟踪。在我之前链接的问题上,显示堆栈跟踪的最佳答案是this one

标签: java android android-manifest osc


【解决方案1】:

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2016-08-20
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多