android - 来自另一个 Activity 的 StopService 不起作用?

标签 android service android-activity

我知道已经有很多关于这个的问题,但它似乎不起作用,所以基本上我有一个自定义服务,当它里面的计时器到期时服务工作正常,并调用 stopservice 方法。但是,当我尝试停止另一项 Activity 的服务时,它不起作用吗?

自定义服务代码:

public class ConnectionService extends Service {

Intent service_intent;

Timer t;
int expiry_time = 0;

int timer_tick_starter = 0;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    t = new Timer();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    try {

        service_intent = intent;

        Log.d("onConnectionService", "onStartCommand");

        expiry_time = Integer.parseInt(intent.getStringExtra("expiry"));

        //convert the minutes to milliseconds
        timer_tick_starter = (expiry_time * 60000);

        Log.d("onConnectionService", "Expiry Time (in milliseconds) " + timer_tick_starter);


    } catch (Exception e) {

        Log.d("onConnectionService", e.getMessage().toString());

    }

    //timer will only start to run on the expiry_time which will then close the connection
    t.schedule(new TimerTask() {
        @Override
        public void run() {

            Log.d("onConnectionService", "inside expiry time");
            //close the connection

            stopService(service_intent);

                       }
    }, 30000);


    return START_NOT_STICKY;
}

@Override
public boolean stopService(Intent name) {

    Log.d("onConnectionService", "StopService called");
    String message = name.getStringExtra("msg");
    //broadcast sender
    Intent intent = new Intent();
    intent.putExtra("msg", message);
    intent.setAction("fi.android.inetkeyapplication.CUSTOM_INTENT");
    intent.putExtra("is_selected", UserConnectionState.getInstance().is_selected);

    sendBroadcast(intent);

    Log.d("onConnectionService", "Service has been killed");

    return super.stopService(name);
}

@Override
public void onDestroy() {

    Log.d("onConnectionService", "Destroy called");

}

here I start the service from onCreate:

  Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {

            Log.d("onConfigurationActivity", "Service starting ");
            // Intent i = new Intent(ConfigurationActivity.this, ConnectionService.class);
            Intent i = new Intent(context, ConnectionService.class);

            i.putExtra("expiry", "1");

            startService(i);

        }
    });
    thread.start();

但是稍后在我的应用程序中,当我试图从我启动它的 Activity 中将其杀死时,它不起作用:

  Button exit_button = (Button) findViewById(R.id.btnExit);
    connect_button.setTypeface(roboto_light);

    exit_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //do closing call
            response_close = "";


            //need to close service
            //also kill the service
            Log.d("onConfigurationActivity", "killing service");
            Intent intent = new Intent(context, ConnectionService.class);
            intent.putExtra("msg", "You have been disconnected");
            stopService(intent);
    }
    });

我已经尝试了我的类(class) - ConfigurationActivity.this(作为上下文)并且在我使用自定义上下文的那一刻我已经尝试了(this),我只是在 onCreate 中获取它。然而似乎没有任何效果,我错过了什么?

谢谢

最佳答案

好吧,显然停止服务不会停止计时器。所以我只是在定时器上调用了 cancel()

关于android - 来自另一个 Activity 的 StopService 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19971911/

相关文章:

android - 错误 : Program type already present: org. openqa.selenium.WebDriver$ImeHandler

android - 如何在 Appcompat 22 中从汉堡包图标动态更改为向上图标

c# - Windows 服务在 net.tcp 连接上失败,但控制台应用程序工作正常

android - 将一个 Android JSON 应用程序迁移到 Android 中的一个简单应用程序

android - 在两个 Activity android 之间传递可序列化的自定义对象时出错

Android TextView重力

java - Android 按钮 - 单击时触发定时事件

javascript - Angular 服务 Jasmine 测试找不到 $http

tomcat - 无法连接到具有IP地址的远程计算机

安卓.view.InflateException : Binary XML file line #8