我是从服务器接收到一些数据时启动的一项活动。需要明确的是,当驾驶员有新工作时,应用程序中会弹出屏幕。有没有办法用声音通知用户这个屏幕。
活动代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Android.App;
using Android.Content;
using Android.Media;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using NorthStar.Driver.Application;
using TheNorthStar.Api.Requests;
using TheNorthStar.Api.Results;
namespace NorthStar.Driver
{
[Activity(Label = "New Work Available")]
public class NewWorkAvailableActivity : Activity
{
public string driverId;
public string bookingId;
public string fullAddress;
public string customerPhoneNumber;
private Timer jobTimer;
private int timerCount = 9;
private TextView jobTimerLabel;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.NewJobAvailable);
fullAddress = Intent.GetStringExtra("fullAddress");
customerPhoneNumber = Intent.GetStringExtra("customerPhoneNumber");
driverId = Intent.GetStringExtra("driverID");
bookingId = Intent.GetStringExtra("bookingId");
TextView jobLocationLabel = FindViewById<TextView>(Resource.Id.newJobLocation);
jobLocationLabel.Text = string.Format("at {0}", Intent.GetStringExtra("jobLocation"));
jobTimerLabel = FindViewById<TextView>(Resource.Id.newJobTimerText);
Button acceptBtn = FindViewById<Button>(Resource.Id.AcceptWorkBtn);
acceptBtn.Click += (sender, args) =>
{
jobTimer.Stop();
AcceptBookingAsync asyncTask = new AcceptBookingAsync(this);
asyncTask.Execute();
};
Button rejectBtn = FindViewById<Button>(Resource.Id.RejectWorkBtn);
rejectBtn.Click += (sender, args) => { Close(); };
jobTimer = new Timer(1000);
jobTimer.Elapsed += (sender, e) =>
{
CheckTimer();
};
jobTimer.Start();
// Create your application here
}
private void Close()
{
jobTimer.Stop();
Intent a = new Intent(this, typeof(MainActivity));
a.PutExtra("driverID", driverId);
a.AddFlags(ActivityFlags.ClearTop);
StartActivity(a);
}
private void CheckTimer()
{
if(timerCount == 0)
{
//timeout so return home
Close();
}
else
{
timerCount--;
RunOnUiThread(() => { jobTimerLabel.Text = string.Format("{0} seconds to respond", timerCount); });
}
}
}
}
我试过这个但是当屏幕被调用时它不起作用。
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.NewJobAvailable);
string ns = Context.NotificationService;
NotificationManager notificationManager = NotificationManager.FromContext(ApplicationContext);
Notification notification = new Notification(Resource.Drawable.Icon, "NorthStar Taxi");
notification.Defaults |= NotificationDefaults.All;
Intent notifyIntent = new Intent(this, typeof(NewWorkAvailableActivity));
PendingIntent contentIntent = PendingIntent.GetActivity(this, 0, notifyIntent, 0);
fullAddress = Intent.GetStringExtra("fullAddress");
customerPhoneNumber = Intent.GetStringExtra("customerPhoneNumber");
driverId = Intent.GetStringExtra("driverID");
bookingId = Intent.GetStringExtra("bookingId");