2017-05-05 92 views
1

我剛開始研究Xamarin,並且無法環繞我的腦袋如何使多個活動具有參考相同的服務實例。如何從Android中的多個活動中調用相同的服務實例

我從MainActivity開始KeyPressedReceiver並開始聆聽按下電源按鈕。

當三次點擊正在進行時,我打電話給服務方法InitCancelActivity,它開始播放mp3文件並打開CancelActivity

CancelActivity有一個文本字段和一個按鈕。當用戶按下此按鈕時,我希望文本字段的值傳遞給GeneralService方法KillAlert

問題是如何引用GeneralService(已創建)的實例從CancelActivity,所以我可以撥打KillAlert

而這部分

if (_service == null) 
    _service = new GeneralService(); 

看起來絕對錯誤的。我是否應該在MainActivity中將它實例化並傳遞給KeyPressedReceiver構造函數?

[Activity(Label = "TTTT", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{ 
    KeyPressedReceiver receiver; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     SetContentView(Resource.Layout.Main); 

     receiver = new KeyPressedReceiver(); 
     RegisterReceiver(receiver, new IntentFilter(Intent.ActionScreenOn)); 
    } 
} 


[BroadcastReceiver(Enabled = true)] 
public class KeyPressedReceiver : BroadcastReceiver 
{ 
    private GeneralService _service; 
    private int _clicks = 0; 
    public override void OnReceive(Context context, Intent intent) 
    { 
     if (_service == null) 
      _service = new GeneralService(); 

     _clicks++; 

     if (_clicks > 5) 
     { 
      _service.InitCancelActivity(); 
     } 
    } 
} 


[Service(Name = "com.ff.GeneralService")] 
public class GeneralService : Service { 
    private readonly Android.Media.MediaPlayer _player; 

    public GeneralService() 
    { 
     _player = new Android.Media.MediaPlayer(); 
    } 

    public void RaiseAlert() 
    { 
     // start playing .mp3 file 
    } 

    public void KillAlert(string pass) 
    { 
     // stop playing .mp3 file 
    } 

    public void InitCancelActivity() 
    { 
     this.RaiseAlert(); 

     var i = new Intent(this, typeof(CancelActivity)); 
     i.SetFlags(ActivityFlags.NewTask); 
     this.StartActivity(i); 
    } 
} 

[Activity(Label = "CancelActivity")] 
public class CancelActivity : Activity 
{ 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.cancel); 

     this.FindViewById(Resource.Id.cancelButtonYes).Click += delegate 
     { 
      var password = this.FindViewById(Resource.Id.cancelPassword); 

      // call KillAlert method from GeneralServic 
     }; 
    } 
} 

回答

0

創建靜態GeneralService實例並在取消活動中使用。 例如

[Service(Name = "com.ff.GeneralService")] 
public class GeneralService : Service { 
    private readonly Android.Media.MediaPlayer _player; 
    public static generalService; 
    public GeneralService() 
    { 
     _player = new Android.Media.MediaPlayer(); 
     generalService=this 
    } 

    public void RaiseAlert() 
    { 
     // start playing .mp3 file 
    } 

    public void KillAlert(string pass) 
    { 
     // stop playing .mp3 file 
    } 

    public void InitCancelActivity() 
    { 
     this.RaiseAlert(); 

     var i = new Intent(this, typeof(CancelActivity)); 
     i.SetFlags(ActivityFlags.NewTask); 
     this.StartActivity(i); 
    } 
} 

和CancelActivity使用像例如

[Activity(Label = "CancelActivity")] 
public class CancelActivity : Activity 
{ 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.cancel); 

     this.FindViewById(Resource.Id.cancelButtonYes).Click += delegate 
     { 
      var password = this.FindViewById(Resource.Id.cancelPassword); 

      // call KillAlert method from GeneralServic 
      GeneralService.generalService.KillAlert(password.TEXT); 

     }; 
    } 
} 
+0

這是我現在有以下,但由於某種原因,它似乎是錯誤的。但它可能只是我 –

相關問題