我正在使用JobSchedulerwhichAsyncTask用于其JobService. 在MJobExecutor扩展AsyncTask使用MediaPlayer哪个需要getApplicationContext()作为参数的类中不起作用。它显示无法解析方法。
public class MJobExecutor extends AsyncTask<Void,Void,String> {
ValueExchange value;
MediaPlayer player;
@Override
protected String doInBackground(Void... params) {
value = new ValueExchange();
Calendar cal = Calendar.getInstance();
Date date=cal.getTime();
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
String formattedDate=dateFormat.format(date);
if(formattedDate.equals(value.getString())){
}
return "Long running task finishes." + value.getString();
}
private void play(){
if(player == null){
player = MediaPlayer.create(getApplicationContext(),R.raw.bensoundfunkyelement);
//In the above code getApplicationContext() not working-
//Cannot resolve method getApplicationContext()
//i have used this as context not working.
//getBaseActivity() not working.
//getActivity().getApplicationContext() also not working.
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlayer();
}
});
}
player.start();
}
private void stop(){
stopPlayer();
}
private void stopPlayer(){
if(player != null){
player.release();
player = null;
}
}
}
慕姐8265434
相关分类