在服务调用AlarmManagerAlarmManager

2023-09-06 00:03:52 作者:教主会发光

背景:我试图实现通过AlarmManager一个服务调用的第二服务(第一的一个子类)。要做到这一点,我需要建立一个广播接收机作为一个内部类,因为这里推荐(服务和一个BroadcastReceiver )。然而,看来该第一服务是从未成功调用第二服务,或所述第二服务未接收呼叫。

我环顾四周SO,发现别人也有类似的问题,这是从来没有得到解决: AlarmManager从服务。

在code在调用第二的第一个服务是根本:

 私人最终长ONE_MINUTE = 60 * 1000 * 1;

 上下文theContext = getBaseContext();
    AlarmManager theService =(AlarmManager)theContext
            .getSystemService(Context.ALARM_SERVICE);

    意图I =新的意图(theContext,LocalWordSubClass.class);

    PendingIntent thePending = PendingIntent.getBroadcast(theContext,0,I,
            PendingIntent.FLAG_CANCEL_CURRENT);

    //两分钟后调用此
    theService.set(AlarmManager.RTC_WAKEUP,SystemClock.elapsedRealtime()+ TWO_MINUTES,thePending);
 

我的第二个服务是:

 公共类LocalWordSubClass扩展LocalWordService {
BroadcastReceiver的mBroadcastReceiver =新的BroadcastReceiver(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        如果(checkIfGooglePlay()&安培;&安培; checkTime()){
            getPostLocation();
        }
        mLocationClient.disconnect(); //连接在第一服务类
        stopSelf();
    }
};
 

}

我的清单公认的第二个服务:

 <服务
        机器人:LocalWordSubClassNAME =
        机器人:标签=LocalWordSubClass>
    < /服务>
 
.NET Core微服务之服务间的调用方式 REST and RPC

问:我是不是不执行的方法?还是我需要的广播接收器添加到清单?我有点困惑,为什么第二类是不被响应。

*的更新:* 的感谢CommonWare的多项建议,我是能够实现连接LocationClient并获得了最后一个位置之间的一分钟过去。但是,该服务不再重复,当由MainActivity的AlarmManager的setRepeating方法要求运行。

这是在code(已成功给locationClient时间来连接)

 如果(mLocationClient == NULL){
    PM =(电源管理器)getBaseContext()getSystemService(Context.POWER_SERVICE)。
    WL = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,);
    wl.acquire();
        initalizeLocks();
        mLocationClient =新LocationClient(这,这,这一点);
    mLocationClient.connect();
        上下文theContext = getBaseContext();

    AlarmManager theService =(AlarmManager)theContext
            .getSystemService(Context.ALARM_SERVICE);

    意图I =新的意图(LocalWordService.this,this.getClass());

    PendingIntent thePending = PendingIntent.getService(此,0,I,
            PendingIntent.FLAG_CANCEL_CURRENT);
    //两分钟后调用此
    theService.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()+ ONE_MINUTE,thePending);
    } 其他 {
        getPostLocation();
        wl.release();
        mLocationClient = NULL;
        stopSelf();
    }
 

该MainActivity的AlarmManager是:

 日历CAL = Calendar.getInstance();

    意向意图=新的意图(这一点,LocalWordService.class);
    PendingIntent pintent = PendingIntent.getService(此,0,意图,0);
    AlarmManager报警=(AlarmManager)getSystemService(Context.ALARM_SERVICE);

    报警。
    setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),EVERY_TWO_MINUTES,pintent);
 

解决方案   

我是不执行的方法?

嗯, LocalWordSubClass 似乎是缺乏方法,不是继承那些其他。

另外,你正试图发送一个广播意图服务(通过你的 PendingIntent ),这是行不通的。

和的的BroadcastReceiver 您已经于 LocalWordSubClass 规定未使用。

接下来,your相关评论:

  

我最初的服务调用一个新的子类(它可以访问LocationClient)两分钟后

您新的子类将拥有自己的 mLocationClient ,无关任何 mLocationClient 从任何其他服务在你的进程。

您似乎在试图实现的东西我在the preceding评论:

  

@ jsc123:你会如何建议我给我LocationClient两分钟的窗口,投票之前连接的位置 - 你可以只使用AlarmManager,调用set()来获得控制在两分钟内。

我的意思说的是,你可以使用设置() AlarmManager 的getService() PendingIntent 来实现控制权交还给您正在运行的服务与它已经获得 WakeLock ,以便它可以得到的位置,松开 WakeLock stopSelf()

Context: I am attempting to implement one service call a second service (a subclass of the first) via an AlarmManager. To do this, I needed to create a Broadcast Receiver as an inner class, as recommended here (Service and a BroadCastReceiver). However, it appears that the first service is never successfully calling the second service, or the second service is not receiving the call.

I have looked around SO and found that someone else had a similar problem that was never resolved: AlarmManager from service.

The code in the first service to call the second is simply:

private final long ONE_MINUTE = 60*1000*1;

 Context theContext = getBaseContext(); 
    AlarmManager theService = (AlarmManager) theContext
            .getSystemService(Context.ALARM_SERVICE);

    Intent i = new Intent(theContext, LocalWordSubClass.class);

    PendingIntent thePending = PendingIntent.getBroadcast(theContext, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);

    //call this after two minutes 
    theService.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+TWO_MINUTES, thePending);

My second service is:

public class LocalWordSubClass extends LocalWordService {
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(checkIfGooglePlay() && checkTime()) {
            getPostLocation();
        }
        mLocationClient.disconnect(); //connected in the first service class
        stopSelf();
    }
};  

}

My manifest with second service recognized:

<service
        android:name=".LocalWordSubClass"
        android:label="LocalWordSubClass" >
    </service>

Question: Am I failing to implement a method? Or did I need to add the Broadcast Receiver to the Manifest? I am a bit confused as to why the second class is not being responsive.

*UPDATE: * Thanks to CommonWare's multiple recommendations, I was able to achieve this one-minute elapse between connecting the LocationClient and getting its last location. However, the service is no longer running on repeat, when called upon by the MainActivity's AlarmManager's setRepeating Method.

This is the code (that is successfully giving the locationClient time to connect)

 if(mLocationClient==null) {
    pm = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();
        initalizeLocks();
        mLocationClient = new LocationClient(this, this, this);
    mLocationClient.connect();
        Context theContext = getBaseContext();

    AlarmManager theService = (AlarmManager) theContext
            .getSystemService(Context.ALARM_SERVICE);

    Intent i = new Intent(LocalWordService.this, this.getClass());

    PendingIntent thePending = PendingIntent.getService(this, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
    //call this after two minutes 
    theService.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONE_MINUTE, thePending);
    } else {
        getPostLocation();
        wl.release();
        mLocationClient = null;
        stopSelf();
    }

The MainActivity's AlarmManager is:

Calendar cal = Calendar.getInstance();

    Intent intent = new Intent(this, LocalWordService.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    alarm.
    setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), EVERY_TWO_MINUTES, pintent);

解决方案

Am I failing to implement a method?

Well, LocalWordSubClass seems to be devoid of methods, other than inherited ones.

Also, you are attempting to send a broadcast Intent to a Service (via your PendingIntent), which is not going to work.

And, the BroadcastReceiver you have defined in LocalWordSubClass is unused.

Next, from your related comment:

I have the initial service call a new subclass (which has access to the LocationClient) after a two minute period

Your "new subclass" will have its own mLocationClient, unrelated to any mLocationClient from any other Service in your process.

You appear to be attempting to implement something I wrote about in the preceding comment:

@jsc123: "How would you recommend I give my LocationClient a two minute window to connect before polling the location?" -- you could just use AlarmManager, calling set() to get control in two minutes.

What I meant by that was that you would use set() on AlarmManager with a getService() PendingIntent to deliver control back to your already-running Service with its already-acquired WakeLock, so that it can obtain the location, release the WakeLock, and stopSelf().