我怎样才能得到一个Android服务的应用程序上下文?上下文、应用程序、Android

2023-09-12 23:45:25 作者:古城白衣少年殇

我正在运行并监听麦克风输入一个Android服务。我希望它推出在满足特定条件的活动。为了创建一个Intent我所需要的应用程序上下文。我怎样才能得到它?

 意向书我=新的意图(CTX,SONR.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(ⅰ);
 

以上行不会启动我的活动。

这是我的构造

 公共SONRClient(上下文C,AudioRecord AR,INT BUFFSIZE,最终AudioManager上午){
        theAudioManager =我;
        theaudiorecord = AR;
        BUFFERSIZE = BUFFSIZE;
        CTX = C;
        CLIENT_ON = TRUE;
    }
 

这是我的onCreate

  @覆盖
    公共无效的onCreate(){
        尝试 {
            // LogFile.MakeLog(\ñ\ nSONRClient创造了);
            clientSto preceiver =新申通preceiver();
            ctx.registerReceiver(clientSto preceiver,新的IntentFilter(SONR.DISCONNECT_ACTION));
            myByteReceiver =新SONRByteReceiver();
            theListener =新MicSerialListener(theaudiorecord,缓冲区大小,myByteReceiver);
            theApplication = getApplication();
        }赶上(例外五){
            e.printStackTrace();
            。ErrorReporter.getInstance()handleException(E);
        }
    }
 
Android应用程序窗口 Activity 的运行上下文环境 Context 的创建过程分析

有myByteReceiver 被监听通过音频输入信号。当它找到一个匹配的信号,我希望它推出的一项活动。

 私有类SONRByteReceiver实现ByteReceiver {
        私人长lastplaytime = 0;
        私人长lastmutetime = 0;
        私人长lastskiptime = 0;
        私人长lastvolutime = 0;
        私人长lastbacktime = 0;

        公共无效receiveByte(INT receivedByte){
            尝试 {
                theKeyEvent = -1;

                如果(ismuted){
                    如果(receivedByte!= MUTE){
                        体积= 0;
                        ismuted = FALSE;
                    }
                }

                开关(receivedByte){

                案例SONR_HOME:
                    Log.d(TAG,HOME);

                    意图I =新的意图(CTX,SONR.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    theApplication.startActivity(ⅰ);

                    打破;
                默认:
                    Log.d(TAG,默认);
                    Log.d(TAG获得+ receivedByte);
                    // LogFile.MakeLog(接收+ receivedByte);
                    打破;
                }

                如果(theKeyEvent> = 0){
                    sendbroadcast();
                }
            }赶上(例外五){
                e.printStackTrace();
                。ErrorReporter.getInstance()handleException(E);
            }
        }
    }
 

下面是堆栈跟踪

 显示java.lang.NullPointerException
    在com.sonrlabs.test.sonr.SONRClient $ SONRByteReceiver.receiveByte(SONRClient.java:320)
    在com.sonrlabs.test.sonr.AudioProcessor.processSample(AudioProcessor.java:145)
    在com.sonrlabs.test.sonr.AudioProcessor.run(AudioProcessor.java:58)
 

320线是 theApplication.startActivity(我);

解决方案

  ctx.getApplicationContext()。startActivity(I)
 

热潮。

I have an Android service that is running and listening for microphone input. I want it to launch an activity when a certain criteria is met. In order to create an Intent I need the application context. How can I get it?

Intent i = new Intent(ctx, SONR.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);

The above line does not start my activity.

Here is my constructor

public SONRClient(Context c, AudioRecord ar, int buffsize, final AudioManager am) {
        theAudioManager = am;
        theaudiorecord = ar;
        bufferSize = buffsize;
        ctx = c;
        CLIENT_ON = true;
    }

Here is my onCreate

@Override
    public void onCreate() {
        try {
            // LogFile.MakeLog("\n\nSONRClient CREATED");
            clientStopReceiver = new StopReceiver();
            ctx.registerReceiver(clientStopReceiver, new IntentFilter(SONR.DISCONNECT_ACTION));
            myByteReceiver = new SONRByteReceiver();
            theListener = new MicSerialListener(theaudiorecord, bufferSize, myByteReceiver);
            theApplication = getApplication();
        } catch (Exception e) {
            e.printStackTrace();
            ErrorReporter.getInstance().handleException(e);
        }
    }

There is myByteReceiver that is listening for signals via audio input. When it finds a matching signal, I want it to launch an activity.

private class SONRByteReceiver implements ByteReceiver {
        private long lastplaytime = 0;
        private long lastmutetime = 0;
        private long lastskiptime = 0;
        private long lastvolutime = 0;
        private long lastbacktime = 0;

        public void receiveByte(int receivedByte) {
            try {
                theKeyEvent = -1;

                if (ismuted) {
                    if (receivedByte != MUTE) {
                        volume = 0;
                        ismuted = false;
                    }
                }

                switch (receivedByte) {

                case SONR_HOME:
                    Log.d(TAG, "HOME");

                    Intent i = new Intent(ctx, SONR.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    theApplication.startActivity(i);

                    break;
                default:
                    Log.d(TAG, "default");
                    Log.d(TAG,"RECEIVED " + receivedByte);
                    // LogFile.MakeLog("RECEIVED " + receivedByte);
                    break;
                }

                if (theKeyEvent >= 0) {
                    sendbroadcast();
                }
            } catch (Exception e) {
                e.printStackTrace();
                ErrorReporter.getInstance().handleException(e);
            }
        }
    }

Here is the stacktrace

java.lang.NullPointerException
    at com.sonrlabs.test.sonr.SONRClient$SONRByteReceiver.receiveByte(SONRClient.java:320)
    at com.sonrlabs.test.sonr.AudioProcessor.processSample(AudioProcessor.java:145)
    at com.sonrlabs.test.sonr.AudioProcessor.run(AudioProcessor.java:58)

Line 320 is theApplication.startActivity(i);

解决方案

ctx.getApplicationContext().startActivity(i)

boom.