在Android的应用程序类和服务之间的通信应用程序、和服务、通信、Android

2023-09-07 01:00:26 作者:愿人生无可辜负

我已经在那里我已经在我的Andr​​oid项目的扩展应用程序类的要求。

例如:

 公共类对myApp扩展应用程序实现    myReceiver.Receiver {...} 

是否有可能对我来说,通过服务使用我的沟通 - Message.obtain或者我应该用其他的东西?请指点。

我也想将数据传递给我的服务,这是一个字符串/恒定值。我能做到这一点是这样的:

 私人无效SENDMSG(INT ARG1,ARG2 INT){        如果(mBound){            消息味精= Message.obtain(NULL,MyService.Hello,                ARG1,ARG2);            尝试{                mService.send(MSG);             }赶上(RemoteException的E){                Log.e(TAG,发送错误信息,E);             }         }     } 

解决方案

试试这个:在扩展应用程序类创建一个内部类

 私有类MyMessageHandler扩展了Handler {    @覆盖    公共无效的handleMessage(消息MSG){        super.handleMessage(MSG);        捆绑bundelData = msg.getData();        如果(bundelData!= NULL){            字符串mString =(字符串)bundelData.get(IConstants.HOME_SCREEN_LISTUPDATE);            如果(mString!= NULL){             //你的逻辑            }        }    } 
android如何访问远程服务器

通过传递信使启动服务

 意图serviceIntent =新意图(这一点,WatchService.class);serviceIntent.putExtra(IConstants.MYMESSAGE_HANDLER,新的信使(新MyMessageHandler));startService(serviceIntent); 

在服务onStartCommand获得信使

 如果(意向!= NULL){捆绑mExtras = intent.getExtras();如果(mExtras!= NULL){信使innrMessenger =(信使)mExtras.get(IConstants.MYMESSAGE_HANDLER);}} 

如果您想从业务数据发送到类

 消息消息= Message.obtain();束束=新包();bundle.putString(IConstants.HOME_SCREEN_LISTUPDATE,状态);message.setData(包);innrMessenger.send(消息); //获取打电话回来的handleMessage(消息MSG) 

I have a requirement where I have already extended an "Application" class in my Android Project.

eg:

  public class myApp extends Application implements
    myReceiver.Receiver {...}

Is it possible for me to communicate through a "Service" using my - "Message.obtain" or should I use other things? Please advice.

I also want to pass data to my Service which is a String/constant value. Can I do it like this :

 private void sendMsg(int arg1, int arg2) {  
        if (mBound) {  
            Message msg = Message.obtain(null, MyService.Hello,
                arg1, arg2);  
            try {  
                mService.send(msg);  
             } catch (RemoteException e) {  
                Log.e(TAG, "Error sending a message", e);  

             }  
         }  
     }  

解决方案

try this: in the extends Application class create one inner class

private class MyMessageHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        Bundle bundelData = msg.getData();
        if (bundelData != null) {
            String mString = (String) bundelData.get(IConstants.HOME_SCREEN_LISTUPDATE);
            if (mString != null) {
             // your logic   
            }
        }
    }

starting the service by passing the Messenger

Intent serviceIntent = new Intent(this, WatchService.class);
serviceIntent.putExtra(IConstants.MYMESSAGE_HANDLER, new Messenger(new MyMessageHandler));
startService(serviceIntent);

in the service onStartCommand get the messenger

if (intent != null) {
Bundle mExtras = intent.getExtras();
if (mExtras != null) {
Messenger innrMessenger = (Messenger)mExtras.get(IConstants.MYMESSAGE_HANDLER);
}
}

if you want to send data from service to that class

Message message = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString(IConstants.HOME_SCREEN_LISTUPDATE, state);
message.setData(bundle);
innrMessenger.send(message);//get call back for handleMessage(Message msg)