标签:android android-alarms android-pendingintent
我有以下代码,我希望无论手机的状态如何,此警报都会调用我的服务.即使它处于睡眠模式,我也需要它来访问互联网并进行一些网络呼叫.
为什么手机处于睡眠模式时不起作用?
报警管理器
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);
AlarmReceiver
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>
编辑:这应该工作,它不适合我吗?
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,android-alarms,android-pendingintent 来源: https://codeday.me/bug/20191008/1874053.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。