我在应用程序中使用了3种服务。它们每个都充当计数器并出现在通知面板中。 当我同时启动所有它们时,通知面板中的图标会更改。我每秒都会生成带有服务类的通知。如何更新通知而不是重新创建?创建通知时我在做什么错了。
public class aktiviteService extends Service {
final static String channelid = "aktivite_notify";
final static int notifyid = 3;
final static String prefe_sure_adi = "aktivite_sure";
private Chronometer mChronometer;
MyServiceReceiver_aktivite myServiceReceiveraktivite;
MyServiceThread_aktivite myServiceThread_aktivite;
String sayac_txt = "-";
int cnt;
long kronometre;
SharedPreferences preferences;
@Override
public void onCreate() {
myServiceReceiveraktivite = new MyServiceReceiver_aktivite();
myServiceThread_aktivite = new MyServiceThread_aktivite();
preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext().getApplicationContext());
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_MSG_TO_SERVICE_aktivite);
intentFilter.addAction(ACTION_DURAKLAT_aktivite);
intentFilter.addAction(ACTION_BASLAT_aktivite);
registerReceiver(myServiceReceiveraktivite, intentFilter);
mChronometer = new Chronometer(this);
//mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.setBase( SystemClock.elapsedRealtime() - Long.parseLong(preferences.getString("aktivite_fark", "")));
myServiceThread_aktivite = new MyServiceThread_aktivite();
myServiceThread_aktivite.start();
showNotification(getApplicationContext());
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
//Toast.makeText(getApplicationContext(),"onDestroy", Toast.LENGTH_SHORT).show();
myServiceThread_aktivite.setRunning(false);
unregisterReceiver(myServiceReceiveraktivite);
super.onDestroy();
}
public class MyServiceReceiver_aktivite extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_MSG_TO_SERVICE_aktivite)) {
String msg = intent.getStringExtra(KEY_MSG_TO_SERVICE_aktivite);
//Toast.makeText(getApplicationContext(), "Mesaj geldi", Toast.LENGTH_LONG).show();
msg = new StringBuilder(msg).reverse().toString();
myServiceThread_aktivite.setRunning(false);
myServiceThread_aktivite.running = false;
Intent i = new Intent();
i.setAction(ACTION_UPDATE_MSG_aktivite);
i.putExtra(KEY_STRING_FROM_SERVICE_aktivite, msg);
sendBroadcast(i);
} else if (action.equals(ACTION_BASLAT_aktivite)) {
mChronometer.setBase(SystemClock.elapsedRealtime() - Long.parseLong(preferences.getString(prefe_sure_adi, "")));
myServiceThread_aktivite = new MyServiceThread_aktivite();
myServiceThread_aktivite.setRunning(false);
myServiceThread_aktivite.running = false;
myServiceThread_aktivite.setRunning(true);
myServiceThread_aktivite.running = true;
myServiceThread_aktivite.start();
}
}
}
private class MyServiceThread_aktivite extends Thread {
private boolean running;
public void setRunning(boolean running) {
this.running = running;
}
@Override
public void run() {
cnt = 0;
running = true;
while (running) {
try {
Thread.sleep(1000);
String saat = "";
long elapsedMillis = SystemClock.elapsedRealtime()
- mChronometer.getBase();
int hours = (int) (elapsedMillis / 3600000);
int minutes = (int) (elapsedMillis - hours * 3600000) / 60000;
int seconds = (int) (elapsedMillis - hours * 3600000 - minutes * 60000) / 1000;
if (hours == 0) {
saat = "";
} else {
saat = checkDigit(hours) + ":";
}
sayac_txt = saat + checkDigit(minutes) + ":" + checkDigit(seconds);
Intent intent = new Intent();
intent.setAction(ACTION_UPDATE_CR_aktivite);
//intent.putExtra(KEY_INT_FROM_SERVICE_aktivite, String.valueOf(sayac_txt));
intent.putExtra(KEY_INT_FROM_SERVICE_aktivite, String.valueOf(elapsedMillis));
sendBroadcast(intent);
showNotification(getApplicationContext());
cnt++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public String checkDigit(int number) {
return number <= 9 ? "0" + number : String.valueOf(number);
}
private NotificationManager notifManager;
public void showNotification(Context mContext) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mContext.getApplicationContext(), channelid);
Intent ii = new Intent(mContext.getApplicationContext(), aktivite.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(sayac_txt);
bigText.setBigContentTitle(getString(R.string.yapilanaktivite)+preferences.getString("aktivite_alttur_isim", ""));
bigText.setSummaryText(getString(R.string.aktivite));
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(getNotificationIcon());
mBuilder.setContentTitle(sayac_txt);
mBuilder.setContentText(getString(R.string.yapilanaktivite)+preferences.getString("aktivite_alttur_isim", ""));
//mBuilder.setPriority(Notification.DEFAULT_ALL);
mBuilder.setStyle(bigText);
mBuilder.setOnlyAlertOnce(true); //güncelleme için kullanıldı
mBuilder.setVisibility(1); //kilit ekranı için kullanıldı
NotificationManager mNotificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelid,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setSound(null, null);
mNotificationManager.createNotificationChannel(channel);
}
startForeground(notifyid, mBuilder.build());
}
private int getNotificationIcon() {
DAO db = new DAO(getApplicationContext()); db.open();
final int imageResID = getResources().getIdentifier(db.tur_idden_arac_ismini_getir(preferences.getInt("aktivite_alttur", 1),"resim"), "drawable",
getApplicationContext().getPackageName());
boolean useWhiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? imageResID : imageResID;
}
}