KeyGeneratorImpl.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 |
package com.Services.KeyService; import java.util.HashSet; import java.util.Set; import java.util.UUID; import android.app.Service; import android.content.Intent; import android.os.IBinder; import com.Services.KeyCommon.KeyGenerator; public class KeyGeneratorImpl extends Service { // Set of already assigned IDs // Note: These keys are not guaranteed to be unique if the Service is killed // and restarted. private final static Set<UUID> mIDs = new HashSet<UUID>(); // Implement the Stub for this Object private final KeyGenerator.Stub mBinder = new KeyGenerator.Stub() { // Implement the remote method public String getKey() { UUID id; // Acquire lock to ensure exclusive access to mIDs // Then examine and modify mIDs synchronized (mIDs) { do { id = UUID.randomUUID(); } while (mIDs.contains(id)); mIDs.add(id); } return id.toString(); } }; // Return the Stub defined above @Override public IBinder onBind(Intent intent) { return mBinder; } } |
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> |