我做一個BrodcastReceiver來說話:接聽電話,而電話鈴聲。廣播接收器的BroadcastReceiver不會停止線程
守則:
@Override
public void onReceive(Context context, Intent intent) {
cont = context;
tts = new Speak(cont);
String action = intent.getAction();
if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
incoming_number = intent.getExtras().getString(
"incoming_number");
falador = new Thread() {
@Override
public void run() {
try {
if (!this.isInterrupted()) {
while (true) {
sleep(5000);
h.post(repeater);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
falador.start();
} else {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)) {
h.removeCallbacks(repeater);
falador.interrupt();
}
}
}
}
但是,事件IDLE不會停止線程。你可以幫我嗎?
感謝, 馬特烏斯
你確定你的線程在中斷之前運行嗎?也許它根本沒有運行。做一些日誌記錄,看看這個 – waqaslam 2012-03-28 12:41:48