如何将主活动中创建的字符串传递给我的服务? 它是在editText中创建的而不是strings.xml ... 我是java的新手,我找不到它......
由于
编辑//
主要是我:
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");
}
我在我的服务中使用文件名字符串... 这里:
@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;
}
我是android和java的新手,我对它不太了解:/有人可以解释一下吗? 谢谢你耐心等待。
答案 0 :(得分:1)
我没有足够的声誉来添加评论。
您可以通过意图传递它:
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");
}
服务:
@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;
}
如果您想在服务运行时更新字符串,则需要使用服务活页夹,您可以在此教程中找到更多信息link
答案 1 :(得分:0)
意图是一种很好的方法。
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")
}
}
}
并发送你的意图:
Intent serviceIntent = new Intent();
serviceIntent.setClassName(PACKAGE_NAME, CLASS_NAME);
serviceIntent.setAction("your.intent");
serviceIntent.putExtra("EXTRA_NAME", "your string");
context.startService(serviceIntent);