屏幕锁定时Xamarin Android振动不起作用

发布于 2025-02-10 16:02:29 字数 2262 浏览 1 评论 0 原文

当屏幕锁定时,我一直在尝试使我的Android手机振动,以下代码是接收SMS消息并振动。我已经看到屏幕上锁定的多个示例可以振动,但我永远无法正常工作。我是移动应用程序开发的新手。下面的代码在屏幕锁定时适合三星Galaxy Note 9。我在清单中设置了振动和接收SMS的权限。但是当屏幕锁定时,它只是失败。有人可以查看我的代码并在锁定屏幕时振动。

我非常感谢。请发布完整的示例,我可以再次使用我是移动开发的新手。

[BroadcastReceiver(Enabled = true, Label = "SMS Receiver", Exported = true)]
[IntentFilter(new string[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority = (int)IntentFilterPriority.HighPriority)]
public class MySMSReciver : BroadcastReceiver
{ 
    public static readonly String INTENT_ACTION = "android.provider.Telephony.SMS_RECEIVED";
    protected string message, address = "";
    public override void OnReceive(Context context, Intent intent)
    {
        InvokeAbortBroadcast();
        try
        {
            if (intent.Action != INTENT_ACTION) return;

            var bundle = intent.Extras;

            if (bundle == null)
            {
                return;
            }
            var pdus = bundle.Get("pdus").ToArray<Java.Lang.Object>();
            var msgs = new SmsMessage[pdus.Length];
            var sb = new StringBuilder();
            String sender = null;
            String messageBody = null;             
            for (int i = 0; i < msgs.Length; i++)
            {

                msgs[i] = SmsMessage.CreateFromPdu((byte[])pdus[i]);

                messageBody = msgs[i].MessageBody;

                if (sender == null)
                {
                    sender = msgs[i].OriginatingAddress;

                }

                sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress, System.Environment.NewLine, messageBody));

            }

            if (sender != null)
            {
                Toast.MakeText(context, "your SMS Message:" +  sb.ToString(), ToastLength.Long).Show();
                //works well but fails when screen is locked.
                var duration = TimeSpan.FromSeconds(5);
                Xamarin.Essentials.Vibration.Vibrate(duration);
            }
         else
           {
             ClearAbortBroadcast();
            }
        }
        catch (Exception ex)
        {
             Toast.MakeText(context, "Error :" +ex.Message, ToastLength.Long).Show();
        }

     }    
}

I have been trying to get my android phone to vibrate when the screen locked, the code below is to receive sms messages and vibrate. I have seen multiple examples to vibrate while the screen is locked but I could never get it to work. I am new to mobile app development. below the code works well for a Samsung Galaxy note 9 when the screen lock is off. I have the permissions set in the manifest to vibrate and to receive SMS. but it just fails when screen is lock is on. Could somebody look at my code and get it vibrate while the screen is locked.

I would greatly Appreciate it. Please Post full example that I could use once again I am new to mobile development.

[BroadcastReceiver(Enabled = true, Label = "SMS Receiver", Exported = true)]
[IntentFilter(new string[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority = (int)IntentFilterPriority.HighPriority)]
public class MySMSReciver : BroadcastReceiver
{ 
    public static readonly String INTENT_ACTION = "android.provider.Telephony.SMS_RECEIVED";
    protected string message, address = "";
    public override void OnReceive(Context context, Intent intent)
    {
        InvokeAbortBroadcast();
        try
        {
            if (intent.Action != INTENT_ACTION) return;

            var bundle = intent.Extras;

            if (bundle == null)
            {
                return;
            }
            var pdus = bundle.Get("pdus").ToArray<Java.Lang.Object>();
            var msgs = new SmsMessage[pdus.Length];
            var sb = new StringBuilder();
            String sender = null;
            String messageBody = null;             
            for (int i = 0; i < msgs.Length; i++)
            {

                msgs[i] = SmsMessage.CreateFromPdu((byte[])pdus[i]);

                messageBody = msgs[i].MessageBody;

                if (sender == null)
                {
                    sender = msgs[i].OriginatingAddress;

                }

                sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress, System.Environment.NewLine, messageBody));

            }

            if (sender != null)
            {
                Toast.MakeText(context, "your SMS Message:" +  sb.ToString(), ToastLength.Long).Show();
                //works well but fails when screen is locked.
                var duration = TimeSpan.FromSeconds(5);
                Xamarin.Essentials.Vibration.Vibrate(duration);
            }
         else
           {
             ClearAbortBroadcast();
            }
        }
        catch (Exception ex)
        {
             Toast.MakeText(context, "Error :" +ex.Message, ToastLength.Long).Show();
        }

     }    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文