0

新しい Huawei p9 lite にアプリをインストールしようとして問題が発生しましたが、すべて正常に動作しましたが、アプリを閉じると、バックグラウンドにあるはずのサービスも閉じます。そのアプリを他の電話にインストールしましたが、問題なく動作しました。どうして???助けてください。PS: その電話には 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"));
    }






}
4

0 に答える 0