简体   繁体   中英

how to extract the string value in Android services from an activity?

how to extract the string value in Android services from an activity?

My activity has a edit text, the entered string must be received in my services.

TempLaunch.java :

public class TempLaunch extends Activity {

  private EditText text;
  private Button okbtn;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.templaunch);

    addListenerOnButton();

  }

  public void addListenerOnButton() {

    text = (EditText) findViewById(R.id.edittext_newid);    
    okbtn = (Button) findViewById(R.id.button_newid);


    okbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

          Toast.makeText(TempLaunch.this, "In Temp launch class ",Toast.LENGTH_SHORT).show();
          Toast.makeText(TempLaunch.this, text.getText(),Toast.LENGTH_SHORT).show();


            Intent intent = new Intent(getApplicationContext(), MainActivity.class);



            Toast.makeText(TempLaunch.this, "I am in Main activity class ",Toast.LENGTH_SHORT).show();
            startActivity(intent);

        }

    });


    Intent i = new Intent(this, MusicService.class);

    i.putExtra("DD_URL", text.getText().toString());

    //startActivity(i);



  }
}

MusicService.java public class MusicService extends Service {

@Override
    public void onCreate() {
        super.onCreate();

        String url = null;



            Intent intent = getIntent();
            String id = intent.getStringExtra("DD_URL");

            System.out.println("Rosh :" + id);
            Toast.makeText(MusicService.this, "I am in Service:"+ id,Toast.LENGTH_SHORT).show();

...

Please help me out with this regard.

Thanks in advance

You may use sendBroadcast(intent); which will broadcast the EditText from your Activity A.

Then you need to call onReceive(Context context, Intent intent) within your Service class. This method is called when the BroadcastReceiver is receiving an Intent broadcast, in your case will be sent from Activity A.

   private final BroadcastReceiver receiver = new BroadcastReceiver() 
   {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            // Do something
        }
   };

    //Register your receiver in onResume:

    @Override
    protected void onResume() 
    {
        super.onResume();        
        IntentFilter filter = new IntentFilter();
        filter.addAction("SOME_ACTION");
        registerReceiver(receiver, filter);
    }

   //Unregister the receiver in onPause:

    @Override
    protected void onPause() 
    {
        super.onPause();       
        unregisterReceiver(receiver);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM