我正在尝试在播放电视频道的应用中实现余数概念。概念是用户可以为某些电视节目设置剩余时间,并且该应用在频道播出前5分钟会显示通知。我正在使用AlarmManager,BroadcastReceiver和Service的组合来处理通知部分。
要求:针对不同的剩余部分显示单独的通知。即,当用户将剩余时间设置为15:26和15:27时,该应用必须在15:21和15:22上显示两个通知
问题:我仅收到一条通知,该通知将针对所有其他剩余部分进行更新。即应用显示剩余15:26的通知,并且同一通知已更新15:27
所以朋友帮我解决我的问题。
以下是执行上述任务的示例应用程序:
这是我的课程:
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView time1, time2, time3, time4, time5, time6;
Button button1, button2, button3, button4, button5, button6;
Intent intent;
String endTime;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
time1 = findViewById(R.id.time1);
button1 = findViewById(R.id.button1);
time2 = findViewById(R.id.time2);
button2 = findViewById(R.id.button2);
time3 = findViewById(R.id.time3);
button3 = findViewById(R.id.button3);
time4 = findViewById(R.id.time4);
button4 = findViewById(R.id.button4);
time5 = findViewById(R.id.time5);
button5 = findViewById(R.id.button5);
time6 = findViewById(R.id.time6);
button6 = findViewById(R.id.button6);
time1.setText("15:26");
time2.setText("15:27");
time3.setText("15:28");
time4.setText("15:29");
time5.setText("15:30");
time6.setText("15:31");
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.newitventure.remainder.START");
int id = (int) System.currentTimeMillis();
intent.setClass(MainActivity.this, ServiceRestarterBroadcastReceivers.class);
intent.putExtra("id", id);
intent.putExtra("time", time1.getText().toString());
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 1000, pi);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.newitventure.remainder.START");
int id = (int) System.currentTimeMillis();
intent.setClass(MainActivity.this, ServiceRestarterBroadcastReceivers.class);
intent.putExtra("id", id);
intent.putExtra("time", time2.getText().toString());
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 1000, pi);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.newitventure.remainder.START");
int id = (int) System.currentTimeMillis();
intent.setClass(MainActivity.this, ServiceRestarterBroadcastReceivers.class);
intent.putExtra("id", id);
intent.putExtra("time", time3.getText().toString());
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 1000, pi);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.newitventure.remainder.START");
int id = (int) System.currentTimeMillis();
intent.setClass(MainActivity.this, ServiceRestarterBroadcastReceivers.class);
intent.putExtra("id", id);
intent.putExtra("time", time4.getText().toString());
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 1000, pi);
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.newitventure.remainder.START");
int id = (int) System.currentTimeMillis();
intent.setClass(MainActivity.this, ServiceRestarterBroadcastReceivers.class);
intent.putExtra("id", id);
intent.putExtra("time", time5.getText().toString());
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 1000, pi);
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.newitventure.remainder.START");
int id = (int) System.currentTimeMillis();
intent.setClass(MainActivity.this, ServiceRestarterBroadcastReceivers.class);
intent.putExtra("id", id);
intent.putExtra("time", time6.getText().toString());
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 1000, pi);
}
});
}
}
ServiceRestarterBroadcastReceivers.java
public class ServiceRestarterBroadcastReceivers extends BroadcastReceiver {
Context context;
long millis;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
String action = intent.getAction();
if (action.equalsIgnoreCase("com.newitventure.remainder.STOP_SERVICE")) {
context.stopService(intent);
} else {
final String programTime = intent.getStringExtra("time");
final int id = intent.getIntExtra("id", 0);
millis = System.currentTimeMillis();
calculateMinute(programTime, id);
}
}
private void calculateMinute(String programTime, int id) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
Date startTime = new Date();
Date endTime = new Date();
try {
startTime = simpleDateFormat.parse(simpleDateFormat.format(millis));
endTime = simpleDateFormat.parse(programTime);
} catch (Exception e) {
e.printStackTrace();
}
long difference = endTime.getTime() - startTime.getTime();
if (difference < 0) {
Date dateMax = new Date();
Date dateMin = new Date();
try {
dateMax = simpleDateFormat.parse("24:00");
dateMin = simpleDateFormat.parse("00:00");
} catch (Exception e) {
e.printStackTrace();
}
difference = (dateMax.getTime() - startTime.getTime()) + (endTime.getTime() - dateMin.getTime());
}
String hour = String.format("%02d", TimeUnit.MILLISECONDS.toHours(difference));
String inMins = String.format("%02d", TimeUnit.MILLISECONDS.toMinutes(difference) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(difference)));
Log.d("lala", "onCreate: min" + hour + " " + inMins);
if (hour.equalsIgnoreCase("00")) {
if (inMins.equalsIgnoreCase("05")) {
Intent background = new Intent(context, AlarmServiceClass.class);
background.putExtra("time", programTime);
background.putExtra("id", id);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(background);
} else {
context.startService(background);
}
}
}
}
}
AlarmServiceClass.java
public class AlarmServiceClass extends Service {
long millis;
int id;
String programTime;
Notification notification;
NotificationManager notificationManager;
IntentFilter intentFilter;
@Override
public void onCreate() {
super.onCreate();
Log.d("lala", "onCreate: ");
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
return START_STICKY;
}
private void showNotification(String programTime, int id) {
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification);
String CHANNEL_ID = "2018";
CharSequence channelName = "Vinay";
Intent recentIntent = new Intent(this, MainActivity.class);
recentIntent.setAction("com.newitventure.remainder.STOP_SERVICE");
recentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendInt = PendingIntent.getActivity(this, id,
recentIntent, PendingIntent.FLAG_ONE_SHOT);
views.setTextViewText(R.id.text, programTime);
notification = new NotificationCompat.Builder(this)
.setCustomContentView(views)
.setContentIntent(pendInt)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setChannelId(CHANNEL_ID)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
channelName, NotificationManager.IMPORTANCE_LOW);
channel.setSound(null, null);
notificationManager.createNotificationChannel(channel);
}
startForeground(id, notification);
}
}
答案 0 :(得分:0)
我找到了解决问题的方法。我没有使用startForeground(id, notification)
来开始服务,而是使用notificationManager.notify(id, notification)
和瞧!有效。我收到所有6条通知。
但是,即使我的应用程序已从最近的应用程序中删除,当我想显示通知时仍然存在问题。 Android Oreo禁止应用显示后台通知,因此它迫使我使用startForeground(id, notification)
。因此,我回到正题。有人吗?