کلیدستان

نسخه‌ی کامل: عدم اجرای سرویس در هنگام باز نبودن اپلیکیشن (در اندروید)
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
سلام
وقت بخیر
من می خوام وقتی اپلیکیشن باز نیست یه کاری رو رو گوشی بتونم انجام بدم
برای این کار از سرویس استفاده می کنم

و در اولین اکتیویتی اپلیکیشنم فراخونیش می کنم :

کد پی‌اچ‌پی:
startService(new Intent(thisMyService.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 intentint startId) {
        
// TODO Auto-generated method stub
        
super.onStart(intentstartId);
        
Log.d(TAG"FirstService started");
         
        final 
Handler handler = new Handler();
        
handler.postDelayed(new Runnable() {
            public 
void run() {
                
readWebpage();
                
handler.postDelayed(this15000); //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_VIEWUri.parse("http://play.google.com/store/apps/details?id=" appName));
        
PendingIntent pIntent PendingIntent.getActivity(this0intent0);
 
        
// 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 
          (
NotificationManagergetSystemService(NOTIFICATION_SERVICE);
 
        
notificationManager.notify(0n); 
         
        
//Toast.makeText(this, "shod", Toast.LENGTH_SHORT).show();
 
    
}

سلام.
این موردی که تذکر می دهم را اجرا کنید، ببینید درست میشه یا نه :
شما کدهای اجرایی Service را در روش onStart نوشته اید، اما این بار آنها را در روش onStartCommand و مطابق ساختار شرح داده شده در کلید زیر بنویسید :

کلید شماره 2609