我正在使用JobScheduler
作为其AsyncTask
的{{1}}。在扩展JobService
的类MJobExecutor
中,使用了AsyncTask
的{{1}},因为它不起作用。显示无法解决的方法。
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;
}
}
}
下面是扩展MainActivity
的{{1}}类,调用了扩展public class MainActivity extends AppCompatActivity {
private static final int JOB_ID = 101;
JobScheduler jobScheduler;
JobInfo jobInfo;
TextView textTime;
ImageButton ibLeft,ibRight,ibTop,ibBottom;
TextClock textClock;
String alarmTime = "12:00 AM";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTime = (TextView)findViewById(R.id.textView);
ibLeft = (ImageButton)findViewById(R.id.left);
ibRight = (ImageButton)findViewById(R.id.right);
ibTop = (ImageButton)findViewById(R.id.top);
ibBottom = (ImageButton)findViewById(R.id.bottom);
textClock.setPadding(0,250,0,0);
ComponentName componentName = new ComponentName(this,MJobScheduler.class);
PersistableBundle bundle = new PersistableBundle();
bundle.putString("alarmTime",alarmTime);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID,componentName);
builder.setExtras(bundle);
builder.setPeriodic(5000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setPersisted(true);
jobInfo = builder.build();
jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
}
public void Start(View view) {
jobScheduler.schedule(jobInfo);
Toast.makeText(this,"Job Started...",Toast.LENGTH_LONG).show();
}
public void Stop(View view) {
jobScheduler.cancel(JOB_ID);
Toast.makeText(this,"Job Cancelled...",Toast.LENGTH_LONG).show();
}
}
的{{1}}类。看来这堂课也没问题。
MjobExecutor
答案 0 :(得分:0)
您不能从getApplicationContext()
内部调用AsyncTask
,因为该方法是在Context
类中定义的,而不是在AsyncTask
类中定义的。仅供参考,您可以在Activity
或Service
或其子类中使用此方法,因为这些类是Context
的子类。
为了解决您的问题,您将需要通过构造函数或setter在您的Context
中传递一个MediaPlayer
对象或一个AsyncTask
对象。
例如:
public class YourTask extends AsyncTask<Void, Void, String> {
private MediaPlayer player;
public YourTask(MediaPlayer player) {
this.player = player;
}
@Override
protected String doInBackground(Void... voids) {
// todo
return null;
}
}
答案 1 :(得分:0)
AsyncTask类MJobExecutor应该具有传递上下文或媒体播放器对象的构造器
public class MJobExecuter extends AsyncTask<Void, Void, String> {
private Context context;
public YourTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(Void... voids) {
// todo
return null;
}
}
然后在扩展了调用AsyncTask的服务的MJobScheduler类中,应如图所示传递Context。
public class MJobScheduler extends JobService {
MJobExecutor mJobExecutor;
String alarmTime;
ValueExchange value;
@Override
public boolean onStartJob(final JobParameters params) {
alarmTime = params.getExtras().getString("alarmTime");
value = new ValueExchange();
value.setString(alarmTime);
mJobExecutor = new MJobExecutor(getApplicationonContext()){//pass context here..
@Override
protected void onPostExecute(String s) {
Toast.makeText(getApplicationContext(),alarmTime+" "+s,Toast.LENGTH_LONG).show();
jobFinished(params,false);
}
};
mJobExecutor.execute();
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
mJobExecutor.cancel(false);
return false;
}
}