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