简体   繁体   中英

Not Able to Display Message in TEXTVIEW on alarm manager

This is code is to display message in textview

        Button can1 = (Button) findViewById(R.id.can1);
    txt1 = (TextView) findViewById(R.id.txt1); 
    Calendar c = Calendar.getInstance(); 
    hr1 = c.get(Calendar.HOUR_OF_DAY);
    startAlarm();

     if(hr1<12)
        {
            txt1.setText("Good morning!");
        }else if(hr1>12&& hr1<17)
        {
            txt1.setText("it's afternoon!");
        }else if(hr1>17&& hr1<20)
        {
            txt1.setText("Good evening!");
        }
    can1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            finish();
        }
    });
}


private void startAlarm() {


    if (alarm.getAlarmTonePath() != "") {
        mediaPlayer = new MediaPlayer();
        if (alarm.getVibrate()) {
            vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            long[] pattern = { 1000, 200, 200, 200 };
            vibrator.vibrate(pattern, 0);
        }
        try {
            mediaPlayer.setVolume(1.0f, 1.0f);
            mediaPlayer.setDataSource(this,
                    Uri.parse(alarm.getAlarmTonePath()));
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mediaPlayer.setLooping(true);
            mediaPlayer.prepare();
            mediaPlayer.start();

    }

}

}

My Layout for displaying message and Button is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/txt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="50dp"/>

<Button
    android:id="@+id/can1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cancel"
    android:tag="cancel" />
</LinearLayout>

Kindly suggest me whats the wrong in code, i am able to display Cancel Button but, Text (Message) is not displaying

If hr1 is higher or equal to 20 you didn't display anything. Try this:

if (hr1 < 12) {
    txt1.setText("Good morning!");
} else if(hr1 > 12 && hr1 < 17) {
    txt1.setText("it's afternoon!");
} else if(hr1 > 17 && hr1 < 20) {
    txt1.setText("Good evening!");
} else {
    txt1.setText("Good night!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM