Back to project page ServicesTutorial.
The source code is released under:
Apache License
If you think the Android project ServicesTutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yanlu.android.services.app; /*from w w w . j a v a 2s. c o m*/ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import com.yanlu.android.services.app.service.BackgroundService; import com.yanlu.android.services.app.service.HandlerService; import com.yanlu.android.services.app.service.MyIntentService; import com.yanlu.android.services.app.utils.ThreadUtils; public class MainActivity extends ActionBarActivity { private static final String TAG = "MainActivity"; private TextView mContent; public static final String BROADCAST_ACTION = "com.yanlu.android.services.app.broadcast"; public static final String SERVICE_COUNTER = "counter"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContent = (TextView) findViewById(R.id.tv_value); ThreadUtils.logThreadSignature(); myHandler = new MyHandler(); new Thread(new MyThread()).start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.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(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void doClick(View view) { switch (view.getId()) { case R.id.startBtn: Log.v(TAG, "Starting service..."); Intent intent = new Intent(this, BackgroundService.class); startService(intent); break; case R.id.stopBtn: stopService(); break; case R.id.startHandler: Log.v(TAG, "Starting Handler service..."); startService(new Intent(this, HandlerService.class)); break; case R.id.stopHandler: stopHandlerService(); break; case R.id.startMyBtn: MyIntentService.startActionFoo(this, "MyIntentService", "start"); break; case R.id.stopMyBtn: stopService(new Intent(this, MyIntentService.class)); break; } } private void stopService() { Log.v(TAG, "Stopping service..."); if (stopService(new Intent(this, BackgroundService.class))) { Log.v(TAG, "stopService was successful"); } else { Log.v(TAG, "stopService was unsuccessful"); } } private void stopHandlerService() { Log.v(TAG, "Stopping Handler service..."); if (stopService(new Intent(this, HandlerService.class))) { Log.v(TAG, "stop Handler Service was successful"); } else { Log.v(TAG, "stop Handler Service was unsuccessful"); } } @Override protected void onResume() { super.onResume(); registerReceiver(receiver, new IntentFilter(BROADCAST_ACTION)); } @Override protected void onPause() { super.onPause(); unregisterReceiver(receiver); } @Override public void onDestroy() { stopService(); super.onDestroy(); } /* * ??Service?? */ private BroadcastReceiver receiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { // mContent.setText("" + ++i); mContent.setText("" + intent.getExtras().getInt(SERVICE_COUNTER)); } }; private static Handler myHandler; class MyHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle b = msg.getData(); String color = b.getString("key"); mContent.append(color); } } class MyThread implements Runnable { public void run() { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } Message msg = myHandler.obtainMessage(); Bundle b = new Bundle();// ?????? b.putString("key", "Handler Message"); msg.setData(b); myHandler.sendMessage(msg); // ???Handler?????????,??UI } } }