Я хочу сделать уведомление, которое звонит в 7 часов утра каждый день.
Но уведомление не работает
это MainActivity .java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import java.util.Calendar;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity{
AlarmManager alarm_manager;
Context context;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Calendar calendar;
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
final PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
long TimeUntilTrigger;
if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
TimeUntilTrigger = calendar.getTimeInMillis() + 86400000;
Toast.makeText(getApplicationContext(), "past", Toast.LENGTH_LONG).show();
} else {
TimeUntilTrigger = calendar.getTimeInMillis();
Toast.makeText(getApplicationContext(), "future", Toast.LENGTH_LONG).show();
}
am.setRepeating(AlarmManager.RTC_WAKEUP, TimeUntilTrigger, 24 * 60 * 60 * 1000, sender);
}
}
И это AlarmReceiver .java. Мне нужно использовать BroadcastReceiver, AppCompatActivity. Но это не может быть больше двух, чем я использовал "AppCompatActivity appCompatActivity" в этом предложении.
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import static android.provider.Settings.System.getString;
public class AlarmReceiver extends BroadcastReceiver {
AppCompatActivity appCompatActivity;
private final static int NOTICATION_ID = 222;
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,"It's time to start", Toast.LENGTH_SHORT).show();
Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(100);
Log.d("AlarmBroadcastReceiver", "onReceive");
String channelId = "channel";
String channelName = "Channel Name";
NotificationManager notifManager
= (NotificationManager) appCompatActivity.getSystemService (Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notifManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder =
new NotificationCompat.Builder(appCompatActivity.getApplicationContext(), channelId);
Intent notificationIntent = new Intent(appCompatActivity.getApplicationContext()
, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
int requestID = (int) System.currentTimeMillis();
PendingIntent pendingIntent
= PendingIntent.getActivity(appCompatActivity.getApplicationContext()
, requestID
, notificationIntent
, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentTitle("Title") // required
.setContentText("Content") // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(android.R.drawable.btn_star)
.setContentIntent(pendingIntent);
notifManager.notify(0, builder.build());
}
}
Я могу 'Сплю из-за этого.(