关于安卓应用程序退出返回键状态检测的问题记录

为方便自己后续回顾安卓相关知识,本文章仅起到记录和借鉴作用


1、目前进行的使用udp进行屏幕的项目中,使用到了安卓程序退出存在如下问题:

==>点击鼠标右键即正常手机的返回键时,程序未能够正常退出,然后再次点击进去时则出现段错误而不能够再次启动的现象。如下为再次启动时打印的错误信息:

2021-03-04 16:43:20.409 8307-8536/com.example.myopenglsetest D/ywl5320: {width=1920, height=1080, csd-1=java.nio.HeapByteBuffer[pos=0 lim=36 cap=36], max-input-size=2073600, mime=video/avc, csd-0=java.nio.HeapByteBuffer[pos=0 lim=36 cap=36]}

2021-03-04 16:43:20.410 8307-8537/com.example.myopenglsetest W/whldebug: start pthread_ffmpeg_recv_rtmp_speech_data

2021-03-04 16:43:20.411 8307-8537/com.example.myopenglsetest W/whldebug: fopen h264 error!!!!!!!!!!!!!

2021-03-04 16:43:20.412 8307-8537/com.example.myopenglsetest W/whldebug: SockOpt Sendbuff BeforeSet Value:212992, optLen:4

2021-03-04 16:43:20.412 8307-8537/com.example.myopenglsetest W/whldebug: SockOpt Sendbuff BeforeSet Value:425984, optLen:4

2021-03-04 16:43:20.412 8307-8537/com.example.myopenglsetest W/whldebug: pthread_ffmpeg_recv_rtmp_speech_data video_width:1920, video_height:1080!!!!!

2021-03-04 16:43:20.427 8307-8540/com.example.myopenglsetest I/OMXClient: MuxOMX ctor

2021-03-04 16:43:20.436 8307-8539/com.example.myopenglsetest E/BufferQueueProducer: [SurfaceTexture-4-8307-0] connect: already connected (cur=3 req=3)

2021-03-04 16:43:20.436 8307-8539/com.example.myopenglsetest E/MediaCodec: native_window_api_connect returned an error: Invalid argument (-22)

2021-03-04 16:43:20.436 8307-8536/com.example.myopenglsetest E/MediaCodec: configure failed with err 0xffffffea, resetting...

2021-03-04 16:43:20.450 8307-8540/com.example.myopenglsetest I/OMXClient: MuxOMX ctor

2021-03-04 16:43:20.458 8307-8345/? E/AndroidRuntime: FATAL EXCEPTION: Thread-71

    Process: com.example.myopenglsetest, PID: 8307

    java.lang.IllegalStateException

        at android.media.MediaCodec.native_dequeueInputBuffer(Native Method)

        at android.media.MediaCodec.dequeueInputBuffer(MediaCodec.java:2505)

        at com.example.myopenglsetest.WhlPlayer.decodeAVPacket(WhlPlayer.java:125)

2021-03-04 16:43:20.462 8307-8536/? I/Process: Sending signal. PID: 8307 SIG: 9

==>在深入的发掘后,发现之所以出现段错误,是由于在对程序退出时,有一些线程还未能够正常退出导致再次启动时出现异常奔溃

package com.example.myopenglsetest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
//import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    public  WhlPlayer whlPlayer;
    public WHLGLSurfaceView whlglSurfaceView;

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        //TextView tv = findViewById(R.id.sample_text);
        //tv.setText(stringFromJNI());

        whlglSurfaceView = findViewById(R.id.whlglsurfaceview);
        whlPlayer = new WhlPlayer();
        whlPlayer.setWhlglSurfaceView(whlglSurfaceView);
        whlPlayer.onCallInitMeiaCodecCallJava();
        whlPlayer.onCallStartUdpScreenJava();

        System.out.println("onCreate success");
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

解决方法参考:参考链接

在安卓程序一开始的activity处重写onKeyUp()这个函数,当检测到KEYCODE_BACK即返回键抬起时,然后执行System.exit()退出整个程序进程,完美解决我遇到的这个问题。

如下为我修改前和修改后的MainActivity的类实现:

修改前:

修改后,增加返回键检测:

package com.example.myopenglsetest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
//import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    public  WhlPlayer whlPlayer;
    public WHLGLSurfaceView whlglSurfaceView;

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        //TextView tv = findViewById(R.id.sample_text);
        //tv.setText(stringFromJNI());

        whlglSurfaceView = findViewById(R.id.whlglsurfaceview);
        whlPlayer = new WhlPlayer();
        whlPlayer.setWhlglSurfaceView(whlglSurfaceView);
        whlPlayer.onCallInitMeiaCodecCallJava();
        whlPlayer.onCallStartUdpScreenJava();

        System.out.println("onCreate success");
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
            case KeyEvent.KEYCODE_BACK:
                System.out.println("onKeyUp KeyCode_back");
                System.exit(0);
        }

        return true;
    }


    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值