亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否可以移動到另一個 Activity 并啟動服務?安卓工作室應用

是否可以移動到另一個 Activity 并啟動服務?安卓工作室應用

胡說叔叔 2023-03-09 14:58:08
我正在嘗試啟動一項服務并打開一個新活動,因此即使我關閉手機,新活動的對象也將始終執行它們的操作。我試過這樣的事情:為了啟動服務,我在MainActivity中這樣寫: startService(new Intent(this, FirstService.class));在第一次服務中我寫道:import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;public class FirstService extends Service {    private MediaPlayer player;    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public void onCreate() {        super.onCreate();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {       intent = new Intent(this, SecondActivity.class);       startActivity(intent, 0);        return START_STICKY; // service will start    }    @Override    public void onDestroy() { //stop the music        super.onDestroy();        intent = new Intent(this, MainActivity.class);        startActivity(intent, 0);    }}這樣做有可能嗎?
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

是的,有可能


從服務類內部:


Intent dialogIntent = new Intent(this, YourActivity.class);

dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(dialogIntent); 


查看完整回答
反對 回復 2023-03-09
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號