Я использую JobScheduler
, который использует AsyncTask
для его 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;
}
}
}
Ниже приведен файл MainActivity
. В этом файле нет проблем.
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();
}
}
Ниже приведен класс MjobExecutor
, который расширяет JobService
, вызывает MJobExecutor
класс, который расширяет AsyncTask
. Кажется, в этом классе тоже нет проблем.
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(){
@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;
}
}