如何设置事件监听器

时间:2016-06-02 05:02:11

标签: java android android-studio

所以我设置了一个媒体记录器程序,并创建了一个向我发送通知的通知方法(notifi)。我的问题是我希望在媒体记录器停止录制时调用通知方法(mediaRecorder = null)。我假设我必须设置某种事件监听器,但我需要帮助。

public class MainActivity extends AppCompatActivity implements
    MediaRecorder.OnInfoListener, MediaController.MediaPlayerControl {
    private boolean cont;
    private MediaRecorder mediaRecorder;
    private MediaPlayer mediaPlayer;
    private String OUTPUT_FILE;
    private MediaController mediaController;

    NotificationCompat.Builder notif;
    private static final int uid = 615204;

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

        OUTPUT_FILE = Environment.getExternalStorageDirectory() + "/androidaudio1.3gpp";
        cont = true;

        notif = new NotificationCompat.Builder(this);
        notif.setAutoCancel(true);
    }

    public void outtfile() {
    }

    public void clikked(View view) {
        switch (view.getId()) {

            case R.id.startRec:
                beginRecord();
                break;

            case R.id.stopRec:
                stopRecord();
                break;

            case R.id.startPlay:
                try {
                    begginPlaying();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

            case R.id.stopPlay:
                stopPlaying();
                break;
        }
    }

    public void notifi(View view) {

        notif.setSmallIcon(R.drawable.notifi);
        notif.setTicker("Stopped Rec");
        notif.setWhen(System.currentTimeMillis());
        notif.setContentTitle("App Stopped Rec");
        notif.setContentText("App has stopped Rec session");

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        notif.setContentIntent(pendingIntent);

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(uid, notif.build());
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您需要检查

if(recorder == null){
     //Do your action
}