第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

getApplicationContext() 不適用于為 JobService

getApplicationContext() 不適用于為 JobService

慕桂英546537 2021-08-25 17:40:03
我正在使用JobSchedulerwhichAsyncTask用于其JobService. 在MJobExecutor擴(kuò)展AsyncTask使用MediaPlayer哪個(gè)需要getApplicationContext()作為參數(shù)的類中不起作用。它顯示無法解析方法。public class MJobExecutor extends AsyncTask<Void,Void,String> {ValueExchange value;MediaPlayer player;@Overrideprotected 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;    }}}
查看完整描述

2 回答

?
慕姐8265434

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊

您不能getApplicationContext()從 an 內(nèi)部調(diào)用,AsyncTask因?yàn)樵摲椒ㄊ窃贑ontextclass 而不是在 class 中定義的AsyncTask。僅供參考,您可以在Activity或Service或其子類中使用此方法,因?yàn)檫@些類是Context.


為了解決您的問題,您需要通過構(gòu)造函數(shù)或 setter傳遞一個(gè)Context對(duì)象或一個(gè)MediaPlayer對(duì)象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;

    }


}


查看完整回答
反對(duì) 回復(fù) 2021-08-25
  • 2 回答
  • 0 關(guān)注
  • 433 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)