Android 在服务中调用AlarmManager

Android 在服务中调用AlarmManager,android,broadcastreceiver,alarmmanager,Android,Broadcastreceiver,Alarmmanager,上下文:我试图通过AlarmManager实现一个服务调用另一个服务(第一个服务的子类)。为此,我需要创建一个广播接收器作为一个内部类,如这里所建议的()。但是,第一个服务似乎从未成功呼叫第二个服务,或者第二个服务没有接收呼叫 我环顾四周,发现其他人也有类似的问题,但从未得到解决: 第一个服务中调用第二个服务的代码很简单: private final long ONE_MINUTE = 60*1000*1; Context theContext = getBaseContext();

上下文:我试图通过AlarmManager实现一个服务调用另一个服务(第一个服务的子类)。为此,我需要创建一个广播接收器作为一个内部类,如这里所建议的()。但是,第一个服务似乎从未成功呼叫第二个服务,或者第二个服务没有接收呼叫

我环顾四周,发现其他人也有类似的问题,但从未得到解决:

第一个服务中调用第二个服务的代码很简单:

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);
我的第二项服务是:

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

已识别我的第二次服务清单:

<service
        android:name=".LocalWordSubClass"
        android:label="LocalWordSubClass" >
    </service>
MainActivity的AlarmManager是:

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);
我是否未能实现一种方法

嗯,
LocalWordSubClass
似乎除了继承的方法之外,没有其他方法

此外,您正试图将广播
意图
发送到
服务
(通过您的
挂起内容
),但该功能不起作用

而且,您在
LocalWordSubClass
中定义的
BroadcastReceiver
未使用

下一步,来自:

两分钟后,我有了一个新的子类(可以访问LocationClient)的初始服务调用

您的“新子类”将拥有自己的
mLocationClient
,与流程中任何其他
服务的
mLocationClient
无关

您似乎正在尝试实现我在中所写的内容:

@jsc123:“您建议我如何在轮询位置之前给LocationClient两分钟的连接时间?”--您可以使用AlarmManager,调用set()在两分钟内获得控制权


我的意思是,您将使用
AlarmManager
上的
set()
getService()
pendingent
将控制权传递回您已经运行的
服务,并使用它已经获取的
WakeLock
,以便它可以获取位置,释放
WakeLock
,和
stopSelf()

@CommmonsWare,我仍然不确定如何实施您的建议。即使我将PendingEvent更改为getService(如下所示:Intent I=new Intent(context,LocalWordService.class);PendingEvent thePending=PendingEvent.getService(context,0,I,PendingEvent.FLAG_CANCEL_CURRENT);),我也不明白服务将如何接收呼叫,此外,我还知道如何避免递归死亡陷阱,在这种情况下,服务将始终运行。更清楚地说,我不确定如何让服务在两分钟后识别,因为没有onReceive()方法。因为在该方法或类似方法中,我需要使用位置,断开客户端连接,释放wakelock,然后终止服务。@jsc123:“我不明白服务将如何接收调用”--将使用
onStartCommand()
调用它,通过阅读
getService()的文档可以看出这一点
关于
挂起内容
:我知道这一点。我的意思是,我不确定如何区分一个自引用调用来访问位置信息,以及一个来自Main的调用告诉服务启动一分钟计时器。@jsc123:如果您有一个非
null
LocationClient
,请访问位置,然后将数据成员设置为
null
。如果您的
null
LocationClient
,请初始化客户端并设置计时器。
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);