Back to project page androidIPCMessages.
The source code is released under:
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...
If you think the Android project androidIPCMessages 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 pl.mbos.messenger; //from w w w . j a v a 2s . co m 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.view.View; import android.widget.Button; import android.widget.TextView; import pl.mbos.MyObject; import pl.mbos.service.MessengerService; public class MainActivity extends Activity { private Button button; private Button button2; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText("MyPid "+android.os.Process.myPid()); sayHello(); } }); button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText("MyPid "+android.os.Process.myPid()); sayMyObject(); } }); } Messenger mService = null; boolean mBound; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { mService = new Messenger(service); mBound = true; } public void onServiceDisconnected(ComponentName className) { mService = null; mBound = false; } }; public void sayHello() { if (!mBound) return; Message msg = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0); try { mService.send(msg); } catch (RemoteException e) { e.printStackTrace(); } } public void sayMyObject() { if (!mBound) return; MyObject myObject = new MyObject("Android", 100); Message msg = Message.obtain(null, MessengerService.MSG_MY_OBJECT, 0, 0); Bundle bundle = new Bundle(MyObject.class.getClassLoader()); bundle.putParcelable(MyObject.KEY, myObject); msg.setData(bundle); try { mService.send(msg); } catch (RemoteException e) { e.printStackTrace(); } } @Override protected void onStart() { super.onStart(); // Bind to the service bindService(new Intent(this, MessengerService.class), mConnection, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); // Unbind from the service if (mBound) { unbindService(mConnection); mBound = false; } } }