警报不会唤醒我的服务

编程入门 行业动态 更新时间:2024-10-12 03:27:01
本文介绍了警报不会唤醒我的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码,我希望无论电话状态如何,此警报都会调用我的服务.即使它处于睡眠模式,我也需要它来访问互联网并拨打一些网络电话.

I have the following code, I expect that this alarm invokes my Service regardless of the state of the phone. Even if its in sleep mode, I need it to access the internet and make some network calls.

为什么手机处于睡眠模式时不工作?

Why doesn't it work when the phone is in sleep mode ?

报警管理器

Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 5); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); Intent notifyintent = new Intent(this, OnAlarmReceiver.class); notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); notifyintent.setAction("android.intent.action.NOTIFY"); PendingIntent notifysender = PendingIntent.getBroadcast(this, 0, notifyintent, PendingIntent.FLAG_UPDATE_CURRENT); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20 * 1000, notifysender);

报警接收器

public class OnAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // PullPendingRequests.acquireStaticLock(context) context.startService(new Intent(context, PullPendingRequests.class)); } }

PullPendingRequests、PendingIntent

public class PullPendingRequests extends IntentService public PullPendingRequests() { super("PullPendingRequests"); me = this ; } @Override final protected void onHandleIntent(Intent intent) { Location myLocation; Log.d("Taxeeta:PullPendingRequets", "Started Location"); if (God.locationManager == null) God.locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); myLocation = God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (myLocation != null) onLocationChanged(myLocation); else { God.notifications.setSpeedNotification(); } Log.d("Taxeeta:PullPendingRequets", "Ended Location"); }

权限和其他设置清单

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_LOGS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> --> <service android:name="com.taxeeta.PullPendingRequests" android:enabled="true" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.Light.NoTitleBar" /> <receiver android:name="com.taxeeta.support.OnAlarmReceiver" android:exported="true" > <intent-filter> <action android:name="android.intent.action.NOTIFY" /> </intent-filter> </receiver>

编辑:如果这行得通,它不适合我吗?

Edit : Should this work, it isn't for me ?

public class PullPendingRequests extends IntentService implements LocationListener { private static final String NAME = "commonsware.cwac.wakeful.WakefulIntentService"; private static volatile PowerManager.WakeLock lockStatic = null; private static PowerManager.WakeLock lock; synchronized private static PowerManager.WakeLock getLock(Context context) { if (lockStatic == null) { PowerManager mgr = (PowerManager) context.getSystemService(Context.POWER_SERVICE); lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME); lockStatic.setReferenceCounted(true); } return (lockStatic); } @Override final protected void onHandleIntent(Intent intent) { try { lock = getLock(this.getApplicationContext()); lock.acquire(); Location myLocation; Log.d("Taxeeta:PullPendingRequets", "Started Location"); if (God.locationManager == null) God.locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); myLocation = God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (myLocation != null) onLocationChanged(myLocation); else { God.notifications.setSpeedNotification(); } Log.d("Taxeeta:PullPendingRequets", "Ended Location"); } finally { if (lock.isHeld()) { lock.release(); } } }

推荐答案

为什么手机处于睡眠模式时不工作?

Why doesn't it work when the phone is in sleep mode ?

因为 startService() 是异步的,Android 只保证 _WAKEUP 警报将使设备保持唤醒状态,直到 onReceive() 结束.您将需要使用 WakeLock,直接或通过使用我的 WakefulIntentService>.

Because startService() is asynchronous, and Android only guarantees that a _WAKEUP alarm will keep the device awake until onReceive() ends. You will need to use a WakeLock, either directly or by means of using my WakefulIntentService.

更多推荐

警报不会唤醒我的服务

本文发布于:2023-11-27 11:32:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!