标签:android broadcastreceiver alarmmanager
上下文:我试图通过AlarmManager实现一个服务调用第二个服务(第一个服务的子类).为此,我需要按照此处的建议(Service and a BroadCastReceiver)创建一个广播接收器作为内部类.但是,似乎第一个服务从未成功调用第二个服务,或者第二个服务未接收到该呼叫.
我环顾四周,发现其他人有类似的问题从未得到解决:AlarmManager from service.
第一个调用第二个服务的代码就是:
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>
问题:我未能实施方法吗?或者我是否需要将广播接收器添加到清单中?我有点困惑为什么第二堂课没有回应.
**更新:**感谢CommonWare的多项建议,我能够在连接LocationClient和获取其最后位置之间实现这一分钟.但是,当MainActivity的AlarmManager的setRepeating方法调用时,该服务不再重复运行.
这是代码(成功提供locationClient连接的时间)
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();
}
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);
解决方法:
Am I failing to implement a method?
好吧,LocalWordSubClass似乎没有方法,除了继承的方法.
此外,您正在尝试将广播意图发送到服务(通过您的PendingIntent),这不会起作用.
并且,您在LocalWordSubClass中定义的BroadcastReceiver未使用.
接下来,从your related comment:
I have the initial service call a new subclass (which has access to the LocationClient) after a two minute period
您的“新子类”将拥有自己的mLocationClient,与您进程中任何其他服务的任何mLocationClient无关.
你似乎试图实现我在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.
我的意思是你会在AlarmManager上使用set()和一个getService()PendingIntent,用它已经获得的WakeLock将控制权交还给你已经运行的服务,这样它就可以获得位置,释放WakeLock,和stopSelf().
标签:android,broadcastreceiver,alarmmanager 来源: https://codeday.me/bug/20191003/1848329.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。