我正在開發鬧鐘應用程序。
如何使用AsyncTask處理按鈕點擊?
我用廣播接收機來接收鬧鐘時間。
當報警時間到了,我顯示一個活動,它啓動鈴聲&使用AsyncTask振動2分鐘。
在該活動中,我有兩個按鈕,命名爲
- 加
- 減
當我按下這些按鈕的點擊事件被推遲到火,表示不由於在後臺運行(播放鈴聲)中運行的asyncTask,因此按下按鈕時點擊。
我讀到的AsyncTask運行在單獨的線程,
比我的按鈕單擊事件應該火,因爲它推向但在這種情況下,它沒有做相同。
如果任何機構有這種情況,並得到解決方案,那麼請建議我!
以下是我的代碼。
new RingtonePlay().execute("");
以下是執行:
使用調用。
public class RingtonePlay extends AsyncTask<String,Void, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
try
{
audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
originalVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
vibratorObj = (Vibrator)mContext.getSystemService(Context.VIBRATOR_SERVICE);
volume = originalVolume;
listObjs.clear();
listObjs.add(audioManager);
listObjs.add(originalVolume);
listObjs.add(vibratorObj);
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params)
{
try
{
Cursor cursorSettings = dbHelper.getAllRecords(Info.SETTINGS);
if(cursorSettings!=null && cursorSettings.getCount()>0)
{
cursorSettings.moveToFirst();
durationInMilliSeconds = cursorSettings.getString(cursorSettings.getColumnIndex(Info.DEFAULT_DURATION));
vibrate = cursorSettings.getInt(cursorSettings.getColumnIndex(Info.DEFAULT_VIBRATE));
if(toneName.equals(""))
{
toneName = cursorSettings.getString(cursorSettings.getColumnIndex(Info.DEFAULT_TONE_NAME));
tonePath = cursorSettings.getString(cursorSettings.getColumnIndex(Info.DEFAULT_TONE_PATH));
}
listObjs.add(vibrate); // For vibration [ YES = 1, NO = 0]
}
else
{
listObjs.add(0); // For vibration [ YES = 1, NO = 0]
}
if(cursorSettings!=null)
cursorSettings.close();
durationInMilliSeconds = 5000;
ringTone = RingtoneManager.getRingtone(mContext, tonePathURI);
if(ringTone!=null)
{
ringTone.play();
}
if(ringTone==null && vibrate==0)
{
// No need to start any counter...
}
else
{
timer = new MyCountDownTimer(durationInMilliSeconds, 1000, ringTone, mContext, listObjs);
timer.start();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
}
}
是的,AsyncTask確實在後臺線程上運行,並且不應該阻塞你的UI線程,如果你正確實現它。向我們顯示您的代碼,以便我們可以更好地幫助您。 – xbonez 2013-04-25 05:45:41