如何从Android服务获取应用程序上下文?

时间:2011-11-29 03:32:22

标签: java android android-intent android-service android-activity

我有一个正在运行并正在侦听麦克风输入的Android服务。我希望它在满足某个标准时启动活动。为了创建一个Intent,我需要应用程序上下文。我怎么能得到它?

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

上述行没有开始我的活动。

这是我的构造函数

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

这是我的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);
    }
}

myByteReceiver 正在通过音频输入侦听信号。当它找到匹配的信号时,我希望它发起一个活动。

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

这是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)

第320行是theApplication.startActivity(i);

9 个答案:

答案 0 :(得分:4)

您可以在服务中使用getApplicationContext()来获取应用程序上下文。

尝试使用

getApplication().startActivity(i);

android start activity from service

答案 1 :(得分:4)

每个服务都有自己的上下文,只需使用它即可。您不需要传递服务和活动的背景信息。

服务中不需要活动上下文。

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

只需按照“活动”

进行操作即可
  

服务和活动都是Context的子类。

答案 2 :(得分:2)

改变这个:

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

为:

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

答案 3 :(得分:1)

您断言您需要应用程序上下文来启动活动是不准确的。您可以start an activity from any context,包括服务,这是一个上下文。

答案 4 :(得分:1)

getBaseContext(),它返回上下文,因为每个活动都扩展了Context类

答案 5 :(得分:1)

您不得在其公共空构造函数中调用getApplicationContext(),否则它将为您提供NullPointerException

答案 6 :(得分:0)

ctx.getApplicationContext().startActivity(i)

吊杆。

答案 7 :(得分:0)

只需执行此操作即可访问服务上下文

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

答案 8 :(得分:0)

仅在Service的'onCreate()'之后有一个非null的上下文:在构造函数中获取服务的上下文不是一个好主意(尚无任何上下文),可通过简单的'getApplicationContext()来获取它在“ onCreate”方法中。

相关问题