rubiks.kde
جمعه 31 خرداد 1392, 13:04 عصر
سلام به همه دوستان
من یک برنامه دارم که یک Activity بیشتر نیست و فیلدهایی روی این اکتیویتی هست که هر دقیقه از اینترنت گرفته میشه و روی اکتیوتی نمایش داده میشه و به کاربر آپدیت شدن رو با notification اطلاع میده.
برای این کار من یک کلاس سرویس دارم که هر دقیقه تابع آپدیت رو فراخونی میکنه.
public class MainService extends Service {
private static final String TAG = "MainService";
private static final long SLEEP_INTERVAL_MS = 2 * 60 * 1000; // 1 minute
private static int COUNTER = 0;
private MainActivity activity = null;
private ServiceThread thread = null;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Creating service");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int result = super.onStartCommand(intent, flags, startId);
Log.i(TAG, "Starting service");
return result;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Destroying service");
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "Binding to service");
if (thread == null) {
thread = new ServiceThread();
thread.start();
}
return new MainBinder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "Unbinding from service");
if (thread != null) {
thread.cancel();
thread.interrupt();
thread = null;
}
return true;
}
public class MainBinder extends Binder {
public void setActivity(MainActivity activity) {
MainService.this.activity = activity;
}
}
private class ServiceThread extends Thread {
private boolean cancelled = false;
public synchronized boolean isCancelled() {
return cancelled;
}
public synchronized void cancel() {
cancelled = true;
}
@Override
public void run() {
Log.i(TAG, "Service thread started");
while (!isCancelled()) {
try {
Thread.sleep(SLEEP_INTERVAL_MS);
COUNTER ++;
Log.i(TAG, "Checking if regular jobs should be performed");
if (activity != null) {
activity.performRegularJobs();
if(COUNTER == 30){
Log.i(TAG, "*************");
activity.performUpdate();
COUNTER = 0;
}
}
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Service thread finished");
}
}
}
این کار تازمانی که activity نمایش داده میشه درسته ولی اگه از برنامه خارج شم دیگه کار نمیکنه .
حال سوالم اینه چیکار کنم که بعد از خروج از برنامه هم برنامه در background درحال اجرا باشه و هم نوتیفیکیشن هم درست کار کنه (یعنی بااینکه برنامه توی background در حال اجرای ولی notification هم درست کار کنه)
باتشکر
من یک برنامه دارم که یک Activity بیشتر نیست و فیلدهایی روی این اکتیویتی هست که هر دقیقه از اینترنت گرفته میشه و روی اکتیوتی نمایش داده میشه و به کاربر آپدیت شدن رو با notification اطلاع میده.
برای این کار من یک کلاس سرویس دارم که هر دقیقه تابع آپدیت رو فراخونی میکنه.
public class MainService extends Service {
private static final String TAG = "MainService";
private static final long SLEEP_INTERVAL_MS = 2 * 60 * 1000; // 1 minute
private static int COUNTER = 0;
private MainActivity activity = null;
private ServiceThread thread = null;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Creating service");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int result = super.onStartCommand(intent, flags, startId);
Log.i(TAG, "Starting service");
return result;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Destroying service");
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "Binding to service");
if (thread == null) {
thread = new ServiceThread();
thread.start();
}
return new MainBinder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "Unbinding from service");
if (thread != null) {
thread.cancel();
thread.interrupt();
thread = null;
}
return true;
}
public class MainBinder extends Binder {
public void setActivity(MainActivity activity) {
MainService.this.activity = activity;
}
}
private class ServiceThread extends Thread {
private boolean cancelled = false;
public synchronized boolean isCancelled() {
return cancelled;
}
public synchronized void cancel() {
cancelled = true;
}
@Override
public void run() {
Log.i(TAG, "Service thread started");
while (!isCancelled()) {
try {
Thread.sleep(SLEEP_INTERVAL_MS);
COUNTER ++;
Log.i(TAG, "Checking if regular jobs should be performed");
if (activity != null) {
activity.performRegularJobs();
if(COUNTER == 30){
Log.i(TAG, "*************");
activity.performUpdate();
COUNTER = 0;
}
}
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Service thread finished");
}
}
}
این کار تازمانی که activity نمایش داده میشه درسته ولی اگه از برنامه خارج شم دیگه کار نمیکنه .
حال سوالم اینه چیکار کنم که بعد از خروج از برنامه هم برنامه در background درحال اجرا باشه و هم نوتیفیکیشن هم درست کار کنه (یعنی بااینکه برنامه توی background در حال اجرای ولی notification هم درست کار کنه)
باتشکر