销毁活动后服务再次启动

时间:2014-06-02 18:28:45

标签: android android-intent android-service

我在处理项目时发现了一个非常奇怪的事情,我认为这是一个重复的问题link,但我找不到合适的解决方案。我正在使用一个Android应用程序,我在其中创建了一个后台服务。我面临的问题是当我销毁一项启动服务的活动时。它再次运行服务(n显示通知)我不知道为什么。我不知道哪里出错了。任何人都可以通过片段解决问题。 提前致谢。您应该感谢您的帮助。

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

1 个答案:

答案 0 :(得分:0)

好像,您正在onPause开始服务而且从不停止