代码之家  ›  专栏  ›  技术社区  ›  Donal Rafferty

安卓-如何确保我的服务重新启动?

  •  1
  • Donal Rafferty  · 技术社区  · 14 年前

    我有一个后台运行服务的Android应用程序。

    然而,服务从来没有真正重新启动,我可以看到Android计划重新启动,但它确实发生了。

    我的代码如下:


    @Override
    public void onCreate() {
        String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) getSystemService(ns);
        mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        audio_service = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    
    
    }
    
    // This is the old onStart method that will be called on the pre-2.0
    // platform.  On 2.0 or later we override onStartCommand() so this
    // method will not be called.
    @Override
    public void onStart(Intent intent, int startId) {
        handleCommand(intent);
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handleCommand(intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }
    
     void handleCommand(Intent intent) {
    
            running = true;
            mTelephonyManager.listen(new IncomingListener(), PhoneStateListener.LISTEN_CALL_STATE);
    
        }
    

    1 回复  |  直到 14 年前
        1
  •  2
  •   Vadim Kotov First Zero    7 年前

    你用过吗 startForeground() ?