【问题标题】:Service Starts Again After Destory An Acticity销毁活动后服务再次启动
【发布时间】:2014-06-02 18:28:44
【问题描述】:

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

活动:

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

服务:

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

【问题讨论】:

    标签: android android-intent android-service


    【解决方案1】:

    看来,您正在onPause 启动您的服务并且永远不会停止它

    【讨论】:

    • 我不想停止我的服务。所以这就是为什么我没有调用 stopService 方法
    • 当您销毁 Activity 时,它首先进入 onPause 并显示 Toast。你为什么要在 onPause 启动它你已经在 onCreate 这样做了
    • 我试过这个,但没有用。销毁活动意味着从手机中清除该过程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 2013-06-13
    相关资源
    最近更新 更多