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

سرویس اندروید

Jaber  8 سال پیش  8 سال پیش
0 0

سلام دوستان

من یه برنامه ای نوشتم که داخلش از یک سرویس استفاده کردم ، که داخل این سرویس یه تایمر قرار داره که بعد از اتمام این تایمر یک نوتیفیکیشن ارسال میشه و یک دکمه مقداری رو ذخیره میکنه

مشکلی که هست اینه که وقتی برنامه رو میبندم سرویس بسته میشه،وقتی مینیمایز میکنم یا دکمه خانه رو میزنم درست کار میکنه اما وقتی برنامه رو کامل میبندم سرویس هم کرش میشه

public class MyService extends Service {


SharedPreferences sharedpreferences;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static final Integer A = 0;

@Override
public int onStartCommand(Intent intent, final int flags, int startId) {



//Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
new CountDownTimer(12000, 1000) {


public void onTick(long millisUntilFinished) {

//Toast.makeText(MyService.this, "Start"+millisUntilFinished/1000, Toast.LENGTH_SHORT).show();


Log.i("MyService",""+millisUntilFinished/1000);

}
@SuppressWarnings("WrongConstant")
public void onFinish() {

SharedPreferences settings = getSharedPreferences("A", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("A",1);
editor.commit();

Uri soundUri = Uri.parse("android.resource://"+MyService.this.getPackageName()+"/"+R.raw.win);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MyService.this)
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle("ضیافت") // title for notification
.setSound(soundUri)
.setContentText("برای یه پیشنهاد جدید آماده ای؟") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(MyService.this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(MyService.this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());

}
}.start();
return START_NOT_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
//Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}


}


 برای این سوال پاسخی وجود ندارد.

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