2016-08-18 68 views
0

我已經與需要調用5到10個服務的應用程序一起工作。我必須逐個調用服務。完成第一個服務後,我需要打電話給第二個服務這是工作流程。我使用了意向服務來達到我的目的。如何以有效的方式實現這一點。如何使用意向服務在活動中逐一調用五個服務

這在MainActivity

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     Intent service_1=new Intent(MainActivity.this,Service1.class); 
     startService(service_1); 
     Intent service_2=new Intent(MainActivity.this,Service2.class); 
     startService(service_2); 
     Intent service_3=new Intent(MainActivity.this,Service3.class); 
     startService(service_3); 

    } 

} 

1服務

public class Service1 extends IntentService{ 

    public Service1() { 
     super("serviceone"); 
    } 



    @Override 
    protected void onHandleIntent(Intent intent) { 

     for(int i=0;i<1000;i++) 
     { 
      Log.e("service1","service1::............."+i); 
     } 

    } 
} 

第二服務

public class Service2 extends IntentService { 

    public Service2() { 
     super("service2"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 


     for(int i=0;i<100;i++) 
     { 
      Log.e("service2", "service::2*****************" + i); 
     } 

    } 
} 

服3

public class Service3 extends IntentService{ 

    public Service3() { 
     super("service3"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     for(int i=0;i<100;i++) 
     { 
      Log.e("service3", "service::3####################" + i); 
     } 
    } 
} 

IAM獲取日誌

ce1: service1::.............323 
08-18 16:12:14.441 6614-6640/homedashboard.egrove.com.multipleservice E/service1: service1::.............324 
08-18 16:12:14.441 6614-6640/homedashboard.egrove.com.multipleservice E/service1: service1::.............325 
08-18 16:12:14.441 6614-6640/homedashboard.egrove.com.multipleservice E/service1: service1::.............326 
08-18 16:12:14.441 6614-6640/homedashboard.egrove.com.multipleservice E/service1: service1::.............327 
08-18 16:12:14.441 6614-6640/homedashboard.egrove.com.multipleservice E/service1: service1::.............328 
08-18 16:12:14.441 6614-6640/homedashboard.egrove.com.multipleservice E/service1: service1::.............329 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************0 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************1 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************2 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************3 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************4 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************5 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************6 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************7 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************8 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************9 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************10 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************11 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************12 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************13 
08-18 16:12:14.453 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************14 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************15 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************16 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************17 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************18 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************19 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************20 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************21 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************22 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************23 
08-18 16:12:14.454 6614-6641/homedashboard.egrove.com.multipleservice E/service2: service::2*****************24 
08-18 16:12:14.457 6614-6640/homedashboard.egrove.com.multipleservice E/service1: service1::.............330 
08-18 16:12:14.457 6614-6640/homedashboard.egrove.com.multipleservice E/service1: service1::.............331 
08-18 16:12:14.457 6614-6640/homedashboard.egrove.com.multipleservice E/service1: service1::.............332 

第二個方法

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     //Intent service_2 = new Intent(MainActivity.this, Service2.class); 
     // startService(service_2); 
     //Intent service_3 = new Intent(MainActivity.this, Service3.class); 
     // startService(service_3); 




     AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, 0); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),10000, 
       pendingIntent); 

    } 

} 

廣播接收機

public class AlarmReceiver extends BroadcastReceiver 
{ 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     //get and send location information 

     Intent service_1 = new Intent(context, Service1.class); 
     context.startService(service_1); 

     Intent service_2 = new Intent(context, Service2.class); 
     context.startService(service_2); 

    } 
} 

服務類

public class Service1 extends IntentService{ 

    public Service1() { 
     super("serviceone"); 
    } 



    @Override 
    protected void onHandleIntent(Intent intent) { 

     forloopMethod(); 


    } 


    private void forloopMethod() 

    { 
     for(int i=0;i<10000;i++) 
     { 
      Log.e("service1","service1::.serviceone............"+i); 
     } 
    } 
} 

