gpt4 book ai didi

android - 警报没有唤醒我的服务

转载 作者:太空狗 更新时间:2023-10-29 16:20:38 26 4
gpt4 key购买 nike

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

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

警报管理器

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

权限和其他设置 list

<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>

编辑:这应该有效,但不适合我?

public class PullPendingRequests extends IntentService implements LocationListener {

private static final String NAME = "com.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() 结束。您将需要直接或通过 using my WakefulIntentService 使用 WakeLock .

关于android - 警报没有唤醒我的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15905099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com