Android服务暂停在某些设备上

时间:2016-04-22 22:04:09

标签: android android-service android-6.0-marshmallow

我的应用使用了我设计的服务。问题是该应用程序在某些手机(如金立,华硕手机)上运行良好,但不适用于Nexus,One Plus或Moto。在这些设备上,服务暂停最小化应用程序。这些是我相应的课程。

public class Sync extends AppCompatActivity {
Button listen;
Button stopListen;

SyncService syncService;
boolean syncServiceBound = false;


/* CreateActivity */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_discoverer);

    /*
    Initialise
     */

    listen = (Button)findViewById(R.id.btn_listen);
    stopListen = (Button)findViewById(R.id.btn_stop_listener);
    /*
     * Check if device is connected via wifi
     */
    ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if(networkInfo.isConnected()){
        displayToast("Connected via wifi");
    }else{
        displayToast("Not connected");
    }


    /**
     * Start broadcasting if device is not already broadcasting
     * Start listening for broadcasts if not already listening
     */

    listen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent syncServiceIntent = new Intent(getBaseContext(), SyncService.class);
            bindService(syncServiceIntent, syncServiceConnection, Context.BIND_AUTO_CREATE);
            startService(syncServiceIntent);
            startPeerListUIThread();
        }
    });

    stopListen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent syncServiceIntent = new Intent(getBaseContext(), SyncService.class);
            if(syncServiceBound) {
                unbindService(syncServiceConnection);
            }
            syncServiceBound = false;
            stopService(syncServiceIntent);
            stopPeerListUIThread();
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
protected void onDestroy(){
    unbindService(syncServiceConnection);
    super.onDestroy();
}

private ServiceConnection syncServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
                                   IBinder service) {
        SyncService.SyncServiceBinder binder = (SyncService.SyncServiceBinder) service;
        syncService = binder.getService();
        syncServiceBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        syncServiceBound = false;
    }
};

}

public class SyncService extends Service {

private final IBinder syncServiceBinder = new SyncServiceBinder();

public SyncService() {
    // initialise 
}

@Override
public void onCreate() {
}


@Override
public int onStartCommand(Intent intent, int flags, int startId){
    // do something
    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return syncServiceBinder;
}


@Override
public boolean stopService(Intent name) {
    // do something 
    return super.stopService(name);
}

@Override
public void onDestroy() {
// do something
    super.onDestroy();
}

public class SyncServiceBinder extends Binder {
    SyncService getService() {
        // Return this instance of SyncService so activity can call public methods
        return SyncService.this;
    }
}


}

最初我认为使用android M是问题(打盹功能可能一直在干扰)但显然并非如此。 任何帮助将不胜感激。

0 个答案:

没有答案