当用户停止服务时,如何将参数从活动传递到服务
我有一个带有复选框的活动:如果未选中复选框,则停止服务。这是我的活动代码的片段:
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.android.savebattery.SaveBatteryService");
if (*unchecked*){
serviceIntent.putExtra("user_stop", true);
stopService(serviceIntent);
当我停止服务时,我传递一个参数“user_stop”来表示服务是用户停止的,而不是系统(内存不足)。
现在我必须在我的服务的 void onDestroy 中读取变量“user_stop”:
public void onDestroy() {
super.onDestroy();
Intent recievedIntent = getIntent();
boolean userStop= recievedIntent.getBooleanExtra("user_stop");
if (userStop) {
*** notification code ****
但它不起作用!我无法在 onDestroy 中使用 getIntent() !
有什么建议吗?
谢谢西蒙娜
I have an activity with a checkbox: if the chekbox is unchecked then stop the service. this is a snippet of my activity code:
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.android.savebattery.SaveBatteryService");
if (*unchecked*){
serviceIntent.putExtra("user_stop", true);
stopService(serviceIntent);
when I stop the service I pass a parameter "user_stop" to say at the service that has been a user to stop it and not the system (for low memory).
now I have to read the variable "user_stop" in void onDestroy of my service:
public void onDestroy() {
super.onDestroy();
Intent recievedIntent = getIntent();
boolean userStop= recievedIntent.getBooleanExtra("user_stop");
if (userStop) {
*** notification code ****
but it doesn't work! I can't use getIntent() in onDestroy!
any suggestion?
thanks
Simone
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我看到有两种方法可以做到这一点:
第一种方法是一种简单直接的方法。但它不是很灵活。基本上你会
另一种方法是更好的方法,但需要更多代码。
b。创建内部类BroadcastReceiver类:
c.在 onCreate 或 onStart 方法中注册此接收器:
d.从您想要停止服务的任何地方:
请注意,您可以使用此方法通过 Intent 传递任何自定义参数。
I see two ways of doing this:
The first approach is an easy and straightforward way. But it is not very flexible. Basically you do:
The other approach is a better way but requires more code.
b. Create an inner class BroadcastReceiver class:
c. Register this receiver in onCreate or onStart method:
d. From any place you want to stop your service:
Note that you can pass any custom arguments through Intent using this approach.
我不认为依赖 onDestroy() 是一件好事。您可以采取几种方法。
建议绑定服务,以便您可以在 onUnbind 中编写用户停止通知 http ://developer.android.com/guide/topics/fundamentals.html#servlife
其他选项(不确定这是否有效)是将变量发布到 SharedPreferences 并从 onDestroy() 获取它。 [您需要检查这在调试模式或 LogCat 消息中是否有效。
I don't think relying on onDestroy() is a good thing. There are couple of approaches you can take.
suggest to bind the service so that you could write your userstop notification in onUnbind http://developer.android.com/guide/topics/fundamentals.html#servlife
Other option (not sure if this works) is to post your variable to SharedPreferences and obtain it from onDestroy(). [You need to check if this works in debug mode or LogCat messages.
我在我的应用程序中有相同的设置,在我的活动中使用 CheckBoxPreference 并在服务类的 onCreate 中使用 getSharedPreference。选中该复选框将启动该服务。取消选中会停止服务。
监听并处理我的活动中的首选项单击:
在我的服务 onCreate 中获取我的“prefEnable”首选项:
也许在您的 onDestroy 中,您可以重新调整它的用途并说:
I have the same setup in my app using CheckBoxPreference in my activity and getSharedPreference in service class's onCreate. Checking the checkbox starts the service. Unchecking stops the service.
Listen and handle preference click in my Activity:
Get my "prefEnable" preference within my service onCreate:
Perhaps in your onDestroy, you can re-purpose this and say:
// 您可以通过这种简单的方式传递参数:-
// You can pass the parameter in this simple way:-