HỎI Cài đặt thời gian hiển thị một thông báo trong android studio?

CuDer

New Member
23/4/20
26
0
1
Viet Nam
Em đang tập tành học làm app và em đang vướng ở chỗ làm sao hiển thị ra cái thông báo khi mà đến đồng hồ chỉ đến thời gian mình cài.Em tạo được một thông báo rồi, cài đặt thời gian hẹn giờ được rồi nhưng mà cái code tạo thông báo em chưa biết đặt ở chỗ nào.Em đặt ở cái class extends BroadcastReceiver thì nó báo lỗi ạ.Code em đây ạ
Java:
package com.example.splitstring;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.ArrayList;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    PendingIntent pendingIntent;
    Calendar calendar;
    AlarmManager alarmManager;
    Button btnSetTime;
    TimePicker timePicker;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        btnSetTime = findViewById(R.id.btnSetTime);
        calendar = Calendar.getInstance();
        alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        final Intent intent = new Intent(this, AlarmReceiver.class);
        btnSetTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                int gio = timePicker.getCurrentHour();
                int phut = timePicker.getCurrentMinute();
                textView.setText("Gio ban dat la "+gio+" :" + phut);

                pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            }
        });
    }


}
Code:
package com.example.splitstring;

import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TimePicker;

import androidx.core.app.NotificationCompat;

import java.util.ArrayList;
import java.util.Calendar;

public class AlarmReceiver extends BroadcastReceiver {
    ArrayList<String> chuoi;
    EditText editText;
    MultiAutoCompleteTextView textView;
    public static final  String CHANNEL_1_ID = "channel1";
    public static final  String CHANNEL_2_ID = "channel2";
    @Override
    public void onReceive(Context context, Intent intent) {
        createNotificationChannels();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(AlarmReceiver.this, CHANNEL_1_ID)
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setContentTitle("Hello")
                    .setContentText("Day la thong bao")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, builder.build());
        }
    }
    private void createNotificationChannels()  {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel1 = new NotificationChannel(
                    CHANNEL_1_ID,
                    "Channel 1",
                    NotificationManager.IMPORTANCE_HIGH
            );
            channel1.setDescription("This is channel 1");

            NotificationChannel channel2 = new NotificationChannel(
                    CHANNEL_2_ID,
                    "Channel 2",
                    NotificationManager.IMPORTANCE_LOW
            );
            channel1.setDescription("This is channel 2");


            NotificationManager manager = this.getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel1);
            manager.createNotificationChannel(channel2);
        }
    }
}
 

Joe

Thành viên VIP
21/1/13
2,969
1,310
113
What does the dot mean ???? If you think that this forum is obligatory to wait for your question and to answer it within a day whenever you have posted it then I am sorry and believe that no one will want to help you. Click HERE to learn the rules.
 
Sửa lần cuối: