首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android Manifest中设置网络权限时崩溃

在Android Manifest中设置网络权限时崩溃
EN

Stack Overflow用户
提问于 2017-12-14 02:21:30
回答 1查看 109关注 0票数 0

我一直在关注一个tutorial,在那里我试图使用JavaOSC库为一个本地安卓应用程序添加发送OSC messages的功能。

下面是我用于设置OSC线程的代码:

代码语言:javascript
复制
    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文件添加以下权限行:

代码语言:javascript
复制
<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上看到的唯一日志如下:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-12-14 23:57:31

在Android Marshmallow(API23)及更高版本中,您可能需要实现Runtime Permission。下面是一个例子,

代码语言:javascript
复制
   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权限的示例。

我想你应该修改一下这一行

代码语言:javascript
复制
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE};  

代码语言:javascript
复制
String[] PERMISSIONS = {android.Manifest.permission.INTERNET,android.Manifest.permission.ACCESS_NETWORK_STATE,android.Manifest.permission.ACCESS_WIFI_STATE};  

希望能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47799773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档