Android中的服务停止

时间:2012-11-02 14:05:54

标签: android android-service

我有始终在后台运行的Android服务,以及由始终运行的服务触发的另一项服务,即警报管理服务。

然而,我想用按钮停止服务,我的目标是停止始终运行服务,以便自动停止警报管理器服务。这是正确的观点吗?

我的示例代码如下

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);


        }});

}

}

我一直在运行的服务

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;
}


}

我的Alarmmanager服务

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();
        }
    }
}*/

}

我的BroadCast接收器类

对于NotifyService类     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);

  }

}

对于AndroidScheduledService类

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);
    }


}

}

1 个答案:

答案 0 :(得分:2)

停止服务不会停止警报管理器服务。你必须手动停止它。

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);

停止服务:

  Intent stopServiceIntent = new Intent(getBaseContext(), yourServiceToStop.class);
  getBaseContext().stopService(stopServiceIntent );