آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
مشکل عدم دسترسی خریداران پیشین به برخی آموزش ها برطرف شد
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

مشکل با سرویس، نوتیفیکیشن و ترد

WS  7 سال پیش  7 سال پیش
0 0

سلام دوستان من یک سرویس نوشتم که هر 5 ثانیه وصل میشم به یک سایت  و اگه دیتا جدید داشت یه نوتیفیکیشن به کاربر میده.همچی درست کار میکنه وقتی برنامه کاملا بسته هست درست کار میکنه اما توی نوتیفیکیشن یه پیغام میاد که app is running   touch for more information or stop the app.

 public class ServiceNotification extends IntentService {
  ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
  public ServiceNotification() {
    super("notify");
  }

  @Override
  public void onCreate() {
    super.onCreate();
  }

  @Override
  public int onStartCommand(final Intent intent, final int flags, final int startId) {
    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
      public void run() {
        CustomNotification customNotification = new CustomNotification();
        customNotification.notification("شمااطلاعات  جدید دارید.برای مشاهده کلیک کنید",0);
        startForeground(1, customNotification);
      }
    }, 0, 5, TimeUnit.SECONDS);

    return Service.START_STICKY;
  }

  public void onTaskRemoved(Intent rootIntent) {
    Intent restartService = new Intent(getApplicationContext(), this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(getApplicationContext(), 0, restartService, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000, restartServicePI);

  }


  @Override
  public void onDestroy() {
    super.onDestroy();
  }

  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }

  @Override
  protected void onHandleIntent(Intent intent) {

  }


  @Override
  public void onRebind(Intent intent) {
    super.onRebind(intent);
  }


}


 
 public class CustomNotification extends Notification {
  public CustomNotification() {
    super();
  }

  public void notification(String txt ,int m) {
    Thread thread = new Thread();
    thread.start();
    if (Thread.data) {
      ThreadSms.data = false;
      Intent notificationIntent = new Intent(GlobalController.currentActivity, ActivityMain.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(GlobalController.context, 1, notificationIntent, 0);
      Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      RemoteViews remoteViews = new RemoteViews(GlobalController.context.getPackageName(), R.layout.custom_toast);
      remoteViews.setTextViewText(R.id.text, txt);
      NotificationCompat.Builder builder = new NotificationCompat.Builder(GlobalController.context)
        .setSmallIcon(R.drawable.icon)
        .setContent(remoteViews)
        .setContentIntent(pendingIntent)
        .setSound(defaultSoundUri).setAutoCancel(false)
        .setLargeIcon(BitmapFactory.decodeResource(GlobalController.context.getResources(), R.drawable.icon))
        .setColor(Color.argb(255, 255, 152, 0))
        .setPriority(Notification.PRIORITY_MAX);
      GlobalController.notificationManager.notify(0, builder.build());

    }

  }

 
 
0 0
از دوستان اساتید کسی کمک نمیکنه؟ (7 سال پیش)
0 0
لینک (7 سال پیش)
0 0
سلام مرسی من اینو دیده بودم ولی نتیجه نداد. (7 سال پیش)
0 0
اقای rabbit راه کار دیکه ای ندارید؟ (7 سال پیش)
 برای این سوال پاسخی وجود ندارد.

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .