C# 如何在Android中从多个活动调用同一服务实例
我刚刚开始研究Xamarin,只是不知道如何使多个活动具有同一服务实例的引用 我正在从C# 如何在Android中从多个活动调用同一服务实例,c#,android,xamarin,xamarin.android,C#,Android,Xamarin,Xamarin.android,我刚刚开始研究Xamarin,只是不知道如何使多个活动具有同一服务实例的引用 我正在从main活动启动按键接收程序,并开始监听电源按钮是否被按下 当进行三次单击时,我调用服务方法InitCancelActivity,它开始播放mp3文件并打开CancelActivity 在CancelActivity中有一个文本字段和一个按钮。当用户按下此按钮时,我希望文本字段中的值传递到GeneralService方法KillAlert 问题是如何从CancelActivity引用GeneralService
main活动启动按键接收程序
,并开始监听电源按钮是否被按下
当进行三次单击时,我调用服务方法InitCancelActivity
,它开始播放mp3文件并打开CancelActivity
在CancelActivity
中有一个文本字段和一个按钮。当用户按下此按钮时,我希望文本字段中的值传递到GeneralService
方法KillAlert
问题是如何从CancelActivity
引用GeneralService
(已创建)的实例,以便调用KillAlert
这部分呢
if (_service == null)
_service = new GeneralService();
看起来完全错了。我应该在main活动中实例化它
并传递给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
};
}
}
创建静态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);
}
}
并在取消活动中使用,如下面的示例所示
[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);
};
}
}
创建静态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);
}
}
并在取消活动中使用,如下面的示例所示
[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);
};
}
}
这就是我现在所拥有的,但出于某种原因,这似乎是错误的。但也许这就是我现在拥有的,但出于某种原因,它似乎是错的。但也许只有我一个人