Android Studio - 来自SQLite数据库的通知

问题描述 投票:0回答:1

我有一个应用程序,它将事件保存在数据库中,如plan_title,plan_date和plan_time。我必须根据SQLite数据库中保存的日期和时间生成通知或警报。这是我试图做的:

public class AlarmSet extends AppCompatActivity {
    Databasehelper databasehelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SQLiteDatabase db = databasehelper.getReadableDatabase();
        //To fetch the event from the database
        Cursor cursor = db.query(Databasehelper.TABLE_NAME,
                new String[]{Databasehelper.COL_1, Databasehelper.COL_4, Databasehelper.COL_3, Databasehelper.COL_2},
                null, null, null, null, null);

        cursor.moveToFirst();

        while (!cursor.isAfterLast()) { // Loop until all vales have been seen

            String time = cursor.getString(1);
            String[] Timesplit = time.split(":"); //Split  String Value stored in db
            String hour = Timesplit[0]; // hour
            String minute = Timesplit[1]; // minute
            String Date = cursor.getString(2);
            String[] Datesplit = Date.split("-"); //Split  String Value stored in db
            String day = Datesplit[0]; // day
            String month = Datesplit[1]; // month
            String year = Datesplit[2]; // year
            String title = cursor.getString(3);
            int hr = Integer.parseInt(hour);
            int min = Integer.parseInt(minute);
            int d = Integer.parseInt(day);
            int m = Integer.parseInt(month);
            int y = Integer.parseInt(year);

            final int id=(int)System.currentTimeMillis();
            //Assigning the event in the alarm
            Calendar calendar1 = Calendar.getInstance();
            calendar1.set(Calendar.YEAR,y);
            calendar1.set(Calendar.MONTH,m);
            calendar1.set(Calendar.DAY_OF_MONTH,d);
            calendar1.set(Calendar.HOUR_OF_DAY, hr);
            calendar1.set(Calendar.MINUTE, min);
            calendar1.set(Calendar.MILLISECOND, 0);
            if (calendar1.before(Calendar.getInstance())) {
                calendar1.add(Calendar.DATE, 1);
            }
            Intent intent = new Intent(AlarmSet.this, Alarm.class);

            intent.putExtra("title",title);
            intent.putExtra("id",   id);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            final AlarmManager alarm1 = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarm1.setExact(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent);
            Toast.makeText(AlarmSet.this, "alarms",Toast.LENGTH_SHORT).show();




            cursor.moveToNext();
        }

    }
    }

这是接收器。

public class Alarm extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        int id= Integer.valueOf(intent.getStringExtra("id"));
        String title=intent.getStringExtra("title");


        NotificationManager notificationManager=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
        Intent intent1=new Intent(context, AlarmSet.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent=PendingIntent.getActivity(context, id,intent1, PendingIntent.FLAG_ONE_SHOT);;
        NotificationCompat.Builder builder2=new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setContentTitle(title)
                .setVibrate(new long[] { 1000, 1000})
                .setAutoCancel(true);
        notificationManager.notify(id,builder2.build());
    }


}

它不会生成通知。我是android studio的新手,所以我会非常感谢任何帮助或提示

javascript android notifications android-sqlite alarm
1个回答
0
投票

自Android 8.0(API级别26)以来运行的设备具有略微不同的通知显示实现,如果实施错误可能不会显示通知,请阅读并遵循官方指南Notifications

在所有设备上工作的示例:

 public void showNotification() {
    NotificationManager notificationManager =
            (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager != null) {
        Intent intent = new Intent(getContext(), MainActivity.class);
        boolean isSound = getCloudMessage().isWithSound();
        if (isSound) {
            getCloudMessage().setSoundMute();
        }
        intent.putExtra(KEY_CLOUD_MESSAGE, getCloudMessage());
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), REQUEST_CODE,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID, getTitle(), importance);
            channel.setDescription(getBodyMessage());
            channel.enableVibration(true);
            notificationManager.createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(getContext(), CHANNEL_ID);
        } else {
            builder = new NotificationCompat.Builder(getContext());
        }

        builder.setContentTitle(getTitle())
                .setContentTitle(getTitle())
                .setContentText(getBodyMessage())
                .setSmallIcon(R.drawable.ic_notification)
                .setSmallIcon(R.drawable.ic_notification)
                .setLargeIcon(BitmapFactory.decodeResource(
                        getContext().getResources(), R.mipmap.ic_launcher))
                .setBadgeIconType(R.mipmap.ic_launcher)
                .setColor(ContextCompat.getColor(getContext(),
                        R.color.primary_color))
                .setStyle(new NotificationCompat
                        .BigTextStyle()
                        .setBigContentTitle(getTitle())
                        .bigText(getBodyMessage()))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
        if (isSound) {
            builder.setSound(sound);
        }

        Notification notification = builder.build();
        notificationManager.notify(NOTIFICATION_ID, notification);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.