线程中的功能只会带来最后的答案

时间:2019-06-18 08:18:31

标签: android-studio java-threads

我正在android studio中创建一个应用程序,该应用程序一直工作,并按时间检查所有值。但是,当我创建线程时,它只会带来for的最后结果。当我想检查所有。请知道答案,可以举个例子。

@Override

public int onStartCommand(Intent intent, int flags, int startId) {
    dataBaseHelper = new DataBaseHelper(this);
    Thread t = new Thread(){
        @Override
        public void run(){
            try{
                while(!isInterrupted()){
                    Thread.sleep(10000);
                    appCompatActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Cursor cursor = dataBaseHelper.getData();
                            arrayList = new ArrayList<>();
                            while(cursor.moveToNext()) {
                                vol = new Volparametrai(cursor.getString(0), cursor.getString(2), cursor.getString(3),
                                        cursor.getString(4), cursor.getString(5));
                                arrayList.add(vol);
                                Toast.makeText(getApplicationContext(), vol.getDate(), Toast.LENGTH_SHORT).show();
                            }
                            size = arrayList.size();
       //---------------------------------------------------------------
                            int Hour = Calendar.getInstance().getTime().getHours();
                            int Minute = Calendar.getInstance().getTime().getMinutes();
                            int Year = Calendar.getInstance().getTime().getYear();
                            int Month = Calendar.getInstance().getTime().getMonth();
                            int Day = Calendar.getInstance().getTime().getDay();

                            date = String.format("%02d/%02d/%d",Day, Month, Year);
                            time = String.format("%02d : %02d", Hour, Minute);
                            for(int i = 0; i < size; i++){
                                Toast.makeText(getApplicationContext(), String.valueOf(i), Toast.LENGTH_SHORT).show();

我想做什么

                                //String Date = arrayList.get(i).getDate();
                                //Toast.makeText(getApplicationContext(), arrayList.get(i).getDate(), Toast.LENGTH_SHORT).show();
                                //if(date.equals(Date))Toast.makeText(getApplicationContext(), arrayList.get(i).getTime(), Toast.LENGTH_SHORT).show();
                            }
                            //Toast.makeText(getApplicationContext(), date, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }catch (InterruptedException e){}
        }
    };
    t.start();



    int progress = 0;

    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_RING, progress, 0);
    //--------------------------------------------------------------------
    String input = intent.getStringExtra("inputExtra");
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("EVERLETHE").setContentText(input)
            .setSmallIcon(R.drawable.android).setContentIntent(pendingIntent)
            .build();
    startForeground(1, notification);
    return START_NOT_STICKY;
}

0 个答案:

没有答案