部署Android应用程序时服务停止(从设置>>应用程序>> RunningServices中消失)

时间:2013-07-22 13:00:06

标签: android service background

这是我的情况:我有一个服务正在运行,每次部署我的应用程序时,服务都会从settings>>application>>runningService消失(因此,服务未运行)如何设置它以便服务不会消失? 我试过startForeground但它没有用。

AndroidManifest

    <service
        android:name=".service.PhoneCallInOutService"
        android:enabled="true"
        android:exported="false" >
    </service>  

这就是我在Activity中启动服务的方式:

    chkCallsRecord.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean isChecked = chkCallsRecord.isChecked();
            updateBackgroundTasks(isChecked);
        }
    });

实际启动服务的方法:

private void updateBackgroundTasks(boolean start) {
    Intent serviceIntent = new Intent(getApplicationContext(),PhoneCallInOutService.class);             

    if (start) {
        getApplicationContext().startService(serviceIntent);

    } else {
        getApplicationContext().stopService(serviceIntent);
    }
}

这是服务:

public class PhoneCallInOutService extends Service {
    private TelephonyManager telephonyMgr;
    private PhoneCallStateListener pcsListener;
    private OutgoingCallReceiver ocReceiver;        

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

    // Listener
    pcsListener = new PhoneCallStateListener(getApplicationContext(),appDto);
    telephonyMgr = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
    telephonyMgr.listen(pcsListener, PhoneStateListener.LISTEN_CALL_STATE);

    // Receiver
    ocReceiver = new OutgoingCallReceiver(getApplication());
    IntentFilter intentF = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
    getApplicationContext().registerReceiver(ocReceiver, intentF);

    return START_STICKY;
}

    @Override
public void onDestroy() {
        super.onDestroy();      

    // Listener
        telephonyMgr.listen(pcsListener, PhoneStateListener.LISTEN_NONE);

        // Receiver
        getApplicationContext().unregisterReceiver(ocReceiver);     
    }

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

}

非常感谢你。

1 个答案:

答案 0 :(得分:0)

如果通过部署您的意思是尝试启动应用的新版本,那么这实际上是正常和预期的行为。通过部署新构建,您可以替换旧代码(包括服务代码),因此必须首先将其杀死,以避免任何崩溃和其他奇怪现象。因此,您的旧应用程序完全被杀死。然后安装新的应用程序,并且通常是自动启动的。应用程序创建的数据通常会保留,但这也是正常的。

修改

出于安全原因,您不得在更新后重新启动。用户必须这样做。至于“他/她可能认为服务仍在运行,这不是真的”,请使用“On Going”类型的通知来指示正在运行的服务