Thursday, February 20, 2014
Since the start of the communication problems between the Service and Activity
Service is when the system boots, after receiving the broadcast starts, the role is to get the underlying data.
Now want to get data from one app through the Service Handler way,
Activity is bound by bindService with the Service, but has not received Activity data.
Testing Service found the handler is null.
Service starts after problem is, but also can not be operated by Activity Set Service's handler.
The main activity of the code is as follows:
public class MainActivity extends Activity {
UsbDongleService usb = null;
private Handler mHandler = new Handler () {
public void handleMessage (Message msg) {
switch (msg.what) {
case UsbDongleService.MSG_USB_ATTACH:
Toast.makeText (getApplicationContext (), "USB insert", Toast.LENGTH_SHORT) show ();.
break;
case UsbDongleService.MSG_USB_DETACH:
Toast.makeText (getApplicationContext (), "USB pull", Toast.LENGTH_SHORT) show ();.
break;
case UsbDongleService.MSG_USB_RX:
String t = (String) msg.obj;
Toast.makeText (getApplicationContext (), t, Toast.LENGTH_LONG) show ();.
TextView tv = (TextView) findViewById (R.id.textView2);
tv.setText (t);
break;
}
}
};
private ServiceConnection connection = new ServiceConnection () {
@ Override
public void onServiceConnected (ComponentName name, IBinder service) {
/ / TODO Auto-generated method stub
usb = new UsbDongleService ();
usb.setHander (mHandler);
Log.d ("connection", "onConnected");
}
@ Override
public void onServiceDisconnected (ComponentName name) {
/ / TODO Auto-generated method stub
usb = null;
Log.d ("connection", "onDisconnected");
}
};
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
Intent intent = new Intent ();
intent.setClass (MainActivity.this, UsbDongleService.class);
bindService (intent, connection, Context.BIND_AUTO_CREATE);
}<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
If two different processes, I think it is not the main thread of the Handler object passed to another object inside a process.
I suggest you use the broadcast, register a broadcast Activity inside, and then send broadcast inside the Service, this is the most common way to get the data to the Service UI layer.
usb = new UsbDongleService ();
usb.setHander (mHandler);
Service is set so obviously unable to receive mHandler objects, UsbDongleService this without instantiating, Android system will be instantiated.
Set a target should go inside through Service Binder subclass.
Reply:
I use Messenger to solve, is the Binder subclass, right?
Because this Service and Activity in a apk in, I still think this is not considered a communication between processes.
Reply:
Activity and Service App is not the same across process
Reply:
But Service is not started by Activity communication between threads can be used to solve the communication problems between them Mody.
Reply:
If not cross-process handler can solve the problem.
Intent can be broadcast across process, contentProvider and AIDL
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment