Created
December 15, 2016 02:47
-
-
Save zodani/ac3b262c68102dfc3a4e1c69acb590f1 to your computer and use it in GitHub Desktop.
AIDL client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private static final String SERVER_PACKAGE = "com.jaeryong.android.aidlserver"; | |
private static final String SERVER_ACTION = "com.jaeryong.android.action.aidlserver"; | |
private IRemoteService mRemoteService; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Intent serviceIntent = new Intent(SERVER_ACTION); | |
serviceIntent.setPackage(SERVER_PACKAGE); | |
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE); | |
Button button = (Button)findViewById(R.id.button); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
try { | |
int sum = mRemoteService.sum(2, 5); | |
Toast.makeText(getApplicationContext(), String.valueOf(sum), Toast.LENGTH_SHORT).show(); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
unbindService(serviceConnection); | |
} | |
private ServiceConnection serviceConnection = new ServiceConnection() { | |
@Override | |
public void onServiceConnected(ComponentName name, IBinder service) { | |
mRemoteService = IRemoteService.Stub.asInterface(service); | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName name) { | |
mRemoteService = null; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment