停止一次后再次创建Android服务

时间:2014-11-11 06:15:22

标签: android service

我已经定期创建一个简单的服务敬酒。但是当服务再次由自己创建并再次连续启动时停止服务。 以下是我的MainActivity代码。

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Start service using AlarmManager

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);

    Intent intent = new Intent(this, TestService.class);

    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int i;
    i=15;
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            i* 1000, pintent);

    Button startBtn = (Button) findViewById(R.id.startBtn);
    startBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startService(new Intent(getBaseContext(), TestService.class));
        }
    });

    Button stopBtn = (Button) findViewById(R.id.stopBtn);
    stopBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            stopService(new Intent(getBaseContext(), TestService.class));
        }
    });

}

以下是我的服务类

TestService的

public class TestService extends Service {

    @Override
        public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub

        Toast.makeText(getApplicationContext(), "Service Created", 1).show();
        Log.i("TestService", "SERVICE START");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Service Destroy", 1).show();
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        Toast.makeText(getApplicationContext(), "Service Running ", 1).show();

        return super.onStartCommand(intent, flags, startId);
    }
}

当按钮点击停止服务时,我的所有代码一旦停止但又重新创建但是它自己的代码 我做错了什么? 提前谢谢..

4 个答案:

答案 0 :(得分:0)

尝试使用START_STICKY:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    Toast.makeText(getApplicationContext(), "Service Running ", 1).show();

    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

如果仍无效,请尝试返回START_REDELIVER_INTENT而不是START_STICKY

查看此链接以获取更多信息:

START_STICKY and START_NOT_STICKY

答案 1 :(得分:0)

尝试

MainActivity.this

而不是getBaseContext()

答案 2 :(得分:0)

只需使用START_STICKY和START_REDELIVER_INTENT

即可
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    // START_REDELIVER_INTENT
    return START_STICKY;
}

Android SDK文档中的定义。

public static final int START_STICKY

在API级别5中添加 从onStartCommand(Intent,int,int)返回的常量:如果此服务的进程在启动时被终止(从onStartCommand(Intent,int,int)返回后),则将其保持在启动状态但是不要保留这个交付的意图。稍后系统将尝试重新创建服务。因为它处于启动状态,所以它将保证在创建新服务实例后调用onStartCommand(Intent,int,int);如果没有任何挂起的启动命令要传递给服务,则将使用null intent对象调用它,因此您必须注意检查这一点。

此模式适用于将在任意时间段内显式启动和停止运行的内容,例如执行背景音乐播放的服务。

常数值:1(0x00000001)

public static final int START_REDELIVER_INTENT

在API级别5中添加 从onStartCommand(Intent,int,int)返回的常量:如果此服务的进程在启动时被终止(从onStartCommand(Intent,int,int)返回后),则它将被安排为重启并且最后交付的Intent通过onStartCommand(Intent,int,int)再次传递给它。此Intent将保持计划重新传递,直到服务调用stopSelf(int),并将启动ID提供给onStartCommand(Intent,int,int)。该服务不会接收带有null Intent的onStartCommand(Intent,int,int)调用,因为只有在没有完成处理发送给它的所有Intent时才会重新启动它(并且任何此类挂起事件将在重启点)。

常数值:3(0x00000003)

答案 3 :(得分:0)

我已解决了我的问题,因为我的AlarmManager定期向我的服务发送意向请求,因此我停止了服务并且还通过alarm.cancel(pintent)停止了我的AlarmManger; 谢谢你们所有人..

相关问题