0

我有一个应用程序,每天设置 5 次闹钟。警报时间来自本地 SQLite 数据库,可以正常工作。我之前为此编写过代码,但会抱怨通知没有“按时”触发。我认为它只是一个android的东西,这不是问题。最近更新了我的所有库并为我的应用程序发送了新更新后,通知的自定义声音已停止工作。

我看到其他应用程序可以设置和运行良好并按时发出警报。我想找到一种更有效的方式来处理和发送这些通知。

我的警报接收器代码:

public class AlarmReceiver extends BroadcastReceiver {

@Override
    public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);

        @SuppressLint("InvalidWakeLockTag")
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TRAININGCOUNTDOWN");
        wl.acquire(10*60*1000L /*10 minutes*/);
        Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.icon);
            Intent inteent = new Intent(context, MainActivity.class);
            inteent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, xx, inteent, PendingIntent.FLAG_UPDATE_CURRENT);
        xx = xx + 1;
        int id = context.getResources().getIdentifier(cc(preff.getString("voices","")), "raw", context.getPackageName());
        Uri notiSound = Uri.parse("android.resource://"+context.getPackageName() + "/" +id);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "com.c4kurd.bang.ANDROID")
                    .setSmallIcon(R.drawable.icon)
                    .setLargeIcon(icon)
                    .setContentTitle("بانگ")
                    .setContentText(notTitle)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setSound(notiSound, AudioManager.STREAM_ALARM)
                    // Set the intent that will fire when the user taps the notification
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);



        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
      // notificationId is a unique int for each notification that you must define


        // Put here YOUR code.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("com.c4kurd.bang");
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "com.c4kurd.bang",
                    "بانگ",
                    NotificationManager.IMPORTANCE_HIGH
            );
            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            
            channel.setSound(notiSound, attributes);
            channel.enableVibration(true);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }


//        assert notificationManager != null;

        notificationManager.notify(xx, builder.setVibrate(new long[]{1000,1000}).build());
        wl.release();


}

}

我在主要活动中用于警报的代码如下:

public void Alarm(int nID,int y,int Bh, int Bm) {
        Calendar wakeupcall = Calendar.getInstance();

        wakeupcall.setTimeInMillis(System.currentTimeMillis());
        SimpleDateFormat datte = new SimpleDateFormat("yyyy", Locale.US);
        d = new Date();
        int xy = Integer.parseInt(datte.format(d));

        String cv = getDate1(y);
        BM = Integer.parseInt(cv.substring(0, 2));
        BD = Integer.parseInt(cv.substring(3));

        int day = wakeupcall.get(Calendar.DAY_OF_YEAR) + y;

        wakeupcall.set(Calendar.YEAR, xy);
        wakeupcall.set(Calendar.MONTH, BM);
        wakeupcall.set(Calendar.DAY_OF_MONTH, BD);
        wakeupcall.set(Calendar.HOUR_OF_DAY, Bh);
        wakeupcall.set(Calendar.DAY_OF_YEAR, day);
        wakeupcall.set(Calendar.MINUTE, Bm);
        wakeupcall.set(Calendar.SECOND, 0);
        wakeupcall.set(Calendar.MILLISECOND, 0);


        Intent intenter = new Intent(getContext(), AlarmReceiver.class);
        intenter.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), nID, intenter, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

        if (Calendar.getInstance().before(wakeupcall)) {
            am.setExact(AlarmManager.RTC_WAKEUP, wakeupcall.getTimeInMillis(), pendingIntent);
        } else {
            am.cancel(pendingIntent);
        }

    }

提前致谢。

4

0 回答 0