报警不会唤醒了我的服务 [英] Alarm Does Not Wake up my Service

查看:176
本文介绍了报警不会唤醒了我的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,我希望不管是手机的这种状态报警调用我的服务。即使它在睡眠模式下,我需要它来访问互联网,并提出一些网络电话。

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

报警管理

 日历CAL = Calendar.getInstance();
        cal.add(Calendar.SECOND,5);
        AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
        意图notifyintent =新的意图(这一点,OnAlarmReceiver.class);
        notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        notifyintent.setAction(android.intent.action.NOTIFY);
        PendingIntent notifysender = PendingIntent.getBroadcast(此,0,notifyintent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),20 * 1000,
                notifysender);
 

AlarmReceiver

  OnAlarmReceiver延伸的BroadcastReceiver {public类
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        // PullPendingRequests.acquireStaticLock(上下文)
        context.startService(新意图(背景下,PullPendingRequests.class));
    }
}
 

PullPendingRequests,PendingIntent

 公共类PullPendingRequests扩展IntentService

公共PullPendingRequests(){
        超级(PullPendingRequests);
        我=这一点;
    }

@覆盖
    最后的保护无效onHandleIntent(意向意图){

        地点myLocation;
        Log.d(Taxeeta:PullPendingRequets,开始位置);
        如果(God.locationManager == NULL)
            God.locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
        myLocation = God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        如果(myLocation!= NULL)
            onLocationChanged(myLocation);
        其他 {
            God.notifications.setSpeed​​Notification();
        }

        Log.d(Taxeeta:PullPendingRequets,端位置);
    }
 

清单的权限和其他设置

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
    <使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_WIFI_STATE/>
    <使用-权限的Andr​​oid:名称=android.permission.CALL_PHONE/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
    <使用-权限的Andr​​oid:名称=android.permission.READ_LOGS/>
    <使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK/>
    !<  - <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_BOOT_COMPLETED/> - >
 <服务
        机器人:名称=com.taxeeta.PullPendingRequests
        机器人:启用=真
        机器人:标签=@字符串/ APP_NAME
        机器人:screenOrientation =画像
        机器人:主题=@安卓风格/ Theme.Light.NoTitleBar/>

<接收器
            机器人:名称=com.taxeeta.support.OnAlarmReceiver
            机器人:出口=真正的>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.NOTIFY/>
            &所述; /意图滤光器>
        < /接收器>
 

修改:若本作品,它不适合我。

 公共类PullPendingRequests扩展IntentService实现LocationListener的{

    私有静态最后字符串名称=com.commonsware.cwac.wakeful.WakefulIntentService;
    私有静态挥发PowerManager.WakeLock lockStatic = NULL;
    私有静态PowerManager.WakeLock锁;

    同步私有静态PowerManager.WakeLock GETLOCK(上下文的背景下){
        如果(lockStatic == NULL){
            PowerManager的经理=(电源管理器)context.getSystemService(Context.POWER_SERVICE);

            lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,名);
            lockStatic.setReferenceCounted(真正的);
        }

        返回(lockStatic);
    }

    @覆盖
    最后的保护无效onHandleIntent(意向意图){
        尝试 {
            锁定= GETLOCK(this.getApplicationContext());
            lock.acquire();
            地点myLocation;
            Log.d(Taxeeta:PullPendingRequets,开始位置);
            如果(God.locationManager == NULL)
                God.locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
            myLocation = God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            如果(myLocation!= NULL)
                onLocationChanged(myLocation);
            其他 {
                God.notifications.setSpeed​​Notification();
            }

            Log.d(Taxeeta:PullPendingRequets,端位置);
        } 最后 {
            如果(lock.isHeld()){
                lock.release();
            }
        }
    }
 

解决方案
  

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

由于 startService()是异步的,而Android只能保证一个 _WAKEUP 报警器件保持清醒,直到的onReceive()结束。您将需要一个 WakeLock ,直接或通过方式使用使用我的 WakefulIntentService

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 ?

Alarm Manager

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

Manifest for permissions and other settings

<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 = "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 ?

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.

这篇关于报警不会唤醒了我的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