2012-02-20 125 views
2

任何人都可以告訴如何傳遞一個字符串或整數,無論​​從活動到服務。 im試圖通過一個整數setpossition(4),但它並不需要,總是需要0服務,當它開始我不知道爲什麼我不能從活動通過使用服務的實例操作。我該如何將活動中的字符串傳遞給Android中的服務?

public class MainMP3 extends Activity{ 
Button play,stop,prev,next,list; 

static final String MEDIA_PATH = new String("/sdcard/"); 
Activity main_activity; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    main_activity=this; 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mp3interface); 


    play= (Button) findViewById(R.id.play); 
    stop= (Button) findViewById(R.id.stop); 
    prev= (Button) findViewById(R.id.prev); 
    next= (Button) findViewById(R.id.next); 
    list= (Button) findViewById(R.id.listofsongs); 


    play.setOnClickListener(new StartClick()); 

    stop.setOnClickListener(new StopClick()); 

    prev.setOnClickListener(new PullClick()); 





    next.setOnClickListener(new View.OnClickListener() { 

     @Override       
     public void onClick(View v) { 


     } 
    }); 


    list.setOnClickListener(new View.OnClickListener() { 

     @Override       
     public void onClick(View v) { 

      Intent i= new Intent(MainMP3.this,songlist.class); 
      startActivity(i); 

     } 
    }); 

} 




    ServiceMP3 service = null; 

    ServiceConnection connection = new ServiceConnection() { 

     @Override // Called when connection is made 
     public void onServiceConnected(ComponentName cName, IBinder binder) { 
      service = ((ServiceMP3.SlowBinder)binder).getService(); 
     } 
     @Override // 
     public void onServiceDisconnected(ComponentName cName) { 
      service = null; 
     } 
    }; 


    private class StartClick implements View.OnClickListener {  
     public void onClick(View v) { 
      Intent intent = new Intent(main_activity,ServiceMP3.class); 
      service.setposition(4); 
      main_activity.startService(intent); 



     } 
    } 

    private class StopClick implements View.OnClickListener {  
     public void onClick(View v) { 
      Intent intent = new Intent(main_activity,ServiceMP3.class); 
      main_activity.stopService(intent); 

     } 
    } 


    private class PullClick implements View.OnClickListener {  
     public void onClick(View v) { 



     } 
    } 


     } 
+0

您正在創建ServiceConnection,但使用startService()API啓動該服務。考慮使用bindService(),並在重新構建代碼之前,我強烈建議您閱讀本文:http://developer.android.com/reference/android/app/Service.html#LocalServiceSample – 2012-02-21 01:37:33

回答

1

如果你只是在談論將數據傳遞到您的Service,那麼這裏是我的解決方案。當通過Intent推出一個服務,把你想要傳遞給ServiceBundle任何數據,就像這樣:

Bundle bundle = new Bundle(); 
bundle.putInt("my_Val", 4); 
intent.putExtras(bundle); 

然後使用Intent正常人一樣。在另一邊,在你Service距離Bundle獲取數據:

Bundle bundle = intent.getExtras(); 
int myVal = bundle.getInt("my_Val", 0); //0 is just the default value used in case of error 
4

必須設置在意圖上的價值和傳遞的意圖,同時啓動該服務, ,您可以在serivce獲取值onStartCommand (Intent intent,int flags,int startId)。

這是你如何通過意圖傳遞這個整數。

private class StartClick implements View.OnClickListener {  
      public void onClick(View v) { 
       Intent intent = new Intent(main_activity,ServiceMP3.class); 
       intent.putExtra("position",4); 
       main_activity.startService(intent); 

      } 
     } 

你可以得到的數值就是這個樣子

public int onStartCommand(Intent intent, int flags, int startId) { 
    if(intent != null){ 
     int position = intent.getIntExtra("position", 0); 
     } 
    } 
0
  1. 服務創建,在你想要得到你的數據到活動延伸BroadcastReceiver一個內部類:

    private BroadcastReceiver ReceivefromService = new BroadcastReceiver(){ 
        @Override 
        public void onReceive(Context context, Intent intent) 
        { 
         //get the data using the keys you entered at the service 
         String IncomingSms=intent.getStringExtra("incomingSms");// 
         String phoneNumber=intent.getStringExtra("incomingPhoneNumber"); 
    
        } 
    }; 
    
  2. 添加到onPause()

    @Override 
    protected void onPause() { 
        super.onPause(); 
        try { 
         unregisterReceiver(ReceivefromService); 
        } catch (IllegalArgumentException e) { 
         if (e.getMessage().contains("Receiver not registered")) { 
          Log.i("TAG","Tried to unregister the reciver when it's not registered"); 
         } 
         else 
         { 
          throw e; 
         } 
        } 
    } 
    
  3. 一下添加到onResume()

    protected void onResume() { 
        super.onResume(); 
        filter.addAction("android.intent.action.SmsReceiver"); 
        registerReceiver(ReceivefromService, filter); 
        //the first parameter is the name of the inner class we created. 
    } 
    
  4. 接收器/服務,開始廣播像這裏面創建一個意圖:

    Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message); 
    i.putExtra("incomingPhoneNumber", phoneNumber); 
    context.sendBroadcast(i); 
    

,這就是它!祝你好運!

相關問題