0

是否可以将线程绑定到服务?从线程绑定服务

我尝试传递一个字符串到服务运行,并运行一个方法来显示通知?

任何想法什么是正确的做法呢?

在此先感谢。

回答

0

可以试试这种方式: 先写下你的Service类。

public class ShowNotifyService extends Service { 

    private Messenger msg = new Messenger(new ShowNotifyHanlder()); 

    @Override 
    public IBinder onBind(Intent arg0) {     
     return msg.getBinder(); 
    } 

    class ShowNotifyHanlder extends Handler { 

     @Override 
     public void handleMessage(Message msg) { 
      // This is the action 
      int msgType = msg.what; 

      switch(msgType) { 
      case SHOW_FIRST_NOTIFY: { 
       try { 
        // Incoming data 
        String data = msg.getData().getString("data"); 
        Message resp = Message.obtain(null, SHOW_FIRST_NOTIFY_RESPONSE); 
        Bundle bResp = new Bundle(); 
        bResp.putString("respData", first_notify_data);// here you set the data you want to show 
        resp.setData(bResp); 

        msg.replyTo.send(resp); 
       } catch (RemoteException e) { 

        e.printStackTrace(); 
       } 
       break; 
      } 
      default: 
       super.handleMessage(msg); 
      } 
     } 
} 

然后写你的Activity类。

public class TestActivity { 

    .. 
    private ServiceConnection sConn; 
    private Messenger messenger; 
    .. 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      // Service Connection to handle system callbacks 
      sConn = new ServiceConnection() { 

       @Override 
       public void onServiceDisconnected(ComponentName name) { 
        messenger = null; 
       } 

       @Override 
       public void onServiceConnected(ComponentName name, IBinder service) { 
        // We are conntected to the service 
        messenger = new Messenger(service); 

       } 
      }; 
    ... 
      // We bind to the service 
      bindService(new Intent(this, ShowNotifyService.class), sConn, 
        Context.BIND_AUTO_CREATE); 
    .. 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String val = edt.getText().toString(); 
       Message msg = Message.obtain(null, ShowNotifyService.SHOW_FIRST_NOTIFY); 

       msg.replyTo = new Messenger(new ResponseHandler()); 
       // We pass the value 
       Bundle b = new Bundle(); 
       b.putString("data", val); 

       msg.setData(b); 

       try { 
        messenger.send(msg); 
       } catch (RemoteException e) {      
        e.printStackTrace(); 
       } 

      } 

     }); 
    } 

    // This class handles the Service response 
    class ResponseHandler extends Handler { 

     @Override 
     public void handleMessage(Message msg) { 
      int respCode = msg.what; 

      switch (respCode) { 
       case ShowNotifyService.SHOW_FIRST_NOTIFY_RESPONSE: { 
        result = msg.getData().getString("respData"); 
        //then you show the result data from service here 
       } 
      } 
     } 

    } 
} 

我从here得到了这个想法。

+0

如果我不在活动上怎么样? 如果不想从非活动绑定服务,该如何处理? 这可能吗? –

+0

你可以显示你的代码吗? – penkzhou

+0

我解决了。但你给我的答案的主要想法。谢谢。 –