服务停止在Android中 [英] Service Stopping in Android
问题描述
我有在后台始终运行Android的服务,并且它是由始终运行服务,这是alarmmanager服务引发的其他服务。
Howewer,我想用一个按钮来停止服务,我的目标是制止始终运行服务,以便报警管理器服务将自动停止。它是正确的观点?
我的示例code是如下:
包com.example.deneme;公共类AndroidNotifyService延伸活动{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 按钮buttonStartService =(按钮)findViewById(R.id.startservice);
按钮buttonStopService =(按钮)findViewById(R.id.stopservice); buttonStartService.setOnClickListener(新Button.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
意向意图=新意图(AndroidNotifyService.this,com.example.deneme.AndroidScheduledService.class);
AndroidNotifyService.this.startService(意向);
}}); buttonStopService.setOnClickListener(新Button.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根 意向意图=新的Intent();
intent.setAction(AndroidScheduledService.ACTION);
intent.putExtra(RQS,AndroidScheduledService.RQS_STOP_SERVICE);
sendBroadcast(意向);
}});}
}
我始终运行服务
包com.example.deneme;
公共类AndroidScheduledService延伸服务{
最终静态字符串ACTION =AndroidScheduledServiceAction;
最终静态字符串STOP_SERVICE =;
最终静态INT RQS_STOP_SERVICE = 1;公众诠释onStartCommand(意向意图,诠释标志诠释startId){
// TODO自动生成方法存根
意图myIntent =新意图(getBaseContext()
MyScheduledReceiver.class); 的PendingIntent的PendingIntent
= PendingIntent.getBroadcast(getBaseContext(),
0,myIntent,0); alarmManager alarmManager
=(AlarmManager)getSystemService(ALARM_SERVICE);
台历挂历= Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis的());
calendar.add(Calendar.SECOND,10);
长间隔= 60 * 1000; //
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),间隔的PendingIntent);
返回super.onStartCommand(意向,旗帜,startId);
}@覆盖
公共无效的onDestroy(){
// TODO自动生成方法存根
//this.unregisterReceiver(notifyServiceReceiver);
意向意图=新的Intent();
intent.setAction(NotifyService.ACTION);
intent.putExtra(RQS,NotifyService.RQS_STOP_SERVICE);
sendBroadcast(意向);
super.onDestroy();
}@覆盖
公众的IBinder onBind(意向为arg0){
// TODO自动生成方法存根
返回null;
}
}
我Alarmmanager服务
公共类NotifyService延伸服务{最终静态字符串ACTION =NotifyServiceAction;
最终静态字符串STOP_SERVICE =;
最终静态INT RQS_STOP_SERVICE = 1;HttpClient的httpclnt;
HttpPost httppst;
字符串消息;
字符串响应;// NotifyServiceReceiver notifyServiceReceiver;私有静态最终诠释MY_NOTIFICATION_ID = 1;
私人NotificationManager notificationManager;
私人通知myNotification;
私人最终字符串myBlog =http://android-er.blogspot.com/;/ *
@覆盖
公共无效的onCreate(){
// TODO自动生成方法存根
notifyServiceReceiver =新NotifyServiceReceiver();
super.onCreate();
}
* /
@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
// TODO自动生成方法存根 / *
IntentFilter的IntentFilter的=新的IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver,IntentFilter的);* / //发送通知
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification =新的通知(R.drawable.ic_launcher,
通知!,
System.currentTimeMillis的());
上下文的背景下= getApplicationContext();
字符串notificationTitle =通知锻炼!
字符串notificationText =http://www.google.com/
意图myIntent =新意图(Intent.ACTION_VIEW,Uri.parse(myBlog));
的PendingIntent的PendingIntent
= PendingIntent.getActivity(getBaseContext(),
0,myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults | = Notification.DEFAULT_SOUND;
myNotification.flags | = Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(背景下,
notificationTitle,
notificationText,
的PendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID,myNotification);
返回super.onStartCommand(意向,旗帜,startId);
}@覆盖
公众的IBinder onBind(意向为arg0){
// TODO自动生成方法存根
返回null;
}
@覆盖
公共无效的onDestroy(){
// TODO自动生成方法存根
//this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}
/ *
@覆盖
公众的IBinder onBind(意向为arg0){
// TODO自动生成方法存根
返回null;
}公共类NotifyServiceReceiver扩展广播接收器{ @覆盖
公共无效的onReceive(上下文为arg0,ARG1意向){
// TODO自动生成方法存根
INT RQS = arg1.getIntExtra(RQS,0);
如果(RQS == RQS_STOP_SERVICE){
stopSelf();
}
}
} * /}
我的广播接收器类
有关NotifyService类
包com.example.deneme;
公共类MyScheduledReceiver扩展广播接收器{@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
// TODO自动生成方法存根意图intent2 =新意图(背景下,com.example.deneme.NotifyService.class);
context.startService(intent2); }}
有关AndroidScheduledService类
包com.example.deneme;
公共类AutoStartNotifyReceiver扩展广播接收器{私人最终字符串BOOT_COMPLETED_ACTION =android.intent.action.BOOT_COMPLETED;@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
// TODO自动生成方法存根 如果(intent.getAction()。等于(BOOT_COMPLETED_ACTION)){
意图myIntent =新意图(背景下,com.example.deneme.AndroidScheduledService.class);
context.startService(myIntent);
}
}}
停止该服务不会停止报警管理器服务。你必须手动停止它。
意向意图=新意图(这一点,com.example.deneme.AndroidScheduledService.class);
发件人的PendingIntent = PendingIntent.getBroadcast(这一点,
0,意图,0);
AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);alarmManager.cancel(寄件人);
要停止服务:
意图stopServiceIntent =新意图(getBaseContext(),yourServiceToStop.class);
。getBaseContext()stopService(stopServiceIntent);
I have Android Service that runs always in background, and another service which is triggered by the always running service which is alarmmanager service.
Howewer, I want to stop service with a button, my aim is to stop always running service so the alarm manager service will automatically be stopped. Is it correct perspective?
My sample code is as follows
package com.example.deneme;
public class AndroidNotifyService extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStartService = (Button)findViewById(R.id.startservice);
Button buttonStopService = (Button)findViewById(R.id.stopservice);
buttonStartService.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(AndroidNotifyService.this, com.example.deneme.AndroidScheduledService.class);
AndroidNotifyService.this.startService(intent);
}});
buttonStopService.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(AndroidScheduledService.ACTION);
intent.putExtra("RQS", AndroidScheduledService.RQS_STOP_SERVICE);
sendBroadcast(intent);
}});
}
}
My always running service
package com.example.deneme;
public class AndroidScheduledService extends Service {
final static String ACTION = "AndroidScheduledServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getBaseContext(),
MyScheduledReceiver.class);
PendingIntent pendingIntent
= PendingIntent.getBroadcast(getBaseContext(),
0, myIntent, 0);
AlarmManager alarmManager
= (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = 60 * 1000; //
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval, pendingIntent);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
//this.unregisterReceiver(notifyServiceReceiver);
Intent intent = new Intent();
intent.setAction(NotifyService.ACTION);
intent.putExtra("RQS", NotifyService.RQS_STOP_SERVICE);
sendBroadcast(intent);
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
My Alarmmanager service
public class NotifyService extends Service {
final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
HttpClient httpclnt;
HttpPost httppst;
String message;
String response;
//NotifyServiceReceiver notifyServiceReceiver;
private static final int MY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";
/*
@Override
public void onCreate() {
// TODO Auto-generated method stub
notifyServiceReceiver = new NotifyServiceReceiver();
super.onCreate();
}
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
/*
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver, intentFilter);
*/
// Send Notification
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!",
System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Exercise of Notification!";
String notificationText = "http://www.google.com/";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
PendingIntent pendingIntent
= PendingIntent.getActivity(getBaseContext(),
0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context,
notificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
//this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}
/*
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public class NotifyServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int rqs = arg1.getIntExtra("RQS", 0);
if (rqs == RQS_STOP_SERVICE){
stopSelf();
}
}
}*/
}
My BroadCast Receiver Classes
For NotifyService Class package com.example.deneme;
public class MyScheduledReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(context, com.example.deneme.NotifyService.class);
context.startService(intent2);
}
}
For AndroidScheduledService Class
package com.example.deneme;
public class AutoStartNotifyReceiver extends BroadcastReceiver {
private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
Intent myIntent = new Intent(context, com.example.deneme.AndroidScheduledService.class);
context.startService(myIntent);
}
}
}
Stopping the service won't stop the Alarm manager service. You have to stop it manually.
Intent intent = new Intent(this, com.example.deneme.AndroidScheduledService.class);
PendingIntent sender = PendingIntent.getBroadcast(this,
0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
To stop a service:
Intent stopServiceIntent = new Intent(getBaseContext(), yourServiceToStop.class);
getBaseContext().stopService(stopServiceIntent );
这篇关于服务停止在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!