第一步:创建服务类MyService,代码如下:// btnStartService = (Button) findViewById(R.id.btnStartService);
// btnStopService = (Button) findViewById(R.id.btnStopService);
package com.example.yangjian.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class MyService extends Service { private static final String TAG = "MyService"; @Override public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate called."); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand called."); return super.onStartCommand(intent, flags, startId); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.i(TAG, "onStart called."); } @Override public IBinder onBind(Intent intent) { Log.i(TAG, "onBind called."); return null; } @Override public boolean onUnbind(Intent intent) { Log.i(TAG, "onUnbind called."); return super.onUnbind(intent); } @Override public void onDestroy() { super.onDestroy(); Log.i(TAG, "onDestory called."); } }
第二步:配置AndroidManifest.xmal中的配置服务信息:
<service android:name=".MyService" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.intent.action.MyService"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service>
第三步:在MainActivity中添加代码:
package com.example.yangjian.service; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity implements View.OnClickListener { private Button btnStartService; private Button btnStopService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupViews(); // btnStartService = (Button) findViewById(R.id.btnStartService); // btnStopService = (Button) findViewById(R.id.btnStopService); } public void setupViews() { btnStartService = (Button) findViewById(R.id.btnStartService); btnStopService = (Button) findViewById(R.id.btnStopService); btnStartService.setOnClickListener(this); btnStopService.setOnClickListener(this); } /* 启动服务 */ /* public void start(View view) { Intent intent = new Intent(MainActivity.this, MyService.class); startService(intent); } /* 停止服务 */ /*public void stop(View view) { Intent intent = new Intent(MainActivity.this, MyService.class); stopService(intent); }*/ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { Intent serviceIntent = new Intent(MainActivity.this, MyService.class); switch (v.getId()) { case R.id.btnStartService: startService(serviceIntent); break; case R.id.btnStopService: stopService(serviceIntent); break; } } }