我正在尝试使用binder类将一个字符串从我的MainActivity传递到我的ServiceAdapter类中。应用程序只需将已写入EditText的内容称为one
,然后将该字符串传递给ServiceAdapter,ServiceAdapter将接收该字符串并将其写入文本文件。我遇到的问题是,如果我只是调用ServiceAdapter类中的方法,无论方法内部是什么,我都会遇到致命的异常错误。我甚至创建了一个方法,只打印“hello world”并且还会崩溃。所以我不确定这里有什么问题,感谢任何帮助。谢谢
//主要活动
Button BtnStart, BtnStop;
EditText Edt;
TextView one;
ServiceAdapter mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BtnStart = (Button)findViewById(R.id.button1);
BtnStop = (Button) findViewById(R.id.button2);
Edt = (EditText) findViewById(R.id.editText1);
one = (TextView) findViewById(R.id.textView1);
}
public String GetText()
{
String Text = this.Edt.getText().toString();
return Text;
}
public void StartService(View v){
//start Service
//startService(new Intent(getBaseContext(), ServiceAdapter.class));
Intent i = new Intent(this,ServiceAdapter.class);
bindService(i, sc, Context.BIND_AUTO_CREATE);
Toast.makeText(getBaseContext(), "Service has been binded", Toast.LENGTH_LONG).show();
this.mService.StringToFile(GetText());
Toast.makeText(getBaseContext(), "Text Written", Toast.LENGTH_LONG).show();
}
public void StopService(View v){
//stop service
unbindService(sc);
Toast.makeText(getBaseContext(), "Service has been unbinded", Toast.LENGTH_LONG).show();
}
private ServiceConnection sc = new ServiceConnection()
{
@Override
public void onServiceDisconnected(ComponentName name)
{
}
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
// serviceAdapter
package com.example.modulefour;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class ServiceAdapter extends Service {
private final IBinder mBinder = new LocalBinder();
MainActivity main;
FileWriter fw;
BufferedWriter bw;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
//create new class to call binder
public class LocalBinder extends Binder{
public ServiceAdapter getService(){
return ServiceAdapter.this;
}
}
public void StringToFile(String x){
//write EditText to text file
String Text = x;
if(!Text.trim().equals(""))
{
File file = new File("TextFile.txt");
//if file doesn't exist, then create it
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//write file
try {
fw = new FileWriter(file.getName(),true);
bw = new BufferedWriter(fw);
bw.write(Text);
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
答案 0 :(得分:1)
致电
时,服务不会立即启动bindService(i, sc, Context.BIND_AUTO_CREATE);
这就是你有这段代码的原因:
private ServiceConnection sc = new ServiceConnection()
{
@Override
public void onServiceDisconnected(ComponentName name)
{
}
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
}
};
正如您所看到的,您可以获得onServiceConnected内部服务的引用,并且您可以安全地调用mService的方法。 尝试添加:
this.mService.StringToFile(GetText());
有:
private ServiceConnection sc = new ServiceConnection()
{
@Override
public void onServiceDisconnected(ComponentName name)
{
}
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
this.mService.StringToFile(GetText());
}
};
(当然,将它从现在的位置移除)