LoggingServiceClient.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class LoggingServiceClient extends Activity { private final static String MESSAGE_KEY = "course.examples.Services.Logging.MESSAGE"; private final static int LOG_OP = 1; private final static String TAG = "LoggingServiceClient"; // Intent used for binding to LoggingService private final static Intent mLoggingServiceIntent = new Intent( "course.examples.Services.LoggingServiceWithMessenger.LoggingService"); private Messenger mMessengerToLoggingService; private boolean mIsBound; // Object implementing Service Connection callbacks private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { // Messenger object connected to the LoggingService mMessengerToLoggingService = new Messenger(service); mIsBound = true; } public void onServiceDisconnected(ComponentName className) { mMessengerToLoggingService = null; mIsBound = false; } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button buttonStart = (Button) findViewById(R.id.buttonStart); buttonStart.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (mIsBound) { // Send Message to the Logging Service logMessageToService(); } } }); } // Create a Message and sent it to the LoggingService // via the mMessenger Object private void logMessageToService() { // Create Message Message msg = Message.obtain(null, LOG_OP); Bundle bundle = new Bundle(); bundle.putString(MESSAGE_KEY, "Log This Message"); msg.setData(bundle); try { // Send Message to LoggingService using Messenger mMessengerToLoggingService.send(msg); } catch (RemoteException e) { Log.e(TAG, e.toString()); } } // Bind to LoggingService @Override protected void onResume() { super.onResume(); bindService(mLoggingServiceIntent, mConnection, Context.BIND_AUTO_CREATE); } // Unbind from the LoggingService @Override protected void onPause() { if (mIsBound) unbindService(mConnection); super.onPause(); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/buttonStart" android:layout_width="match_parent" android:layout_height="64dp" android:layout_alignParentBottom="true" android:text="@string/generate_log_message_string" android:textSize="24sp" > </Button> </RelativeLayout> |