錯誤日誌

ice E/service1: service1::.serviceone............9837 
08-18 17:47:39.916 12517-21515/homedashboard.egrove.com.multipleservice E/service1: service1::.serviceone............9838 
08-18 17:47:39.916 12517-21515/homedashboard.egrove.com.multipleservice E/service1: service1::.serviceone............9839 
08-18 17:47:39.916 12517-21515/homedashboard.egrove.com.multipleservice E/service1: service1::.serviceone............9840 
08-18 17:47:39.916 12517-21515/homedashboard.egrove.com.multipleservice E/service1: service1::.serviceone............9841 
08-18 17:47:39.936 12517-21516/homedashboard.egrove.com.multipleservice E/service2: service::2*****************9751 
08-18 17:47:39.936 12517-21516/homedashboard.egrove.com.multipleservice E/service2: service::2*****************9752 
08-18 17:47:39.936 12517-21516/homedashboard.egrove.com.multipleservice E/service2: service::2*****************9753 
08-18 17:47:39.936 12517-21516/homedashboard.egrove.com.multipleservice E/service2: service::2*****************9754 

同樣得到我想打。經過第一服務第一完成服務調用,然後進行第二服務call.This是need..help我

+0

你爲什麼不嘗試用'BroadcastReceiver'?在每個「服務」開始之後,它會發送一條消息給'Activity'上註冊的'BroadcastReceiver'。取決於該消息,請決定您現在要開始的服務。 –

+0

請參閱下面的代碼 –

+0

您的意思是?在第二種方法中沒有找到任何代碼:( –

回答

0

當服務完成,以你可以發送廣播讓活動知道。

樣品實施:

public class MainActivity extends AppCompatActivity { 
    public void onCreate() { 
     ... 
     mMyReceiver = new MyReceiver(this); 
     LocalBroadcastManager.getInstance(this).registerReceiver(mMyReceiver, new IntentFilter(SERVICE_FINISHED_ACTION)); 
    } 

    public void onDestroy() { 
     if (mMyReceiver != null) { 
      LocalBroadcastManager.getInstance(this).unregisterReceiver(mMyReceiver); 
     } 
     super.onDestroy(); 
    } 

    public void launchServiceTwo() { 
     Intent service2 = new Intent(MainActivity.this, Service2.class); 
     startService(service2); 
    } 

    ... 

    private static class MyReceiver extends BroadcastReceiver { 

     private final WeakReference<MainActivity> mWeakActivity; 

     public MyReceiver(@NonNull MainActivity activity) { 
      mWeakActivity = new WeakReference<>(activity); 
     } 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Bundle bundle = intent.getExtras(); 

      MainActivity listener = mWeakActivity.get(); 
      if (listener != null && bundle != null) { 
       String serviceFinishedExtra = bundle.getString(SERVICE_FINISHED_EXTRA); 
       if ("Service1".equalsIgnoreCase(serviceFinishedExtra)) { 
        launchServiceTwo(); 
       } else if ("Service2".equalsIgnoreCase(serviceFinishedExtra)) { 
        launchServiceThree(); 
       } else if ... 
      } 
     } 

    } 
} 

public class Service1 extends IntentService { 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     ... 
     Intent broadcastIntent = new Intent(); 
     broadcastIntent.setAction(SERVICE_FINISHED_ACTION); 
     broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); 
     broadcastIntent.putExtra(SERVICE_FINISHED_EXTRA, "Service1"); 
     sendBroadcast(broadcastIntent); 
    } 

} 
+0

AlarmManager alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE); Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(thi s,0,intent1,0); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),10000, pendingIntent); –

+0

我已經使用該代碼每10秒調用一次該服務。如何將此代碼集成到您的代碼中? –

+0

請嘗試更好地解釋你想達到的目標。最初的問題是如何順序調用各種服務。現在你說的是你正在使用報警管理器。這是爲什麼?你是否試圖按順序運行這些服務,並且每次都要重複一定的時間? – fernandospr

相關問題