关闭我的应用时服务关闭

时间:2016-10-29 17:06:28

标签: android service android-6.0-marshmallow huawei

我有一个问题因为我试图在我的新华为p9 lite上安装我的应用程序并且一切正常,但在关闭应用程序之后我的服务也应该在后台关闭。我在其他手机上安装了该应用程序并且运行正常。为什么???请帮忙。 P.S:手机上有android 6.0。

这是我的服务代码:

@Nullable
@Override
public IBinder onBind(Intent intent) {

    return null;
}

public int onStartCommand(Intent intent, int flags, int startId) {


    return START_STICKY;
}

@Override
public void onCreate() {
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (!bluetoothAdapter.isEnabled()) {
        bluetoothAdapter.enable();
    }
    Log.e("siemka","tak");
    initBt();
    update();
    try {
        updateWeather();
    } catch (IOException | JSONException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

    ScheduledExecutorService updateTimeEveryMinute = Executors.newScheduledThreadPool(1);
    updateTimeEveryMinute.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            Log.e("updatuhe","yes");
            triedToConnect = false;
            update();




        }
    }, 0, 5, TimeUnit.SECONDS);

    ScheduledExecutorService checkWeather = Executors.newScheduledThreadPool(1);
    checkWeather.scheduleAtFixedRate(new Runnable(){
        @Override
        public void run(){
            try {
                updateWeather();
            } catch (IOException | ExecutionException | InterruptedException | JSONException e) {
                e.printStackTrace();
            }

        }
    },0,900,TimeUnit.SECONDS);

}


public void manageConnection(BluetoothSocket bluetoothSocket) {
    try {
        inputStream = bluetoothSocket.getInputStream();
        outputStream = bluetoothSocket.getOutputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Intent updateTimeService = new Intent(this, UpdateTimeService.class);
    startService(updateTimeService);
    //updateTime();
}

public void write(final byte[] data) {
    AsyncTask asyncTask = new AsyncTask() {
        @Override
        protected Object doInBackground(Object[] params) {
            try {
                outputStream.write(data);
            } catch (IOException e) {
                if(!triedToConnect) {
                    triedToConnect = true;
                    initBt();
                    update();

                }
                e.printStackTrace();
            }
            return null;
        }
    };
    asyncTask.execute();

}

public boolean initBt() {
    Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
    if (devices.size() > 0) {
        Log.e("jest", "cos tam");
        for (BluetoothDevice device : devices) {
            Log.e("Device ", "name: " + device.getName() + " adress: " + device.getAddress());
            if (device.getAddress().equals(WATCH_ADRESS)) {
                Accept connect = new Accept();
                connect.execute();
                return true;
            }

        }
    }
    return false;
}

private class Accept extends AsyncTask {

    Accept() {

        BluetoothDevice device = bluetoothAdapter.getRemoteDevice(WATCH_ADRESS);

        try {
            Method m = device.getClass().getMethod("createRfcommSocket", int.class);
            bluetoothSocket = (BluetoothSocket) m.invoke(device, 1);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        Log.e("socket", bluetoothSocket.getRemoteDevice().toString());
        Log.e("device", device.toString());

    }

    @Override
    protected Object doInBackground(Object[] params) {
        bluetoothAdapter.cancelDiscovery();

        try {
            bluetoothSocket.connect();
        } catch (IOException e) {
            try {
                bluetoothSocket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            Log.e("reror", e.getMessage());
        }
        manageConnection(bluetoothSocket);
        return null;
    }


}

public void update() {
    Calendar calendar = Calendar.getInstance();
    Integer hour = calendar.get(Calendar.HOUR_OF_DAY);
    Integer minute = calendar.get(Calendar.MINUTE);
    Integer second = calendar.get(Calendar.SECOND);
    Integer day = calendar.get(Calendar.DAY_OF_MONTH);
    Integer month = calendar.get(Calendar.MONTH);
    Integer year = calendar.get(Calendar.YEAR);
    String[] projection = {CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE};
    String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE;

    Cursor calls = null;

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
        calls = this.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, where, null, null, null);
    }
    if(calls != null) {
        calls.moveToFirst();
        Integer missedCount = calls.getCount();
        write((String.valueOf(missedCount) + ";Wieprzfon;" + String.valueOf(hour) + ":" + String.valueOf(minute) + ":" + String.valueOf(second) + ";" +
                String.valueOf(day) + ":" + String.valueOf(month) + ":" + String.valueOf(year)+ ";" + String.valueOf(temperature)+"C;").getBytes());
    }
    if(calls!=null) {
        calls.close();
    }


}

@Override
public void onDestroy(){
    inputStream = null;
    outputStream = null;
    bluetoothSocket = null;
    timesRunCount = 0;
}

public void updateWeather() throws IOException, JSONException, ExecutionException, InterruptedException {
    GetJsonFromUrl getWeather = new GetJsonFromUrl("http://api.openweathermap.org/data/2.5/weather?q=Warsaw,pl&units=metric&appid=3b8a02f1272a46f7a0167eee20c3ad08");
    JSONObject weatherJson = getWeather.execute().get();
    JSONObject mainValuesObject = (JSONObject) weatherJson.get("main");
    temperature = (int) Math.round(mainValuesObject.getDouble("temp"));
    }






}

0 个答案:

没有答案