KeyServiceUser.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 |
package com.Services.KeyClient; 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.RemoteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.Services.KeyCommon.KeyGenerator; public class KeyServiceUser extends Activity { protected static final String TAG = "KeyServiceUser"; private KeyGenerator mKeyGeneratorService; private boolean mIsBound; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); final TextView output = (TextView) findViewById(R.id.output); final Button goButton = (Button) findViewById(R.id.go_button); goButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { try { // Call KeyGenerator and get a new ID if (mIsBound) output.setText(mKeyGeneratorService.getKey()); } catch (RemoteException e) { Log.e(TAG, e.toString()); } } }); } // Bind to KeyGenerator Service @Override protected void onResume() { super.onResume(); if (!mIsBound) { Intent intent = new Intent(KeyGenerator.class.getName()); bindService(intent, this.mConnection, Context.BIND_AUTO_CREATE); } } // Unbind from KeyGenerator Service @Override protected void onPause() { if (mIsBound) { unbindService(this.mConnection); } super.onPause(); } private final ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder iservice) { mKeyGeneratorService = KeyGenerator.Stub.asInterface(iservice); mIsBound = true; } public void onServiceDisconnected(ComponentName className) { mKeyGeneratorService = null; mIsBound = false; } }; } |
KeyGenerator.aidl
1 2 3 4 5 |
package com.Services.KeyCommon; interface KeyGenerator { String getKey(); } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/labeloutput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your ID" /> <TextView android:id="@+id/output" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:lines="2" /> <Button android:id="@+id/go_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:text="Get New ID" /> </LinearLayout> |