मेरे पास एक एंड्रॉइड सेवा है जो माइक्रोफोन इनपुट के लिए चल रही है और सुन रही है। मैं चाहता हूं कि यह एक निश्चित मानदंड पूरा होने पर एक गतिविधि शुरू करे। एक इरादा बनाने के लिए मुझे आवेदन संदर्भ की आवश्यकता है। मैं इसे कैसे प्राप्त कर सकता हूँ?मैं एंड्रॉइड सेवा से एप्लिकेशन संदर्भ कैसे प्राप्त कर सकता हूं?
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);
}
}
}
यहाँ स्टैकट्रेस
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);
आप विधि आप में 'getApplicationContext' फोन पोस्ट किया जा सका उपयोग करने के लिए करते हैं? – Craigy
मैंने इसे –
पोस्ट किया है यदि आप NullPointerException के लॉग को पोस्ट करते हैं तो यह अधिक उपयोगी होगा। – keno