在活动开始时播放声音

时间:2012-10-24 15:54:44

标签: android

我是从服务器接收一些数据时启动的一项活动。 需要明确的是,当新的工作可用于驱动程序时,屏幕会弹出应用程序。 有没有办法通过声音通知用户这个屏幕。

活动的代码如下

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");

0 个答案:

没有答案