2016-05-30 56 views
0

我想從一個應用程序發送一個字符串到服務發送字符串服務的應用程序,我使用的綁定服務,我已經實現了這個代碼:從另一個應用程序

package pref.com.app1; 
public class MainActivity extends AppCompatActivity { 


    Messenger mService = null; 
    /** Flag indicating whether we have called bind on the service. */ 
    boolean mIsBound; 



    class IncomingHandler extends Handler { 
     @Override 
     public void handleMessage(Message msg) { 

    Toast.makeText(getApplicationContext(), "IncomingClient--:"+msg.arg1, Toast.LENGTH_LONG).show(); 
     } 
    } 

    /** 
    * Target we publish for clients to send messages to IncomingHandler. 
    */ 
    final Messenger mMessenger = new Messenger(new IncomingHandler()); 
    private ServiceConnection mConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName className, 
             IBinder service) { 
      Toast.makeText(getApplicationContext(), "sn:"+service.toString(), 
        Toast.LENGTH_SHORT).show(); 

      mService = new Messenger(service); 

      try { 
       Message msg = Message.obtain(null, 
         1); 
       msg.replyTo = mMessenger; 
       mService.send(msg); 

       // Give it some value as an example. 
       msg = Message.obtain(null, 
         1, this.hashCode(), 0); 
       mService.send(msg); 
      } catch (RemoteException e) { 

        Toast.makeText(getApplicationContext(), "catch1--:"+e.toString(), 
       Toast.LENGTH_SHORT).show(); 
      } 


     } 

     public void onServiceDisconnected(ComponentName className) { 

      mService = null; 
      Toast.makeText(getApplicationContext(),"Handler-Disconnect---", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }; 


    public void doBindService() { 

     Intent intentForMcuService = new Intent(); 
     intentForMcuService.setComponent(new ComponentName("com.example.newpro", "com.example.newpro.MainActivity")); 
     if (getApplicationContext().bindService(intentForMcuService, mConnection,Context.BIND_AUTO_CREATE 
     )){ 
      Toast.makeText(getApplicationContext(),"binded", 
        Toast.LENGTH_SHORT).show(); 
     }else { 
      Toast.makeText(getApplicationContext(),"not binded", 
        Toast.LENGTH_SHORT).show(); 
     } 

     mIsBound = true; 
     Toast.makeText(getApplicationContext(), "binding-client", 
       Toast.LENGTH_SHORT).show(); 
    } 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     Button sosBtn = (Button) findViewById(R.id.button); 
     sosBtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       doBindService(); 
      } 
     }); 
    } 

,我的服務是:

​​

當我運行兩個應用,APP1顯示 「未綁定」:在這一行 :

Toast.makeText(getApplicationContext(),"not binded", 
         Toast.LENGTH_SHORT).show(); 

有何我該怎麼辦? 請幫助我。 由於事先

+0

'新的組件名稱( 「com.example.newpro」, 「com.example.newpro.MainActivity」)'要創建錯誤'ComponentName' – pskink

+0

pskink,我現在應該做什麼? –

+0

做一個正確的'ComponentName'指向你想要綁定的'Service'? – pskink

回答