当我的应用程序启动时,我正在使用 AsyncTask 来获取联系人的铃声,它可以正常工作,直到我的 Activity 在 AsyncTask 期间关闭几次之后,在这种情况下,AsyncTask 只会到达 onPreExecute() 并且永远不会 doInBackground ,所以在强制停止或设备重启之前,我永远无法获取铃声。
谁能解释为什么会发生这种情况?
为什么 AsyncTask 会到达 onPreExecute 但从不运行 doInBackground()?
这是我的代码:(按照货架源代码)
public void getRingTone(){
Log.d("cda", "Into getRingTone");
if (audio_service.getStreamVolume(AudioManager.STREAM_RING) > 0) {
if(aRingTone != null){
oRingtone = RingtoneManager.getRingtone(this,
Uri.parse(aRingTone));
}
this.setVolumeControlStream(AudioManager.STREAM_RING);
}
}
private void saveRingToneTask(Bundle outState) {
final SelectRingtoneTask task = srtt;
if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) {
task.cancel(true);
srtt = null;
}
}
private void restoreRingToneTask(Bundle savedInstanceState) {
srtt = (SelectRingtoneTask) new SelectRingtoneTask().execute();
}
private void onAddRingTone() {
srtt = (SelectRingtoneTask) new SelectRingtoneTask().execute();
}
private void onCancelAddRingTone() {
if (srtt != null && srtt.getStatus() == AsyncTask.Status.RUNNING) {
srtt.cancel(true);
srtt = null;
}
}
--
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
restoreRingToneTask(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (isFinishing()) {
saveRingToneTask(outState);
}
}
private class SelectRingtoneTask extends AsyncTask<String, Void, Void> {
@Override
public void onPreExecute() {
Log.d("cda", "Into selectRingToneTask - onPreExecute() - " + selectRingtoneFinished);
findViewById(R.id.answercallimage).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
serviceBinder.answer(lineId);
onCancelAddRingTone();
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
findViewById(R.id.declinecallimage).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mNotificationManager.cancel(2);
callConnected = false;
try {
serviceBinder.reject(lineId);
onCancelAddRingTone();
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
public Void doInBackground(String... params) {
Log.d("cda", "Into selectRingToneTask - !!!!!!!!!!!!!!!");
if(!this.isCancelled()){
getRingTone();
}
return null;
}
@Override
public void onCancelled() {
Log.d("cda", "Into selectRingToneTask - onCancelled() - ");
}
@Override
public void onPostExecute(final Void unused) {
selectRingtoneFinished = true;
Log.d("cda", "Into selectRingToneTask - onPostExecute - " + selectRingtoneFinished);
if(oRingtone != null && playRingTone){
Log.d("cda", "Into getRingTone - PLAY RINGTONE");
oRingtone.play();
}
}
}
onAddRingtone() 用于 onCreate 和 onCancelRingTone() 用于 onDestroy() 以及您可以在上面的代码中查看的位置。
我已经花了3天的时间,我还没有找到解决方案?我采取了错误的方法吗?使用取消错误?有错误吗?