该服务负责轮询服务器以获取数据,并在满足特定条件时发送警报通知.我不希望服务在活动可见时发送这些通知.
服务是否有办法了解任何特定活动的状态?特别是一项与之相关的活动?
使用清单更新以解决权限问题:
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.interact.Listen.androID.voicemail" androID:versionCode="1" androID:versionname="1.0"> <application androID:icon="@drawable/icon" androID:label="@string/app_name"> <activity androID:name=".MyAppname" androID:label="@string/app_name"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name=".MyObjectDetails"/> <service androID:enabled="true" androID:name=".MyService" /> <receiver androID:name=".MyReceiver"> <intent-filter> <action androID:name="com.foo.bar.EVENT"/> </intent-filter> </receiver> </application> <uses-permission androID:name="androID.permission.INTERNET"></uses-permission></manifest>
我在Logcat中遇到的错误:
09-17 15:33:17.881: WARN/ActivityManager(53): Permission Denial: receiving Intent { act=com.foo.bar.myobject.EVENT (has extras) } to ProcessRecord{43928b40 223:com.foo.bar.myobject/10022} (pID=223,uID=10022) requires com.foo.bar.myobject.EVENT due to sender com.foo.bar.myobject (uID 10022)09-17 15:33:48.901: WARN/ActivityManager(53): Permission Denial: receiving Intent { act=com.foo.bar.myobject.EVENT (has extras) } to com.foo.bar.myobject requires com.foo.bar.myobject.EVENT due to sender com.foo.bar.myobject (uID 10022)
发送广播的任务:
@OverrIDepublic voID onStart(Intent intent,int startID){ super.onStart(intent,startID); serviceHandler = new Handler(); serviceHandler.postDelayed(myTask,100L); Log.d(TAG,"onStart()");}class Task implements Runnable{ public voID run() { Log.i(TAG,"Getting myObjects..."); getMyObjects(); Bundle bundle = new Bundle(); bundle.putLongArray("IDs",getIDsToUpdate()); Intent i = new Intent(); i.setAction(UPDATE_ACTION_STRING); i.putExtras(bundle); sendOrderedbroadcast(i,UPDATE_ACTION_STRING); serviceHandler.postDelayed(this,30000L); } }}解决方法
Is there a way for the Service to kNow the status of any particular activity? Particularly an activity that
is bound to it?
您可以实现某种回调系统.
但是有一种更清洁的方法可以解决这个问题.使用有序广播.活动有一个高优先级的接收器;将raise-a-notification逻辑置于低优先级接收器中.让活动的接收者调用abortbroadcast()来阻止通知.我有一个关于这项技术的blog post.使用事件总线:greenrobot的EventBus,LocalbroadcastManager,基于Rx的事件总线,甚至是mutablelivedata.让服务向总线发送消息.当UI位于前台时,让UI层注册总线上的事件.如果没有人拿起放在公交车上的事件,请让服务提出通知.
总结以上是内存溢出为你收集整理的android – 如果活动正在运行,则禁止来自服务的通知全部内容,希望文章能够帮你解决android – 如果活动正在运行,则禁止来自服务的通知所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)