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);
You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.
Services can be started with Context. startService() and Context.
Definition. it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically, you call it to get information regarding another part of your program (activity and package/application).
You can use getApplicationContext()
inside your service to get the application context.
Try using
getApplication().startActivity(i);
android start activity from service
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.
Change this:
Intent i = new Intent(ctx, SONR.class);
to:
Intent i = new Intent(getApplicationContext(),SONR.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With