マイク入力を実行してリスニングするAndroidサービスがあります。一定の基準を満たしたときに活動を開始してほしい。インテントを作成するには、アプリケーションコンテキストが必要です。どうすれば入手できますか?
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);
ctx.getApplicationContext().startActivity(i)
ブーム。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加