简体   繁体   English

恢复活动后,服务再次开始

[英]Service Starts Again After Destory An Acticity

I have observed a very strange thing while working on my project I think is a duplicate question link but I didn't find an appropriate solution. 我在进行项目时观察到一个非常奇怪的事情,我认为这是一个重复的问题链接,但是我没有找到合适的解决方案。 I'm working an android application in which I have created a background service. 我正在使用其中创建了后台服务的android应用程序。 The problem I'm facing is when I destroy an activity which starts a service. 我面临的问题是当我销毁启动服务的活动时。 Its run the service again (n show a notification) I don't know why. 它再次运行服务(不显示通知),我不知道为什么。 I don't know where I went wrong. 我不知道哪里出了问题。 Could anyone please go thru the snippet and sort out the problem. 任何人都可以通过摘录来解决问题。 thanks in Advance. 提前致谢。 YOUR help should be appreciated. 您的帮助将不胜感激。

Acitivty: 积极性:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_option_selection);

    cm = new ConnectionManager(getApplicationContext());
    isInternetPresent = cm.isConnected();

    serviceIntent = new Intent(getApplicationContext(),MyService.class);
//      serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//  isMyServiceRunning();
    if(!isMyServiceRunning())
    {
        Toast.makeText(getBaseContext(), "There is no service running, starting service..", Toast.LENGTH_SHORT).show();
        startService(serviceIntent);
    }else
    {
        Toast.makeText(getBaseContext(), "Service is already running", Toast.LENGTH_SHORT).show();          
    }
}

    @Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    isMyServiceRunning();
    if(!isMyServiceRunning())
    {
        Toast.makeText(getBaseContext(), "There is no service running, starting service..",    Toast.LENGTH_SHORT).show();
        startService(serviceIntent);
    }else
    {
        Toast.makeText(getBaseContext(), "Service is already running", Toast.LENGTH_SHORT).show();          
    }
}

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        String temp = service.service.getClassName();
        if ("com.smartclasss.alerts.MyService".equals(temp)) {
            return true;
        }
    }
    return false;
}

SERVICE: 服务:

public class MyService extends Service{
   @Override
public int onStartCommand(Intent intent, int flags, int startId) {

    // TODO Auto-generated method stub
    time = new Timer();
    time.schedule(new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
            final String currentDate = df.format(Calendar.getInstance().getTime());         
            if(flag == false)
            {
                try {
                    savingDateinPref(currentDate);          
                    new DoInBackground().execute(currentDate);  
                    flag = true;
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }

            }
            String val = prefs.getString("TAG_KEY", "defValue");
            if(!currentDate.equals(val))
            {
            flag = false;
            prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
            SharedPreferences.Editor editor = prefs.edit(); 

            editor.remove("TAG_KEY");
             //---saves the values--- 
             editor.commit(); 

            }
        }
    },0,5000);
return START_STICKY;
}
}

似乎您是在onPause中启动服务,而从未停止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM