The Establishment of The Open Mobile Alliance and the launch of the Android service are significant changes to the current situation. Prior to the initial benefits, it still requires a lot of patience and high investment. Google will continue to work hard, make these services better and add more attractive features, applications, and services.
1. The Service in the Android Service is in the same thread as the caller, so it is necessary to open a new thread in the Service for time-consuming operations.
2. In Android services, we mainly implement functions such as onCreate, onStart, onDestroy, onBind, and onUnBind to implement the functions we need.
Simple calls can be called using Context. startService In the caller object. Intent is used as the parameter. Of course, Intent search matches the target in the same way as in Intent usage.
Let's look at a routine:
- package test.pHello;
-
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
-
- public class HelloActivity extends Activity {
-
- ITestService mService = null;
- ServiceConnection sconnection = new ServiceConnection()
- {
- public void onServiceConnected(ComponentName name, IBinder service)
- {
- mService = (ITestService)service;
- if (mService != null)
- {
- mService.showName();
- }
- }
-
- public void onServiceDisconnected(ComponentName name)
- {
-
- }
- };
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // TODO Auto-generated method stub
- super.onCreateOptionsMenu(menu);
- menu.add(0, Menu.FIRST+1, 1, "OpenActivity");
- menu.add(0, Menu.FIRST+2, 2, "StartService");
- menu.add(0, Menu.FIRST+3, 3, "StopService");
- menu.add(0, Menu.FIRST+4, 4, "BindService");
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // TODO Auto-generated method stub
- super.onOptionsItemSelected(item);
- switch(item.getItemId())
- {
- case Menu.FIRST + 1:
- {
- this.setTitle("Switch Activity");
- Intent i = new Intent();
- i.setAction("test_action");
- if (Tools.isIntentAvailable(this,i))
- this.startActivity(i);
- else
- this.setTitle("the Intent is unavailable!!!");
- break;
- }
- case Menu.FIRST + 2:
- {
- this.setTitle("Start Service");
- //Intent i = new Intent(this, TestService.class);
- Intent i = new Intent();
- i.setAction("start_service");
- this.startService(i);
- break;
- }
- case Menu.FIRST + 3:
- {
- this.setTitle("Stop Service");
- Intent i = new Intent(this, TestService.class);
- this.stopService(i);
- break;
- }
- case Menu.FIRST + 4:
- {
- this.setTitle("Bind Service!");
- Intent i = new Intent(this, TestService.class);
- this.bindService(i, this.sconnection, Context.BIND_AUTO_CREATE);
-
- break;
- }
- }
- return true;
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.main);
- }
- }
Compile and execute. You will find that onCreate is executed first and then onBind is executed. When Context. bindService of the caller returns, OnConnected of ServiceConnection is not executed immediately. Remote binding of Android services: The preceding binding is performed when the caller and Service are in the same application. If the caller and Service are in different programs, the call method is in another situation. Let's take a look.