PDA

View Full Version : عدم اجرای سرویس در هنگام باز نبودن اپلیکیشن



parniaznet
سه شنبه 15 اردیبهشت 1394, 15:34 عصر
سلام
وقت بخیر
من می خوام وقتی اپلیکیشن باز نیست یه کاری رو رو گوشی بتونم انجام بدم
برای این کار از سرویس استفاده می کنم

و در اولین اکتیویتی اپلیکیشنم فراخونیش می کنم :
startService(new Intent(this, MyService.class));

تا وقتی اپلیکیشن باز هست یا مینیمایز هست مشکلی نیست و سرویس کار می کنه اما وقتی اپ بسته میشه سرویس از کار میفته

مگه سرویس ها برای این نیستند که بدون باز بودن اپلیکیشن و در بک گراند کار انجام بدن؟!

کد های سرویس من :


public class MyService extends Service{
private static String TAG = "Inchoo.net tutorial";
int mNotificationId = 001;
Notification n;
NotificationManager nm;


@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub




return null;
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.d(TAG, "FirstService started");

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
readWebpage();
handler.postDelayed(this, 15000); //now is every 2 minutes
}
}, 15000); //Every 120000 ms (2 minutes)



this.stopSelf();
}
/*
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(TAG, "FirstService destroyed");
}*/



private void readWebpage() {
String appName = "com.fallrooz.srp";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appName));
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("تست نوتیفیکیشن")
.setContentText("تست تست تست")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneMa nager.TYPE_NOTIFICATION))
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();


NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, n);

//Toast.makeText(this, "shod", Toast.LENGTH_SHORT).show();

}
}

JYasProgramer
سه شنبه 15 اردیبهشت 1394, 16:44 عصر
در منیفست برنامت سرویس رو معرفی کردی؟
متد onStartCommand رو در کلاس سرویس خودت بازنویسی کن
و در کلاس اکتیویتی خودت کلاس سرویس رو فراخونی کن
startService(new Intent(getBaseContext(), MyService.class));

parniaznet
سه شنبه 15 اردیبهشت 1394, 17:07 عصر
در منیفست برنامت سرویس رو معرفی کردی؟
متد onStartCommand رو در کلاس سرویس خودت بازنویسی کن
و در کلاس اکتیویتی خودت کلاس سرویس رو فراخونی کن
startService(new Intent(getBaseContext(), MyService.class));

با این کد در منیفست سرویس رو تعریف کدم :

<service android:name=".MyService" />

در اکتیویتی هم فراخونی می کنم اما بعد از خروج از برنامه سرویس هم قطع میشه

JYasProgramer
سه شنبه 15 اردیبهشت 1394, 17:20 عصر
باید متد onStartCommand رو بازنویسی کند اونم در سرویست

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

// اینجا هندلر رو فراخونی کن
return START_STICKY;
}

parniaznet
سه شنبه 15 اردیبهشت 1394, 17:37 عصر
باید متد onStartCommand رو بازنویسی کند اونم در سرویست

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

// اینجا هندلر رو فراخونی کن
return START_STICKY;
}


خب انجام دادم اما فرقی نکرد عزیز :



public class MyService extends Service{
private static String TAG = "Inchoo.net tutorial";
int mNotificationId = 001;
Notification n;
NotificationManager nm;

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
readWebpage();
handler.postDelayed(this, 15000); //now is every 2 minutes
}
}, 15000); //Every 120000 ms (2 minutes)
// اینجا هندلر رو فراخونی کن
return START_STICKY;
}

private void readWebpage() {


String appName = "com.fallrooz.srp";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appName));
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("تست نوتیفیکیشن")
.setContentText("تست تست تست")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneMa nager.TYPE_NOTIFICATION))
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();


NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, n);

//Toast.makeText(this, "shod", Toast.LENGTH_SHORT).show();

}
}

JYasProgramer
سه شنبه 15 اردیبهشت 1394, 17:52 عصر
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}

این رو به جای متد onStartCommand خودت بزار و تغییرش نده و اجرا کن اگه نشد مشکا در اکتیویتیته سایر متدها رو در سرویست حذف کن

parniaznet
سه شنبه 15 اردیبهشت 1394, 17:59 عصر
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}

این رو به جای متد onStartCommand خودت بزار و تغییرش نده و اجرا کن اگه نشد مشکا در اکتیویتیته سایر متدها رو در سرویست حذف کن

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

من می خوام هر 20 ثانیه یا 15 ثانیه اون توست رو نمایش بدم حتی وقتی کاربر از برنامه خارج شده باز هم نمایش بده

tux-world
سه شنبه 15 اردیبهشت 1394, 20:28 عصر
مشکل شما WeakUp هستش. البته این سی پی یو رو برای برنامه شما همیشه تو حالت باز نگه میداره. اگه تراکنش آنچنانی ندارید ازش استفاده کنید یعنی وقتی برنامه شما بستست سی پی یو برای شما ترد نگه میداره و اصلا بسته نمیشه سرویس.