How can I get the application context from an Android Service?

谁都会走 提交于 2019-12-08 15:02:40

问题


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);


回答1:


You can use getApplicationContext() inside your service to get the application context.

Try using

getApplication().startActivity(i);

android start activity from service




回答2:


Every Service has its own Context, just use the that. You don't need to pass a Service an Activity's Context.

No need for activity context in Service.

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

Simply do as you do in Activity

Service and Activity both are subclasses of Context.




回答3:


Change this:

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

to:

Intent i = new Intent(getApplicationContext(),SONR.class);



回答4:


You're assertion that you need an application context to start an activity is inaccurate. You can start an activity from any context, including the service, which is a context.




回答5:


getBaseContext() which returns context since every activity extends Context class




回答6:


You mustn't call to getApplicationContext() inside its public empty constructor, or it will give you a NullPointerException.




回答7:


ctx.getApplicationContext().startActivity(i)

boom.




回答8:


Just do this to access the Service context

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


来源:https://stackoverflow.com/questions/8305496/how-can-i-get-the-application-context-from-an-android-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!