微调框无法将正确的数据发送到警报

时间:2019-05-25 18:02:18

标签: android android-intent spinner alarm

我是编程新手,我只是制作了一个应用程序,但是问题是我尝试从微调器(A101,A102,A103,A104,A105)发送所选数据,例如,我选择了A102,那么A102将发送到“警报广播接收者”,然后制作Toast A102,但是问题是我尝试使用“日志”检查从警报接收的数据。正在发送的数据与我在微调器中选择的数据不同。就像我在选择A102之前所说的那样,但是从日志中我看到发送的数据是A104,有时它可以是A103或A101。但有时接收到的数据与我从微调器中选择的数据相同。我在下面包含我的代码:

希望任何人都可以帮助解决我的问题。

// Spinner on class

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.blockA, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        class_number.setAdapter(adapter);
        class_number.setOnItemSelectedListener(this);



        //Pick Start Time

        btnAddData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Set Alarm

                setAlarm(tms);

 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        text = parent.getItemAtPosition(position).toString();
        text2 = parent.getSelectedItem().toString();
        Toast.makeText(parent.getContext(), "Kelas :" + text, Toast.LENGTH_SHORT).show();
        classnumberD.setText(text);

    }


    public void onNothingSelected(AdapterView<?> parent) {

    }

private void setAlarm(Long timeInMillis) {
        //getting the alarm manager
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);


        //creating a new intent specifying the broadcast receiver
        Intent intent = new Intent(this, MyAlarm.class);
        intent.putExtra("classnnumberD",class_number.getOnItemSelectedListener().toString());
        intent.putExtra("notificationId", notificationId);



        //creating a pending intent using the intent
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        //setting the repeating alarm that will be fired every day
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent);
        Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();


    }

0 个答案:

没有答案