如何从JobScheduler的JobService将MediaPlayer对象传递给AsyncTask

时间:2018-09-03 10:15:57

标签: java android

我正在使用JobScheduler,其JobService使用AsyncTask。在扩展AsyncTask的MJobExecutor类中使用MediaPlayer。我想传递媒体播放器对象,但如何传递。以下是我的AsyncTask代码 名为MJobExecutor.java的类     我已经构造了一个传递MediaPlayer对象的构造函数。但是代码似乎不起作用。

public class MJobExecutor extends AsyncTask<Void,Void,String> {

ValueExchange value;
private MediaPlayer player;
//Constructor to pass MediaPlayer object.
public MJobExecutor(MediaPlayer player){
    this.player = 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())){

    }
    play();
    return "Long running task finishes." + value.getString();
}



private void play(){
    if(player == null){
        //Below the getApplicationContext() not working even after the constructor-
        //being declared.
        player = MediaPlayer.create(getApplicatioContext(),R.raw.bensoundfunkyelement);
        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;
    }
}

}

再次在扩展JobService的MJobScheduler.java类中,传递了MediaPlayer对象。但是它不起作用。

public class MJobScheduler extends JobService {

MJobExecutor mJobExecutor;
String alarmTime;
ValueExchange value;
MediaPlayer player;
@Override
public boolean onStartJob(final JobParameters params) {
    alarmTime = params.getExtras().getString("alarmTime");
    value = new ValueExchange();
    value.setString(alarmTime);
    //MediaPlayer object is Passed into the constructor-
    //but the app crashes
    mJobExecutor = new MJobExecutor(player){

        @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;
}
}

主要活动类如下...

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 个答案:

答案 0 :(得分:0)

在类@Interceptors({MyInterceptor1.class, MyInterceptor2.class}) public void myMethod() { // do intercepted stuff } 中实例化public class MyInterceptor1 { @AroundInvoke public void someMethod(InvocationContext ctx) { ... ctx.proceed(); ... } } 对象,这样您在MediaPlayer中不需要上下文。

MJobScheduler

将以上行从类AsyncTask中的方法player = MediaPlayer.create(getApplicatioContext(),R.raw.bensoundfunkyelement); 移到play行之前的类MJobExecutor的方法onStartJob

然后在您的方法MJobScheduler中,删除条件检查mJobExecutor = new MJobExecutor(player)...,因为它始终为false,从而阻止执行其中的代码。

因此,方法play的主体如下:

if(player == null)

您的onStartJob方法如下:

alarmTime = params.getExtras().getString("alarmTime");
value = new ValueExchange();
value.setString(alarmTime);

player = MediaPlayer.create(getApplicatioContext(), R.raw.bensoundfunkyelement);
mJobExecutor = new MJobExecutor(player){

    @Override
    protected void onPostExecute(String s) {
        Toast.makeText(getApplicationContext(),alarmTime+" "+s,Toast.LENGTH_LONG).show();
        jobFinished(params,false);
    }
};
mJobExecutor.execute();
return true;