0

How can i pass string created in main activity to my services ? It was created in editText not strings.xml... I am new to java and I can 't find it...

Thanks

edit//

In main I have:

public class MainActivity extends Activity {

private static final String LOG_TAG = "aktivita" ;
private Button btnStart, btnStop;
public EditText input;
public String filename;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnStart = (Button) findViewById(R.id.btnStart);
    btnStop = (Button) findViewById(R.id.btnStop);
    btnStart.setEnabled(true);
    btnStop.setEnabled(true);
    input = (EditText) findViewById(R.id.editTxt);

    Log.d( LOG_TAG, "ON CREATE" );
}

public void onStartClick(View view) {
    filename = input.getText().toString();
    Intent serviceIntentAcc = new Intent(this, accService.class );
    startService(serviceIntentAcc);
    Intent serviceIntentGyro = new Intent(this, gyroService.class );
    startService(serviceIntentGyro);
    btnStop.setEnabled(true);
    btnStart.setEnabled(false);
    Log.d(LOG_TAG, "ON START CLICK");
}

I nned to use filename string in my service ... Here:

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    //String FileName = editTxt.getText().toString();
    try {
        writer = new FileWriter(root + "/" + filename + "acc.txt",true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.d(LOG_TAG, "SERVICE - ON START COMMAND");
    return START_STICKY;
}

I am new to android and java and I do not understand it very well :/ can someone explain it ? Thanks for being patient.

Mirko
  • 39
  • 2
  • 13

2 Answers2

1

I do not have enough reputation to add comment.

You can pass it via intent:

    public class MainActivity extends Activity {

    public static final String FILENAME = "filename";
    private static final String LOG_TAG = "aktivita" ;
    private Button btnStart, btnStop;
    public EditText input;
    public String filename;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStart = (Button) findViewById(R.id.btnStart);
        btnStop = (Button) findViewById(R.id.btnStop);
        btnStart.setEnabled(true);
        btnStop.setEnabled(true);
        input = (EditText) findViewById(R.id.editTxt);


        Log.d( LOG_TAG, "ON CREATE" );
    }

    public void onStartClick(View view) {
        filename = input.getText().toString();
        Intent serviceIntentAcc = new Intent(this, accService.class );

        // add string to intent(filename example)
        serviceIntentAcc.putExtra(FILENAME, filename);

        startService(serviceIntentAcc);
        Intent serviceIntentGyro = new Intent(this, gyroService.class );
        startService(serviceIntentGyro);
        btnStop.setEnabled(true);
        btnStart.setEnabled(false);
        Log.d(LOG_TAG, "ON START CLICK");
    }

Service:

     @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        // retreive string from intent
        String FileName = intent.getStringExtra(FILENAME);

        try {
            writer = new FileWriter(root + "/" + filename + "acc.txt",true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.d(LOG_TAG, "SERVICE - ON START COMMAND");
        return START_STICKY;
    }

If you want to update your string while service is running you will need to use service binder, you can find more information on this tutorial link

  • I understand that you can't post a comment, but this is essentially an answer. Just explain the solution from the link you posted and it should qualify as an answer. – damian Nov 19 '14 at 18:16
0

Intents are a good way to do that.

public class Service extends IntentService {

private final static String TAG = "a tag";
private final static String GET_POWER_INTENT = "your.intent";

/***
 * Constructor
 */
public Service() {
    super(TAG);     
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "onHandleIntent");

    if (extras.containsKey("EXTRA_NAME")) { 
        // your string can be retrieved with extras.getString("EXTRE_NAME")
    }


}

}

And to send your intent :

Intent serviceIntent = new Intent();
    serviceIntent.setClassName(PACKAGE_NAME, CLASS_NAME);
    serviceIntent.setAction("your.intent");
    serviceIntent.putExtra("EXTRA_NAME", "your string");
    context.startService(serviceIntent);
Benoist Laforge
  • 127
  • 1
  • 6