如何在Android中使用广播接收器停止正在运行的服务 [英] How to stop a running service using broadcast receiver in android
问题描述
我认为我的代码中有一个简单的问题,但我无法弄清楚.我有一个HeaderService,它在某些特定的应用程序上显示气泡弹出窗口.现在我想要的是当用户使用BroadCastReceiver按Home键或Back键时停止该服务.请看一下我的代码,并向我提出一些解决方案.在此先感谢!
这是我的Receiver类
公共类PopupClosingReceiver扩展了BroadcastReceiver {私人布尔值isMyServiceRunning(Context con){ActivityManager管理器=(ActivityManager)con.getSystemService(Context.ACTIVITY_SERVICE);对于(ActivityManager.RunningServiceInfo服务:manager.getRunningServices(Integer.MAX_VALUE)){如果(HeaderService.class.getName().equals(service.service.getClassName())){返回true;}}返回false;}@Overridepublic void onReceive(Context context,Intent intent){if(isMyServiceRunning(context)){意图serviceIntent =新的Intent(上下文,HeaderService.class);context.stopService(serviceIntent);}}
AndroidManifest.xml
< receiver android:name =.PopupClosingReceiver"><意图过滤器>< category android:name ="android.intent.category.HOME"/></intent-filter></receiver>
您可以随时访问该服务,因为该服务已在系统中注册(线程"),等待激活或随时停止./p>
示例:
公共类Phonecall扩展了BroadcastReceiver {@Overridepublic void onReceive(Context context,Intent intent){Intent song_wweather = new Intent(context,Song_weather.class);//服务类别if(intent.getAction()=="android.intent.action.PHONE_STATE"){TelephonyManager telephonyManager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);PhoneStateListener callStateListener = new PhoneStateListener(){公共无效的onCallStateChanged(int状态,字符串incomingNumber){if(state == TelephonyManager.CALL_STATE_RINGING){context.stopService(song_wweather);}if(state == TelephonyManager.CALL_STATE_IDLE){context.startService(song_wweather);}}};telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);}}}
I think there is a simple problem in my code but I couldn't figure it out. I have a HeaderService which shows a bubble popup over some particular apps. Now what I want is stop that service when user presses Home key or Back key using BroadCastReceiver. Please take a look at my code and suggest me some solution. Thanks in Advance!!
This is my Receiver class
public class PopupClosingReceiver extends BroadcastReceiver {
private boolean isMyServiceRunning(Context con) {
ActivityManager manager = (ActivityManager) con.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (HeaderService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
public void onReceive(Context context, Intent intent) {
if(isMyServiceRunning(context)){
Intent serviceIntent = new Intent(context, HeaderService.class);
context.stopService(serviceIntent);
}
}
AndroidManifest.xml
<receiver android:name=".PopupClosingReceiver">
<intent-filter>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
You can access the service at any time because the service is registered in the system ("Thread") wait to be activated or stops at any time.
Example:
public class Phonecall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent song_wweather= new Intent(context,Song_weather.class); //service class
if(intent.getAction()=="android.intent.action.PHONE_STATE"){
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING){
context.stopService(song_wweather);
}
if(state==TelephonyManager.CALL_STATE_IDLE){
context.startService(song_wweather);
}
}
};
telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}}
}
这篇关于如何在Android中使用广播接收器停止正在运行的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!